Debian Codegraph @

texmf-dist/doc/latex/tikz-swigs/tikz-swigs.tex

raw
1 % Note the LaTeX code here uses a package that both shows the code and the output.
2 % This package is: tkzexample
3 % This ensures that the LaTeX corresponds to the figure.
4 %
5 % However, this means that there are extra lines in the source code below:
6 %
7 % \begin{tkzexample}
8 % [....]
9 % \end{tkzexample}
10 %
11 % (This should not cause confusion since the lines do not show up in the
12 % pdf version, but we note it here, in case a reader does not have access to the pdf)
13
14 \documentclass[11pt]{article}
15 \usepackage{natbib}
16 \usepackage{amsmath,amssymb,booktabs,url}
17 \usepackage{xcolor}
18 \usepackage{pgf,tikz}
19 \usetikzlibrary{arrows,shapes.arrows,shapes.geometric,
20 shapes.multipart,decorations.pathmorphing,positioning,
21 swigs}
22
23 \usepackage{tkzexample} % to show code and pictures side by side
24 %%% Note useful tkzexample options here:
25 %% [very small] makes the code small
26 %% [vbox] stacks the figure before the code
27
28 \begin{document}
29
30 \colorlet{graphicbackground}{blue!10!white}%
31 \colorlet{codebackground}{red!10}%
32 \colorlet{codeonlybackground}{red!10}% sets background for tkzltxexample
33
34 \title{TikZ/PGF shape library for constructing Single-World Intervention Graphs (SWIGs)}
35 \author{Thomas S. Richardson\\[2pt]
36 Department of Statistics\\
37 University of Washington}
38 \date{9 July 2021}
39 \maketitle
40
41 Single World Intervention Graphs (SWIGs) are a graphical formalism for unifying two approaches to building
42 (statistical) causal models \citep{swigs}.
43
44 Key to the representation is an operation of `node splitting', whereby a single node is divided into
45 two pieces. It is important that the resulting halves can still be seen to have originated from a single node.
46
47 \begin{center}
48
49 \begin{tikzpicture}
50 \tikzset{line width=1.5pt}
51 \node[name=a1,shape=swig hsplit, swig hsplit={line color lower=red}]{
52 \nodepart{upper}{$A_1$}
53 \nodepart{lower}{$a_1$} };
54 \node[name=a2,shape=swig vsplit, right=of a1, swig vsplit={line color right=red}]{
55 \nodepart{left}{$A_2(a_1)$}
56 \nodepart{right}{$a_2$} };
57 \draw[->,line width=1pt](a1) to[out=350,in=180] (a2);
58 \end{tikzpicture}
59
60 \end{center}
61
62 It has been hard to draw SWIGs using standard packages in TikZ/PGF. Two separate {\tt semicircle} shapes can be used, but it is difficult to ensure that the two halves look as if they arise from a single circle, at least without introducing a lot of whitespace. TikZ does contain a shape, called {\tt split ellipse}, but this does not provide a way to add space between the two halves of the shape.
63 (There does not appear to be a semi-ellipse shape.)
64
65 We address this by introducing two multipart shapes: {\tt swig hsplit} that creates an ellipse that has been split horizontally, and
66 and {\tt swig vsplit} that is split vertically. The latter shape also adjusts the ratio of the two halves depending on the text that is contained.
67
68 \eject
69 \subsection*{Preliminaries}
70
71 The examples included below use the following packages:
72 %%%%%
73 \begin{tkzltxexample}[]
74 \documentclass[10pt]{article}
75 \usepackage{amsmath,amssymb,xcolor}
76 \usepackage{pgf,tikz}
77 \usetikzlibrary{arrows,shapes.arrows,shapes.geometric,
78 shapes.multipart,decorations.pathmorphing,positioning,
79 swigs}
80 \begin{document}
81 ...
82 \end{tkzltxexample}
83
84 %%%%%
85
86 %\noindent The last library listed here uses the code in the file:\par
87 %{\tt tikzlibraryshapes.swigs.code.tex} \par
88 %\noindent (As this is not part of the TeX distribution, this file should be placed in the working directory.)
89
90
91
92
93 \subsection*{SWIG with horizontal split}
94
95 Here is a very simple example:
96
97 \begin{tkzexample}[very small,latex=4cm]
98 \begin{tikzpicture}
99 \node[name=a1,shape=swig hsplit]{
100 \nodepart{upper}{$A_1$}
101 \nodepart{lower}{$a_1$} };
102 \node[name=a2,shape=swig hsplit,right=of a1]{
103 \nodepart{upper}{$A_2(a_1)$}
104 \nodepart{lower}{$a_2$} };
105 \draw[->](a1) to[out=350,in=170] (a2);
106 \end{tikzpicture}
107 \end{tkzexample}
108
109 \bigskip
110
111 \noindent The parameter {\tt gap} can be adjusted to change the size of the gap:
112
113
114
115 \begin{tkzexample}[latex=4cm, very small]
116 \begin{tikzpicture}
117 \node[name=a1,shape=swig hsplit,
118 swig hsplit={gap=5pt}]{
119 \nodepart{upper}{$A_1$}
120 \nodepart{lower}{$a_1$} };
121 \node[name=a2,shape=swig hsplit, right=of a1]{
122 \nodepart{upper}{$A_2(a_1)$}
123 \nodepart{lower}{$a_2$} };
124 \draw[->](a1) to[out=350,in=170] (a2);
125 \end{tikzpicture}
126 \end{tkzexample}
127
128 \medskip
129 \eject
130
131 \noindent This can also be done globally for all split nodes;
132 here we also make the line color red for lower halves.
133
134 \begin{tkzexample}[latex=4cm, very small]
135 \begin{tikzpicture}
136 \tikzset{swig hsplit={gap=5pt,line color lower=red}}
137 \node[name=a1,shape=swig hsplit]{
138 \nodepart{upper}{$A_1$}
139 \nodepart{lower}{$a_1$} };
140 \node[name=a2,shape=swig hsplit, right=of a1]{
141 \nodepart{upper}{$A_2(a_1)$}
142 \nodepart{lower}{$a_2$} };
143 \draw[->](a1) to[out=350,in=170] (a2);
144 \end{tikzpicture}
145 \end{tkzexample}
146
147 \medskip
148
149 \noindent For black and white publications color may not be sufficient to distinguish
150 upper and lower halves. For this purpose a double line can be used.
151 {\tt inner line width lower} specifies the width of the inner gap.
152
153 \begin{tkzexample}[latex=4cm, very small]
154 \begin{tikzpicture}
155 \tikzset{line width=1.5pt,
156 swig hsplit={gap=4pt,
157 inner line width lower=0.5pt}}]
158 \node[name=a1,shape=swig hsplit]{
159 \nodepart{upper}{$A_1$}
160 \nodepart{lower}{$a_1$} };
161 \node[name=a2,shape=swig hsplit, right=of a1]{
162 \nodepart{upper}{$A_2(a_1)$}
163 \nodepart{lower}{$a_2$} };
164 \draw[->,line width=1.5pt,>=stealth](a1)
165 to[out=350,in=170] (a2);
166 \end{tikzpicture}
167 \end{tkzexample}
168
169 \eject
170
171 \noindent The example below shows the full set of anchors and other options.
172
173 \begin{center}
174
175 \begin{tkzexample}[vbox, very small]
176 \Huge
177 \begin{tikzpicture}
178 \pgfsetinnerstrokecolor{blue!10!white} % so inner line col=background
179 \tikzset{shape example/.style={fill=yellow!5,
180 inner sep=0.3cm,outer sep=0cm}}
181 \node[name=s, shape example, shape=swig hsplit,
182 swig hsplit={
183 line color upper=red!30,
184 fill color upper=yellow!30,
185 line color lower=green!30,
186 fill color lower=yellow!30,
187 gap=40pt,
188 line width upper= 10pt,
189 inner line width upper = 0pt,
190 line width lower=15pt,
191 inner line width lower = 4pt}]
192 {\nodepart[red!30]{upper}{$A_4(a_1,a_2)$}
193 \nodepart[green!30]{lower}{$a_4$}};
194 \draw[->,line width=5pt, draw=blue!15,>=stealth]
195 (s.upper center) to (s.lower center);
196 \foreach \anchor/\placement in
197 {center/right, upper center/left,
198 upper/below, lower center/left, lower/left,
199 60/above right, 200/below left,
200 mid/above, mid east/above right, mid west/above left,
201 upper base/below right, base east/right, base west/left,
202 upper east/right, upper west/left,
203 north/above, south/below, west/left, east/right,
204 lower west/left, lower east/right,
205 north east/above right, south east/below right,
206 south west/below left, north west/above left,
207 lower base/below right, lower mid/above}
208 \draw[shift=(s.\anchor)] plot[mark=x] coordinates{(0,0)}
209 node[\placement] {\scriptsize\texttt{(s.\anchor)}};
210 \end{tikzpicture}
211 \end{tkzexample}
212 \end{center}
213
214 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
215
216 \eject
217
218 \subsection*{SWIG with vertical split}
219
220 Here we describe the instructions for creating ellipses with vertical splits. These are often more efficient in terms of space.
221 As before, first a very simple example:
222
223 \begin{tkzexample}[very small,latex=5.5cm]
224 \begin{tikzpicture}
225 \node[name=a1,shape=swig vsplit]{
226 \nodepart{left}{$A_1$}
227 \nodepart{right}{$a_1$} };
228 \node[name=a2, right=5mm of a1,
229 shape=swig vsplit] {
230 \nodepart{left}{$A_2(a_1)$}
231 \nodepart{right}{$a_2$} };
232 \draw[->](a1) to (a2);
233 \end{tikzpicture}
234 \end{tkzexample}
235
236 \bigskip
237
238 \noindent The parameter {\tt gap} can be adjusted to change the size of the gap:
239
240 \begin{tkzexample}[very small,latex=5cm]
241 \begin{tikzpicture}
242 \node[name=a1,shape=swig vsplit,
243 swig vsplit={gap=3pt}]{
244 \nodepart{left}{$A_1$}
245 \nodepart{right}{$a_1$} };
246 \node[name=a2, right=3mm of a1,
247 shape=swig vsplit] {
248 \nodepart{left}{$A_2(a_1)$}
249 \nodepart{right}{$a_2$} };
250 \draw[->](a1) to (a2);
251 \end{tikzpicture}
252 \end{tkzexample}
253
254 \medskip
255
256 \noindent This can also be done globally for all split nodes;
257 here we also make the line color red for right halves.
258
259 \begin{tkzexample}[latex=5cm, very small]
260 \begin{tikzpicture}
261 \tikzset{swig vsplit={gap=3pt,
262 line color right=red}}
263 \node[name=a1,shape=swig vsplit]{
264 \nodepart{left}{$A_1$}
265 \nodepart{right}{$a_1$} };
266 \node[name=a2,shape=swig vsplit,
267 right=5mm of a1]{
268 \nodepart{left}{$A_2(a_1)$}
269 \nodepart{right}{$a_2$} };
270 \draw[->](a1) to (a2);
271 \end{tikzpicture}
272 \end{tkzexample}
273
274 \medskip
275
276 \eject
277
278 \noindent As before, a version for black and white publications:
279
280 \begin{tkzexample}[latex=5cm, very small]
281 \begin{tikzpicture}
282 \tikzset{line width=1.5pt,
283 swig vsplit={gap=3pt,
284 inner line width right=0.5pt}}
285 \node[name=a1,shape=swig vsplit]{
286 \nodepart{left}{$A_1$}
287 \nodepart{right}{$a_1$} };
288 \node[name=a2,shape=swig vsplit,
289 right=3mm of a1]{
290 \nodepart{left}{$A_2(a_1)$}
291 \nodepart{right}{$a_2$} };
292 \draw[->,line width=1.5pt,>=stealth]
293 (a1) to (a2);
294 \end{tikzpicture}
295 \end{tkzexample}
296
297 \bigskip
298
299 \noindent Here is an example with multiple stages:
300
301 \begin{tkzexample}[vbox]
302 \begin{tikzpicture}
303 \tikzset{line width=1.5pt, outer sep=0pt,
304 ell/.style={draw,fill=white, inner sep=2pt,
305 line width=1.5pt},
306 swig vsplit={gap=5pt,
307 inner line width right=0.5pt}};
308 \node[name=l1, ell, shape=ellipse]{$L_1$};
309 \node[name=a1,right=5mm of l1, shape=swig vsplit]{
310 \nodepart{left}{$A_1$}
311 \nodepart{right}{$a_1$} };
312 \node[name=l2, right=5mm of a1, ell, shape=ellipse]{$L_2(a_1)$};
313 \node[name=a2,shape=swig vsplit,
314 right=5mm of l2]{
315 \nodepart{left}{$A_2(a_1)$}
316 \nodepart{right}{$a_2$} };
317 \node[name=y, right=5mm of a2, ell, shape=ellipse]{$Y(a_1,a_2)$};
318 \draw[->,line width=1.5pt,>=stealth]
319 (l1) edge (a1)
320 (l1) edge[out=330,in=210] (a2)
321 (a1) edge (l2)
322 (a1) edge[out=15,in=150] (a2)
323 (a1) edge[out=30,in=150] (y)
324 (l2) edge (a2)
325 (a2) edge (y);
326 \end{tikzpicture}
327 \end{tkzexample}
328
329
330
331
332
333 \eject
334
335 \noindent Expanded nodes showing additional options and anchors.
336
337 \begin{tkzexample}[vbox, very small]
338 \Huge
339 \begin{tikzpicture}
340 \pgfsetinnerstrokecolor{blue!10!white} % so inner line col=background
341 \tikzset{shape example/.style={draw,fill=yellow!30,
342 inner xsep=10pt,inner ysep=25pt,outer xsep=0cm,outer ysep=0cm},
343 swig vsplit={line color right=green!30, line color left=red!20,
344 gap=25pt, line width left= 10pt, line width right=15pt,
345 inner line width left = 0pt, inner line width right = 4pt}}
346 \node[name=s,shape=swig vsplit, shape example]
347 {\nodepart[red!20]{left}{$Y_1(a_1)$}
348 \nodepart[green!30]{right}{$y_1$}};
349 \foreach\anchor/\placement in
350 {north/above, south/below, east/right, west/left,
351 left base/below, right base/below, right/above, left/above,
352 left mid/above, mid east/right, mid west/left, base east/right,
353 base west/left, right mid/above,
354 142/above, 320/below, center/below}
355 \draw[shift=(s.\anchor)] plot[mark=x] coordinates{(0,0)}
356 node[\placement] {\scriptsize\texttt{(s.\anchor)}};
357 \begin{scope}[yshift=-4cm]
358 \node[name=s,shape=swig vsplit, shape example]
359 {\nodepart[red!20]{left}{$Y_1(a_1)$}%
360 \nodepart[green!30]{right}{$y_1$}};
361 \foreach\anchor/\placement in
362 {left center/below, right center/above,
363 left north/above left, left south/below left,
364 right north/above right, right south/below right,
365 north west/above left, south west/below left,
366 north east/above right, south east/below right}
367 \draw[shift=(s.\anchor)] plot[mark=x] coordinates{(0,0)}
368 node[\placement] {\scriptsize\texttt{(s.\anchor)}};
369 \end{scope}
370 \end{tikzpicture}
371 \end{tkzexample}
372
373
374 \bibliographystyle{chicago}
375
376 \begin{thebibliography}{}
377
378 \bibitem[\protect\citeauthoryear{Richardson and Robins}{Richardson and
379 Robins}{2013}]{swigs}
380 Richardson, T.S. and J.M.~Robins (2013).
381 \newblock Single world intervention graphs (SWIGs): A unification of
382 counterfactual and graphical approaches to causality.
383 \newblock Working Paper Number 128. Available at
384 \url{http://www.csss.washington.edu/Papers/wp128.pdf}.
385
386 \end{thebibliography}
387
388 %\bibliography{references}
389
390 \end{document}
391