Debian Codegraph @

texmf-dist/tex/latex/tikz-dependency/tikz-dependency.sty

raw
1 % Copyright 2011 by Daniele Pighin
2 %
3 % This file may be distributed and/or modified
4 %
5 % 1. under the LaTeX Project Public License and/or
6 % 2. under the GNU Public License.
7 %
8 % See the file doc/generic/tikz-dependency/licenses/LICENSE for more details.
9 %
10 % Changelog
11 %
12 % 1.2 (Nov 3, 2016)
13 % - It's been a while :) Integrated a patch submitted by Jan Strunk <[email protected]>
14 % (thanks a lot Jan!) fixing a bug with the behavior of "edge end x offset".
15 %
16 % 1.1 (Jan 17, 2012)
17 % - Added the possibility to use simple arcs as edges (keys: arc edge/segmented edge)
18 % - Added "edge start x offset" and "edge end x offset" to fine tune edge start/end point position
19 % - Added \depstyle to define styles more easily
20 % - Added "simple" theme, based on the parse on page 1 of [Nivre and McDonald, 2008 ACL-HLT]
21 % - Added instructions to draw bubble parses
22 % - Fixed bug that would cause groupedges not to be straight under some circumstances;
23 %
24 % 1.0 (Nov 26, 2011)
25 % - First public release
26
27 \ProvidesPackage{tikz-dependency}[2011/01/08 v1.1 Macros to draw dependency trees]
28 \NeedsTeXFormat{LaTeX2e}[1999/12/01]
29
30 \RequirePackage{tikz,environ}
31 \usetikzlibrary{matrix,arrows,backgrounds,calc,patterns,positioning,fit,shapes}
32
33 \pgfdeclarelayer{depgroups}
34 \pgfsetlayers{depgroups,main}
35
36 \newcounter{dt@labelid}
37 \newif\ifdt@linkbelow
38 \newif\ifdt@arcedge
39 \tikzset{
40 /depgraph/.cd,
41 /depgraph/.search also = {/tikz},
42 dep id/.code = {\def\dt@depid{#1}},
43 dep id = dependency,
44 % fix the distance of the edge
45 edge height/.code = {\pgfmathsetlengthmacro{\dt@linkheight}{#1}},
46 edge height = 0pt,
47 % the distant of the horizontal line of the edge style for two adjacent words
48 edge unit distance/.code = {\pgfmathsetlengthmacro{\dt@linkstep}{#1}},
49 edge unit distance = 3ex,
50 % the horizontal offset defining the trapezoidal look
51 edge slant/.code = {\pgfmathsetlengthmacro{\dt@linkslant}{#1}},
52 edge slant = 3pt,
53 % the horizontal offset defining the starting/ending position of the edge style
54 edge horizontal padding/.code = {\pgfmathsetlengthmacro{\dt@linkoffset}{#1}},
55 edge horizontal padding = 4pt,
56 % vertical offset of the edge style from the word
57 edge vertical padding/.code = {\pgfmathsetlengthmacro{\dt@linkdist}{#1}},
58 edge vertical padding = 0ex,
59 % should links be placed above or below the sentence
60 edge below/.is if = dt@linkbelow,
61 edge below/.default=true,
62 edge above/.code = {\dt@linkbelowfalse},
63 % arc edge
64 arc edge/.is if = dt@arcedge,
65 segmented edge/.code = {\dt@arcedgefalse},
66 arc angle/.store in = \dt@arcangle,
67 arc angle = 60,
68 % an explicit offset to compensate the x position of the edge
69 edge start x offset/.code = {\pgfmathsetlengthmacro{\dt@EdgeStartOffsetX}{#1}},
70 edge start x offset = 0,
71 edge end x offset/.code = {\pgfmathsetlengthmacro{\dt@EdgeEndOffsetX}{#1}},
72 edge end x offset = 0,
73 % the layer that links should connect
74 % (should be set to "1" for edge above, and to the number of rows
75 % in the matrix for edge below)
76 target layer/.store in = \dt@tgtlayer,
77 target layer/.default = 0,
78 target layer = 0,
79 % styling options
80 reserved/edge style/.style = {->, >=stealth, black, solid, rounded corners = 2, line cap = round, segmented edge},
81 edge style/.style = {reserved/edge style/.append style = {#1}},
82 reserved/label style/.style = {
83 anchor = mid,
84 draw, solid,
85 black,
86 scale = .7,
87 text height = 1.5ex, text depth = 0.25ex, % needed to center text vertically
88 inner sep=.5ex,
89 outer sep = 0pt,
90 rounded corners = 2pt,
91 text = black,
92 fill = white},
93 label style/.style = {reserved/label style/.append style = {#1}},
94 hide label/.style = {reserved/label style/.append style = {opacity = 0, text opacity = 0}},
95 show label/.style = {reserved/label style/.append style = {opacity = 1, text opacity = 1}},
96 text only label/.style = {reserved/label style/.append style = {opacity=0, text opacity=1}},
97 reserved/text style/.style = {
98 text height=1.5ex, text depth = 0.25ex, % needed to center text vertically
99 inner sep = .5ex},
100 text style/.style = {reserved/text style/.append style = {#1}},
101 % group styling
102 reserved/group style/.style = {
103 inner sep = 0,
104 draw, solid,
105 outer sep = .5ex,
106 rounded corners = 2pt},
107 group style/.style = {reserved/group style/.append style = {#1}},
108 % themes for text
109 text theme/.is choice,
110 text theme/default/.style = {text style={black}},
111 text theme/brazil/.style = {text style={blue!60!black}},
112 text theme/iron/.style = {text style={black!80}},
113 text theme/copper/.style = {text style={brown!60!black}},
114 text theme/night/.style = {text style={black}},
115 text theme/grassy/.style = {text style={green!40!black}},
116 text theme/simple/.style = {text style={black}},
117 % themes for labels
118 label theme/.is choice,
119 label theme/default/.style = {label style={fill=white, draw=black}},
120 label theme/night/.style = {label style={text=white, fill=black, font=\bfseries}},
121 label theme/brazil/.style = {label style={thick, black, fill=yellow, text=black, font=\bfseries}},
122 label theme/iron/.style = {label style={top color=black!60, bottom color=black!80, draw=black!80, text=white, font=\bfseries}},
123 label theme/copper/.style = {label style={top color=brown!80!pink, bottom color=brown!60!black, draw=brown!80, text=white, font=\bfseries}},
124 label theme/grassy/.style = {label style={bottom color=green!60!black, top color=green!20!black, draw=green!40!black, text=white, font=\bfseries}},
125 label theme/simple/.style = {label style={draw=none,fill=none,above,font=\scriptsize}},
126 % themes for edges
127 edge theme/.is choice,
128 edge theme/default/.style = {edge style={thin,black}},
129 edge theme/night/.style = {edge style={thick}},
130 edge theme/brazil/.style = {edge style={thick,green!60!black}},
131 edge theme/iron/.style = {edge style={thick, black!80}},
132 edge theme/copper/.style = {edge style={thick, brown!80}},
133 edge theme/grassy/.style = {edge style={thick, green!40!black}},
134 edge theme/simple/.style = {arc edge, arc angle=79},
135 % themes styles
136 theme/.style = {label theme = #1, edge theme = #1, text theme = #1},
137 }
138
139 \newcommand{\depstyle}[2]{\tikzset{#1/.style = {/depgraph/.cd, #2}}}
140
141
142 \newenvironment{dependency}[1][]{%
143 \begin{tikzpicture}[/depgraph/.cd, #1]
144 \begin{scope}
145 }{%
146 \end{scope}%
147 \end{tikzpicture}%
148 }
149
150 \NewEnviron{deptext}[1][]{%
151 \begin{scope}
152 \matrix (\dt@depid)[%
153 nodes = {/depgraph/reserved/text style},
154 column sep = 0,
155 row sep = -.5ex,
156 matrix of nodes,
157 ampersand replacement = \&,
158 nodes in empty cells,
159 #1] {
160 \BODY%
161 };%
162 \pgfmathtruncatemacro\dt@tmp{\pgfmatrixcurrentrow}
163 \xdef\dt@numrows{\dt@tmp}
164 \end{scope}%
165 }
166
167 \newcommand{\depkeys}[1]{\tikzset{/depgraph/.cd, #1}}
168
169 \newcommand{\settgtlayer}{%
170 \ifnum\dt@tgtlayer=0%
171 \ifdt@linkbelow%
172 \pgfmathtruncatemacro{\dt@tgtlayer}{\dt@numrows}%
173 \else%
174 \pgfmathtruncatemacro{\dt@tgtlayer}{1}%
175 \fi%
176 \fi%
177 }
178
179 \newcommand{\deproot}[3][]{% options, root offset, root reserved/label style
180 \begin{scope}
181 \depkeys{#1}
182 \pgfmathsetmacro{\offa}{#2}
183 \settgtlayer
184 \def\anchorpoint{north}
185 \ifdt@linkbelow
186 \def\anchorpoint{south}
187 \fi
188 \def\source{\dt@depid-\dt@tgtlayer-#2}
189
190 \ifdim\dt@linkheight=0pt
191 \pgfmathsetmacro{\distance}{\dt@linkstep * 4}
192 \else
193 \pgfmathsetlengthmacro{\distance}{\dt@linkheight}
194 \fi
195 \pgfmathsetlengthmacro{\dt@startdist}{\dt@linkdist}
196 \ifdt@linkbelow
197 \pgfmathsetmacro{\distance}{-(\dt@linkstep * 4)}
198 \pgfmathsetlengthmacro{\dt@startdist}{-\dt@startdist}
199 \fi
200 \node at (\source) (\rootref) [yshift=\distance, /depgraph/.cd, reserved/label style, #1] {#3};
201 \draw [/depgraph/.cd, reserved/edge style, #1] (\rootref) -- ($(\source.\anchorpoint) + (0, \dt@startdist)$);
202 \end{scope}
203 }
204
205 \newcommand{\depedge}[4][]{% options, source offset, target offset, dep name
206 \begin{scope}
207 \depkeys{#1}
208 \pgfmathsetmacro{\offa}{#2}
209 \pgfmathsetmacro{\offb}{#3}
210 \settgtlayer
211 \def\source{\wordref{\dt@tgtlayer}{#2}}
212 \def\dest{\wordref{\dt@tgtlayer}{#3}}
213 \def\depname{#4}
214 \ifdim\dt@linkheight=0pt
215 \pgfmathsetlengthmacro{\distance}{abs(\offb - \offa)*\dt@linkstep}
216 \else
217 \pgfmathsetlengthmacro{\distance}{\dt@linkheight}
218 \fi
219 \groupedge[#1]{\source}{\dest}{\depname}{\distance}
220 \end{scope}}
221
222 \newlength{\xca}
223 \newlength{\yca}
224 \newlength{\xcb}
225 \newlength{\ycb}
226
227 \newcommand{\wordref}[2]{\dt@depid-#1-#2}
228
229 \newcommand{\rootref}{\dt@depid-root}
230
231 \newcommand{\matrixref}{\dt@depid}
232
233 % \storelabelnode
234 %
235 % \edef#1 the name of the last reserved/label style, stored in \dt@lastlabel
236 %
237 % #1 - a macro
238 \newcommand{\storelabelnode}[1]{\edef#1{\dt@lastlabel}}
239
240 \newcommand{\storefirstcorner}[1]{\edef#1{\dt@lastlabel-edge-first-corner}}
241 \newcommand{\storesecondcorner}[1]{\edef#1{\dt@lastlabel-edge-second-corner}}
242
243 \newcommand{\wordgroup}[5][]{% options, layer, col-start, col-end, identifier
244 \begin{scope}[/depgraph/.cd, #1]
245 \pgfonlayer{depgroups}
246 \node (#5) [fit = (\wordref{#2}{#3}) (\wordref{#2}{#4}), /depgraph/.cd, reserved/group style, #1] {};
247 \endpgfonlayer
248 \end{scope}
249 }
250
251 \newcommand{\groupedge}[5][]{% options, source, target, depname, distance
252 \begin{scope}[/depgraph/.cd, #1]
253 \def\anchorpoint{north}
254 \ifdt@linkbelow
255 \def\anchorpoint{south}
256 \fi
257 \pgfextractx{\xca}{\pgfpointanchor{#2}{\anchorpoint}}
258 \pgfextractx{\xcb}{\pgfpointanchor{#3}{\anchorpoint}}
259 \pgfextracty{\yca}{\pgfpointanchor{#2}{\anchorpoint}}
260 \pgfextracty{\ycb}{\pgfpointanchor{#3}{\anchorpoint}}
261 \pgfmathsetlengthmacro{\ydiff}{\yca-\ycb}
262 \ifdim\xca>\xcb
263 \pgfmathsetlengthmacro{\doff}{-\dt@linkoffset}
264 \pgfmathsetlengthmacro{\dslant}{-\dt@linkslant}
265 \else
266 \pgfmathsetlengthmacro{\doff}{\dt@linkoffset}
267 \pgfmathsetlengthmacro{\dslant}{\dt@linkslant}
268 \fi
269 \addtocounter{dt@labelid}{1}
270 \xdef\dt@lastlabel{\dt@depid-\the\value{dt@labelid}}
271 \pgfmathsetlengthmacro{\dt@startdist}{\dt@linkdist}
272 \pgfmathsetlengthmacro{\dt@enddist}{#5}
273 \ifdt@linkbelow
274 \pgfmathsetlengthmacro{\dt@startdist}{-\dt@startdist}
275 \pgfmathsetlengthmacro{\dt@enddist}{-\dt@enddist}
276 \fi
277
278 % Calculate edge anchors. -edge-first-corner and -edge-second-corner are only
279 % meaningful for segmented edge, but we also draw the nodes for arc edge, someone
280 % may always find these anchors useful...
281 \node (\dt@lastlabel-edge-origin) [coordinate] at ($(#2.\anchorpoint) + (\doff,\dt@startdist) + (\dt@EdgeStartOffsetX,0)$) {};
282 \node (\dt@lastlabel-edge-first-corner) [coordinate] at ($(\dt@lastlabel-edge-origin) + (\dslant,\dt@enddist)$) {};
283 \node (\dt@lastlabel-edge-second-corner) [coordinate] at ($(#3.\anchorpoint) + (\dt@EdgeEndOffsetX,0) + (-\dslant,\dt@enddist+\dt@startdist+\ydiff)$) {};
284 \node (\dt@lastlabel-edge-endpoint) [coordinate] at ($(#3.\anchorpoint) + (0,\dt@startdist) + (\dt@EdgeEndOffsetX,0)$) {};
285
286 \ifdt@arcedge
287 % link above, left to right
288 \pgfmathsetmacro{\dt@arcin}{180-\dt@arcangle}
289 \pgfmathsetmacro{\dt@arcout}{\dt@arcangle}
290 \ifdt@linkbelow
291 % link below
292 \pgfmathsetmacro{\dt@arcin}{-\dt@arcin}
293 \pgfmathsetmacro{\dt@arcout}{-\dt@arcout}
294 \fi
295 \ifdim\xca>\xcb
296 % right to left
297 \pgfmathsetmacro{\dt@temp}{\dt@arcin}
298 \pgfmathsetmacro{\dt@arcin}{\dt@arcout}
299 \pgfmathsetmacro{\dt@arcout}{\dt@temp}
300 \fi
301 %\draw [/depgraph/.cd, reserved/edge style, rounded corners = #5/5, #1]
302 \draw [out=\dt@arcout, in=\dt@arcin,/depgraph/.cd, reserved/edge style, rounded corners = #5/5, #1]
303 (\dt@lastlabel-edge-origin)
304 to node (\dt@lastlabel) [/depgraph/.cd, #1, reserved/label style] {#4}
305 (\dt@lastlabel-edge-endpoint);
306
307 \else
308 \draw [/depgraph/.cd, reserved/edge style, rounded corners = #5/5, #1]
309 (\dt@lastlabel-edge-origin) --
310 (\dt@lastlabel-edge-first-corner) --
311 (\dt@lastlabel-edge-second-corner) --
312 (\dt@lastlabel-edge-endpoint);
313 \node (\dt@lastlabel) [/depgraph/.cd, #1, reserved/label style] at
314 ($ .5*(\dt@lastlabel-edge-second-corner) + .5*(\dt@lastlabel-edge-first-corner) $)
315 {#4};
316 \fi
317 \end{scope}
318 }
319