summaryrefslogtreecommitdiff
path: root/graphics/pgf/contrib/tikz-swigs/tikz-swigs.tex
blob: c0afc9a9721c1be2926c1fe7aa3b71cf3d1ca27c (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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
% Note the LaTeX code here uses a package that both shows the code and the output.
% This package is: tkzexample
% This ensures that the LaTeX corresponds to the figure.
%
% However, this means that there are extra lines in the source code below:
%
% \begin{tkzexample}
%  [....]
% \end{tkzexample} 
%
% (This should not cause confusion since the lines do not show up in the 
% pdf version, but we note it here, in case a reader does not have access to the pdf)

\documentclass[11pt]{article}
\usepackage{natbib}
\usepackage{amsmath,amssymb,booktabs,url}
\usepackage{xcolor}
\usepackage{pgf,tikz}
\usetikzlibrary{arrows,shapes.arrows,shapes.geometric,
shapes.multipart,decorations.pathmorphing,positioning,
swigs}

\usepackage{tkzexample} % to show code and pictures side by side
%%% Note useful tkzexample options here:
%% [very small] makes the code small
%% [vbox]   stacks the figure before the code

\begin{document}

\colorlet{graphicbackground}{blue!10!white}%
\colorlet{codebackground}{red!10}%  
\colorlet{codeonlybackground}{red!10}% sets background for tkzltxexample

\title{TikZ/PGF shape library for constructing Single-World Intervention Graphs (SWIGs)}
\author{Thomas S. Richardson\\[2pt]
Department of Statistics\\
 University of Washington}
\date{9 July 2021}
\maketitle

Single World Intervention Graphs (SWIGs) are a graphical formalism for unifying two approaches to building
(statistical) causal models \citep{swigs}.

Key to the representation is an operation of `node splitting', whereby a single node is divided into
two pieces. It is important that the resulting halves can still be seen to have originated from a single node.

\begin{center}

\begin{tikzpicture}
\tikzset{line width=1.5pt}
\node[name=a1,shape=swig hsplit, swig hsplit={line color lower=red}]{
        \nodepart{upper}{$A_1$}
        \nodepart{lower}{$a_1$}   };
\node[name=a2,shape=swig vsplit, right=of a1, swig vsplit={line color right=red}]{
         \nodepart{left}{$A_2(a_1)$}
         \nodepart{right}{$a_2$}  };
\draw[->,line width=1pt](a1) to[out=350,in=180] (a2);
\end{tikzpicture}

\end{center}

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.
(There does not appear to be a semi-ellipse shape.)

We address this by introducing two multipart shapes: {\tt swig hsplit}  that creates an ellipse that has been split horizontally, and
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.

\eject
\subsection*{Preliminaries}

The examples included below use the following packages:
%%%%%
\begin{tkzltxexample}[]
\documentclass[10pt]{article}
\usepackage{amsmath,amssymb,xcolor}
\usepackage{pgf,tikz}
\usetikzlibrary{arrows,shapes.arrows,shapes.geometric,
shapes.multipart,decorations.pathmorphing,positioning,
swigs}
\begin{document}
...
\end{tkzltxexample}

%%%%%

%\noindent The last library listed here uses the code in the file:\par
%{\tt tikzlibraryshapes.swigs.code.tex} \par
%\noindent (As this is not part of the TeX distribution,  this file should be placed in the working directory.)




\subsection*{SWIG with horizontal split}

Here is a very simple example:

\begin{tkzexample}[very small,latex=4cm] 
\begin{tikzpicture}
\node[name=a1,shape=swig hsplit]{
        \nodepart{upper}{$A_1$}
        \nodepart{lower}{$a_1$}   };
\node[name=a2,shape=swig hsplit,right=of a1]{
         \nodepart{upper}{$A_2(a_1)$}
         \nodepart{lower}{$a_2$}  };
\draw[->](a1) to[out=350,in=170] (a2);
\end{tikzpicture}
\end{tkzexample} 

\bigskip

\noindent The parameter {\tt gap} can be adjusted to change the size of the gap:



\begin{tkzexample}[latex=4cm, very small] 
\begin{tikzpicture}
\node[name=a1,shape=swig hsplit,
swig hsplit={gap=5pt}]{
        \nodepart{upper}{$A_1$}
        \nodepart{lower}{$a_1$}  };
\node[name=a2,shape=swig hsplit, right=of a1]{
        \nodepart{upper}{$A_2(a_1)$}
        \nodepart{lower}{$a_2$}  };
\draw[->](a1) to[out=350,in=170] (a2);
\end{tikzpicture}
\end{tkzexample} 

\medskip
\eject

\noindent This can also be done globally for all split nodes;
here we also make the line color red for lower halves.

\begin{tkzexample}[latex=4cm, very small]
\begin{tikzpicture}
\tikzset{swig hsplit={gap=5pt,line color lower=red}}
\node[name=a1,shape=swig hsplit]{
        \nodepart{upper}{$A_1$}
        \nodepart{lower}{$a_1$} };
\node[name=a2,shape=swig hsplit, right=of a1]{
        \nodepart{upper}{$A_2(a_1)$}
        \nodepart{lower}{$a_2$} };
\draw[->](a1) to[out=350,in=170] (a2);
\end{tikzpicture}
\end{tkzexample}

\medskip

\noindent For black and white publications color may not be sufficient to distinguish
upper and lower halves. For this purpose a double line can be used.
{\tt inner line width lower} specifies the width of the inner gap.

\begin{tkzexample}[latex=4cm, very small]
\begin{tikzpicture}
\tikzset{line width=1.5pt, 
    swig hsplit={gap=4pt,
                 inner line width lower=0.5pt}}]
\node[name=a1,shape=swig hsplit]{
        \nodepart{upper}{$A_1$}
        \nodepart{lower}{$a_1$} };
\node[name=a2,shape=swig hsplit, right=of a1]{
        \nodepart{upper}{$A_2(a_1)$}
        \nodepart{lower}{$a_2$} };
\draw[->,line width=1.5pt,>=stealth](a1) 
    to[out=350,in=170] (a2);
\end{tikzpicture}
\end{tkzexample}

\eject

\noindent The example below shows the full set of anchors and other options.

\begin{center}

\begin{tkzexample}[vbox, very small] 
\Huge
\begin{tikzpicture}
\pgfsetinnerstrokecolor{blue!10!white} % so inner line col=background
\tikzset{shape example/.style={fill=yellow!5,
            inner sep=0.3cm,outer sep=0cm}}
  \node[name=s, shape example, shape=swig hsplit,
       swig hsplit={
             line color upper=red!30, 
             fill color upper=yellow!30,
             line color lower=green!30,
             fill color lower=yellow!30,  
             gap=40pt,
             line width upper= 10pt, 
             inner line width upper = 0pt, 
             line width lower=15pt,  
             inner line width lower = 4pt}]
            {\nodepart[red!30]{upper}{$A_4(a_1,a_2)$}
             \nodepart[green!30]{lower}{$a_4$}};
  \draw[->,line width=5pt, draw=blue!15,>=stealth] 
                              (s.upper center) to (s.lower center);
  \foreach \anchor/\placement in
    {center/right, upper center/left, 
    upper/below, lower center/left, lower/left,
    60/above right,  200/below left, 
     mid/above, mid east/above right, mid west/above left,
     upper base/below right, base east/right, base west/left, 
     upper east/right, upper west/left,
     north/above, south/below, west/left, east/right,
     lower west/left, lower east/right,
     north east/above right, south east/below right, 
     south west/below left, north west/above left, 
     lower base/below right, lower mid/above}
     \draw[shift=(s.\anchor)] plot[mark=x] coordinates{(0,0)}
       node[\placement] {\scriptsize\texttt{(s.\anchor)}};   
\end{tikzpicture}
\end{tkzexample}
\end{center}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\eject

\subsection*{SWIG with vertical split}

Here we describe the instructions for creating ellipses with vertical splits. These are often more efficient in terms of space.
As before, first a very simple example:

\begin{tkzexample}[very small,latex=5.5cm] 
\begin{tikzpicture}
\node[name=a1,shape=swig vsplit]{
        \nodepart{left}{$A_1$}
        \nodepart{right}{$a_1$}  };
\node[name=a2, right=5mm of a1,
             shape=swig vsplit] {
             \nodepart{left}{$A_2(a_1)$}
             \nodepart{right}{$a_2$} };
\draw[->](a1) to (a2);
\end{tikzpicture}
\end{tkzexample} 

\bigskip

\noindent The parameter {\tt gap} can be adjusted to change the size of the gap:

\begin{tkzexample}[very small,latex=5cm] 
\begin{tikzpicture}
\node[name=a1,shape=swig vsplit,
  swig vsplit={gap=3pt}]{
        \nodepart{left}{$A_1$}
        \nodepart{right}{$a_1$}  };
\node[name=a2, right=3mm of a1,
             shape=swig vsplit] {
             \nodepart{left}{$A_2(a_1)$}
             \nodepart{right}{$a_2$} };
\draw[->](a1) to (a2);
\end{tikzpicture}
\end{tkzexample} 

\medskip

\noindent This can also be done globally for all split nodes;
here we also make the line color red for right halves.

\begin{tkzexample}[latex=5cm, very small]
\begin{tikzpicture}
\tikzset{swig vsplit={gap=3pt,
                  line color right=red}}
\node[name=a1,shape=swig vsplit]{
        \nodepart{left}{$A_1$}
        \nodepart{right}{$a_1$} };
\node[name=a2,shape=swig vsplit, 
                  right=5mm of a1]{
        \nodepart{left}{$A_2(a_1)$}
        \nodepart{right}{$a_2$} };
\draw[->](a1) to (a2);
\end{tikzpicture}
\end{tkzexample}

\medskip

\eject

\noindent  As before, a version for black and white publications:

\begin{tkzexample}[latex=5cm, very small]
\begin{tikzpicture}
\tikzset{line width=1.5pt, 
         swig vsplit={gap=3pt,
         inner line width right=0.5pt}}
\node[name=a1,shape=swig vsplit]{
        \nodepart{left}{$A_1$}
        \nodepart{right}{$a_1$} };
\node[name=a2,shape=swig vsplit, 
                  right=3mm of a1]{
        \nodepart{left}{$A_2(a_1)$}
        \nodepart{right}{$a_2$} };
\draw[->,line width=1.5pt,>=stealth]
    (a1) to (a2);
\end{tikzpicture}
\end{tkzexample}

\bigskip

\noindent Here is an example with multiple stages:

\begin{tkzexample}[vbox]
\begin{tikzpicture}
\tikzset{line width=1.5pt, outer sep=0pt,
         ell/.style={draw,fill=white, inner sep=2pt,
          line width=1.5pt},
         swig vsplit={gap=5pt,
         inner line width right=0.5pt}};
\node[name=l1, ell, shape=ellipse]{$L_1$};
\node[name=a1,right=5mm of l1, shape=swig vsplit]{
        \nodepart{left}{$A_1$}
        \nodepart{right}{$a_1$} };
\node[name=l2, right=5mm of a1, ell, shape=ellipse]{$L_2(a_1)$};
\node[name=a2,shape=swig vsplit, 
                  right=5mm of l2]{
        \nodepart{left}{$A_2(a_1)$}
        \nodepart{right}{$a_2$} };
\node[name=y, right=5mm of a2, ell, shape=ellipse]{$Y(a_1,a_2)$};
\draw[->,line width=1.5pt,>=stealth]
    (l1) edge (a1)
     (l1) edge[out=330,in=210] (a2)
    (a1) edge (l2)
    (a1) edge[out=15,in=150] (a2)
    (a1) edge[out=30,in=150] (y)
    (l2) edge (a2)
    (a2) edge (y);
\end{tikzpicture}
\end{tkzexample}





\eject

\noindent Expanded nodes showing additional options and anchors.

\begin{tkzexample}[vbox, very small] 
\Huge
\begin{tikzpicture}
\pgfsetinnerstrokecolor{blue!10!white} % so inner line col=background
\tikzset{shape example/.style={draw,fill=yellow!30,
             inner xsep=10pt,inner ysep=25pt,outer xsep=0cm,outer ysep=0cm},
             swig vsplit={line color right=green!30, line color left=red!20, 
                        gap=25pt, line width left= 10pt, line width right=15pt,  
                      inner line width left = 0pt, inner line width right = 4pt}}
  \node[name=s,shape=swig vsplit, shape example]
            {\nodepart[red!20]{left}{$Y_1(a_1)$}
              \nodepart[green!30]{right}{$y_1$}};
 \foreach\anchor/\placement in 
  {north/above, south/below, east/right, west/left, 
 left base/below, right base/below, right/above, left/above,
 left mid/above, mid east/right, mid west/left, base east/right,
 base west/left, right mid/above,
 142/above, 320/below, center/below} 
  \draw[shift=(s.\anchor)] plot[mark=x] coordinates{(0,0)}
       node[\placement] {\scriptsize\texttt{(s.\anchor)}};
\begin{scope}[yshift=-4cm]
  \node[name=s,shape=swig vsplit, shape example]
  {\nodepart[red!20]{left}{$Y_1(a_1)$}%
 \nodepart[green!30]{right}{$y_1$}};
 \foreach\anchor/\placement in 
  {left center/below, right center/above, 
  left north/above left, left south/below left,
  right north/above right, right south/below right,
north west/above left, south west/below left,
 north east/above right, south east/below right} 
 \draw[shift=(s.\anchor)] plot[mark=x] coordinates{(0,0)}
       node[\placement] {\scriptsize\texttt{(s.\anchor)}};
 \end{scope}
 \end{tikzpicture}
\end{tkzexample}


\bibliographystyle{chicago}

\begin{thebibliography}{}

\bibitem[\protect\citeauthoryear{Richardson and Robins}{Richardson and
  Robins}{2013}]{swigs}
Richardson, T.S. and J.M.~Robins (2013).
\newblock Single world intervention graphs (SWIGs): A unification of
  counterfactual and graphical approaches to causality.
\newblock Working Paper Number 128. Available at
  \url{http://www.csss.washington.edu/Papers/wp128.pdf}.

\end{thebibliography}

%\bibliography{references}

\end{document}