summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/latex/tikz-dependency/tikz-dependency.sty
blob: 6aa31c3c867cbc9500f9c0992a77597dae90b60a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
\ProvidesPackage{tikz-dependency}[2011/01/08 v1.0 Macros to draw dependency trees]
\NeedsTeXFormat{LaTeX2e}[1999/12/01]

\RequirePackage{tikz,environ}
\usetikzlibrary{matrix,arrows,backgrounds,calc,patterns,positioning,fit}

\pgfdeclarelayer{depgroups}
\pgfsetlayers{depgroups,main}

\newcounter{dt@labelid}
\newif\ifdt@linkbelow
\tikzset{
   /depgraph/.cd,
   /depgraph/.search also = {/tikz},
   dep id/.code = {\def\dt@depid{#1}},
   dep id = dependency,
   % the distant of the horizontal line of the edge style  for two adjacent words
   edge unit distance/.code = {\pgfmathsetlengthmacro{\dt@linkstep}{#1}},
   edge unit distance = 3ex,
   % the horizontal offset defining the trapezoidal look
   edge slant/.code = {\pgfmathsetlengthmacro{\dt@linkslant}{#1}},
   edge slant = 3pt,
   % the horizontal offset defining the starting/ending position of the edge style
   edge horizontal padding/.code = {\pgfmathsetlengthmacro{\dt@linkoffset}{#1}},
   edge horizontal padding = 4pt,
   % vertical offset of the edge style from the word
   edge vertical padding/.code = {\pgfmathsetlengthmacro{\dt@linkdist}{#1}}, 
   edge vertical padding = 0ex,
   % should links be placed above or below the sentence
   edge below/.is if = dt@linkbelow,
   edge below/.default=true,
   edge above/.code = {\dt@linkbelowfalse},
   % the layer that links should connect 
   % (should be set to "1" for edge above, and to the number of rows 
   % in the matrix for edge below)
   target layer/.store in = \dt@tgtlayer,
   target layer/.default = 0,
   target layer = 0,
   % styling options
   reserved/edge style/.style = {->, >=stealth, black, solid, rounded corners = 2, line cap = round},
   edge style/.style = {reserved/edge style/.append style = {#1}},
   reserved/label style/.style = {
      anchor = mid,
      draw, solid,
      black,
      scale = .7,
      text height = 1.5ex, text depth = 0.25ex, % needed to center text vertically
      inner sep=.5ex, 
      outer sep = 0pt,
      rounded corners = 2pt, 
      text = black, 
      fill = white},
   label style/.style = {reserved/label style/.append style = {#1}},
   hide label/.style = {reserved/label style/.append style = {opacity = 0, text opacity = 0}},
   show label/.style = {reserved/label style/.append style = {opacity = 1, text opacity = 1}},
   reserved/text style/.style = {
      text height=1.5ex, text depth = 0.25ex, % needed to center text vertically
      inner sep = .5ex},
   text style/.style = {reserved/text style/.append style = {#1}},
   reserved/group style/.style = {
      inner sep = 0,
      draw, solid,
      outer sep = .5ex,
      rounded corners = 2pt},
   group style/.style = {reserved/group style/.append style = {#1}},
}

\newenvironment{dependency}[1][]{%
   \begin{tikzpicture}
      \begin{scope}[/depgraph/.cd, #1]
}{%
      \end{scope}%
   \end{tikzpicture}%
}

\NewEnviron{deptext}[1][]{%
   \begin{scope}
      \matrix (\dt@depid)[%
         nodes = {/depgraph/reserved/text style},
         column sep = 0,
         row sep = -.5ex,
         matrix of nodes,
         ampersand replacement = \&,
         nodes in empty cells,
         #1] {
            \BODY%
         };%
         \pgfmathtruncatemacro\dt@tmp{\pgfmatrixcurrentrow}
         \xdef\dt@numrows{\dt@tmp}
   \end{scope}%
}

\newcommand{\depkeys}[1]{\tikzset{/depgraph/.cd, #1}}

\newcommand{\settgtlayer}{%
   \ifnum\dt@tgtlayer=0%
      \ifdt@linkbelow%
         \pgfmathtruncatemacro{\dt@tgtlayer}{\dt@numrows}%
      \else%
         \pgfmathtruncatemacro{\dt@tgtlayer}{1}%
      \fi%
   \fi%
}

\newcommand{\deproot}[3][]{% options, root offset, root reserved/label style
   \begin{scope}
      \depkeys{#1}
      \pgfmathsetmacro{\offa}{#2}
      \settgtlayer
      \def\anchorpoint{north}
      \ifdt@linkbelow
         \def\anchorpoint{south}
      \fi
      \def\source{\dt@depid-\dt@tgtlayer-#2}
      \pgfmathsetmacro{\distance}{\dt@linkstep * 4}
      \pgfmathsetlengthmacro{\dt@startdist}{\dt@linkdist}
      \ifdt@linkbelow
         \pgfmathsetmacro{\distance}{-(\dt@linkstep * 4)}
      \pgfmathsetlengthmacro{\dt@startdist}{-\dt@startdist}
      \fi
      \node at (\source) (\rootref) [yshift=\distance, /depgraph/.cd, reserved/label style, #1] {#3};
      \draw [/depgraph/.cd, reserved/edge style, #1] (\rootref) -- ($(\source.\anchorpoint) + (0, \dt@startdist)$);
   \end{scope}
}

\newcommand{\depedge}[4][]{% options, source offset, target offset, dep name
   \begin{scope}
   \depkeys{#1}
   \pgfmathsetmacro{\offa}{#2}
   \pgfmathsetmacro{\offb}{#3}
   \settgtlayer
   \def\source{\wordref{\dt@tgtlayer}{#2}}
   \def\dest{\wordref{\dt@tgtlayer}{#3}}
   \def\depname{#4}
   \pgfmathsetlengthmacro{\distance}{abs(\offb - \offa)*\dt@linkstep}
   \groupedge[#1]{\source}{\dest}{\depname}{\distance}
   \end{scope}}

\newlength{\xca}
\newlength{\yca}
\newlength{\xcb}
\newlength{\ycb}

\newcommand{\wordref}[2]{\dt@depid-#1-#2}

\newcommand{\rootref}{\dt@depid-root}

\newcommand{\matrixref}{\dt@depid}

% \storelabelnode
% 
% \edef#1 the name of the last reserved/label style, stored in \dt@lastlabel
%
% #1 - a macro
\newcommand{\storelabelnode}[1]{\edef#1{\dt@lastlabel}}

\newcommand{\storefirstcorner}[1]{\edef#1{\dt@lastlabel-edge-first-corner}}
\newcommand{\storesecondcorner}[1]{\edef#1{\dt@lastlabel-edge-second-corner}}

\newcommand{\wordgroup}[5][]{% options, layer, col-start, col-end, identifier
   \begin{scope}[/depgraph/.cd, #1]
      \pgfonlayer{depgroups}
      \node (#5) [fit = (\wordref{#2}{#3}) (\wordref{#2}{#4}), /depgraph/.cd, reserved/group style, #1] {};
      \endpgfonlayer
   \end{scope}
}

\newcommand{\groupedge}[5][]{% options, source, target, depname, distance
   \begin{scope}[/depgraph/.cd, #1]
      \def\anchorpoint{north}
      \ifdt@linkbelow
         \def\anchorpoint{south}
      \fi
      \pgfextractx{\xca}{\pgfpointanchor{#2}{\anchorpoint}}
      \pgfextractx{\xcb}{\pgfpointanchor{#3}{\anchorpoint}}
      \pgfextracty{\yca}{\pgfpointanchor{#2}{\anchorpoint}}
      \pgfextracty{\ycb}{\pgfpointanchor{#3}{\anchorpoint}}
      \pgfmathsetlengthmacro{\ydiff}{abs(\yca-\ycb)}
      \ifdim\xca>\xcb
         \pgfmathsetlengthmacro{\doff}{-\dt@linkoffset}
         \pgfmathsetlengthmacro{\dslant}{-\dt@linkslant}
      \else
         \pgfmathsetlengthmacro{\doff}{\dt@linkoffset}
         \pgfmathsetlengthmacro{\dslant}{\dt@linkslant}
      \fi
      \addtocounter{dt@labelid}{1}
      \xdef\dt@lastlabel{\dt@depid-\the\value{dt@labelid}}
      \pgfmathsetlengthmacro{\dt@startdist}{\dt@linkdist}
      \pgfmathsetlengthmacro{\dt@enddist}{#5}
      \ifdt@linkbelow
         \pgfmathsetlengthmacro{\dt@startdist}{-\dt@startdist}
         \pgfmathsetlengthmacro{\dt@enddist}{-\dt@enddist}
      \fi
      \node (\dt@lastlabel-edge-origin) [coordinate] at ($(#2.\anchorpoint) + (\doff,\dt@startdist)$) {};
      \node (\dt@lastlabel-edge-first-corner) [coordinate] at ($(\dt@lastlabel-edge-origin) + (\dslant,\dt@enddist)$) {};
      \node (\dt@lastlabel-edge-second-corner) [coordinate] at ($(#3.\anchorpoint) + (-\dslant,\dt@enddist+\dt@startdist+\ydiff)$) {};
      \node (\dt@lastlabel-edge-endpoint) [coordinate] at ($(#3.\anchorpoint) + (0,\dt@startdist)$) {}; 

      \draw [/depgraph/.cd, reserved/edge style, rounded corners = #5/5, #1] 
         (\dt@lastlabel-edge-origin) --
         (\dt@lastlabel-edge-first-corner) --
         (\dt@lastlabel-edge-second-corner) --
         (\dt@lastlabel-edge-endpoint);
      \node (\dt@lastlabel) [/depgraph/.cd, #1, reserved/label style] at
      ($ .5*(\dt@lastlabel-edge-second-corner) + .5*(\dt@lastlabel-edge-first-corner) $)
      {#4};
   \end{scope}
}