Diagramming in TeX
<tex> \documentclass[11pt,oneside,article]{memoir}
\usepackage{amsthm} %\usepackage{xltxtra} %\usepackage{newpxtext} %\usepackage[varg,bigdelims]{newpxmath} \usepackage{amsmath} \usepackage{amssymb} \usepackage[usenames,dvipsnames]{xcolor} \usepackage{tikz} \usetikzlibrary{
cd,
math, decorations.markings, decorations.pathreplacing, positioning, arrows.meta, circuits.logic.US, shapes, calc, fit,
quotes}
\newcommand{\tn}{\textnormal} \newcommand{\inp}[1]{#1^{\tn{in}}} \newcommand{\outp}[1]{#1^{\tn{out}}} \newcommand{\upd}[1]{#1^{\tn{upd}}} \newcommand{\rdt}[1]{#1^{\tn{rdt}}}
\tikzset{
oriented WD/.style={%everything after equals replaces "oriented WD" in key. every to/.style={out=0,in=180,draw}, label/.style={ font=\everymath\expandafter{\the\everymath\scriptstyle}, inner sep=0pt, node distance=2pt and -2pt}, semithick, node distance=1 and 1, decoration={markings, mark=at position \stringdecpos with \stringdec}, ar/.style={postaction={decorate}}, execute at begin picture={\tikzset{ x=\bbx, y=\bby, every fit/.style={inner xsep=\bbx, inner ysep=\bby}}} }, string decoration/.store in=\stringdec, string decoration={\arrow{stealth};}, string decoration pos/.store in=\stringdecpos, string decoration pos=.7, bbx/.store in=\bbx, bbx = 1.5cm, bby/.store in=\bby, bby = 1.5ex, bb port sep/.store in=\bbportsep, bb port sep=1.5, % bb wire sep/.store in=\bbwiresep, % bb wire sep=1.75ex, bb port length/.store in=\bbportlen, bb port length=4pt, bb penetrate/.store in=\bbpenetrate, bb penetrate=0, bb min width/.store in=\bbminwidth, bb min width=1cm, bb rounded corners/.store in=\bbcorners, bb rounded corners=2pt, bb small/.style={bb port sep=1, bb port length=2.5pt, bbx=.4cm, bb min width=.4cm,
bby=.7ex}, bb medium/.style={bb port sep=1, bb port length=2.5pt, bbx=.4cm, bb min width=.4cm, bby=.9ex},
bb/.code 2 args={%When you see this key, run the code below: \pgfmathsetlengthmacro{\bbheight}{\bbportsep * (max(#1,#2)+1) * \bby} \pgfkeysalso{draw,minimum height=\bbheight,minimum width=\bbminwidth,outer
sep=0pt,
rounded corners=\bbcorners,thick, prefix after command={\pgfextra{\let\fixname\tikzlastnode}}, append after command={\pgfextra{\draw \ifnum #1=0{} \else foreach \i in {1,...,#1} { ($(\fixname.north west)!{\i/(#1+1)}!(\fixname.south west)$) +(-
\bbportlen,0)
coordinate (\fixname_in\i) -- +(\bbpenetrate,0) coordinate (\fixname_in\i')}\fi %Define the endpoints of tickmarks \ifnum #2=0{} \else foreach \i in {1,...,#2} { ($(\fixname.north east)!{\i/(#2+1)}!(\fixname.south east)$) +(-
\bbpenetrate,0)
coordinate (\fixname_out\i') -- +(\bbportlen,0) coordinate (\fixname_out\i)}\fi; }}} }, bb name/.style={append after command={\pgfextra{\node[anchor=north] at
(\fixname.north) {#1};}}} }
\tikzset{ unoriented WD/.style={ every to/.style={draw}, shorten <=-\penetration, shorten >=-\penetration, label distance=-2pt, thick, node distance=\spacing, execute at begin picture={\tikzset{ x=\spacing, y=\spacing}} }, pack size/.store in=\psize, pack size = 8pt, spacing/.store in=\spacing, spacing = 8pt, link size/.store in=\lsize, link size = 2pt,
penetration/.store in=\penetration, penetration = 2pt,
pack color/.store in=\pcolor, pack color = blue, pack inside color/.store in=\picolor, pack inside color=blue!20, pack outside color/.store in=\pocolor, pack outside color=blue!50!black, surround sep/.store in=\ssep, surround sep=8pt, link/.style={ circle, draw=black, fill=black, inner sep=0pt, minimum size=\lsize }, pack/.style={ circle, draw = \pocolor, fill = \picolor, inner sep = .25*\psize, minimum size = \psize }, outer pack/.style={ ellipse, draw, inner sep=\ssep, color=\pocolor, }, intermediate pack/.style={ ellipse, dashed, draw, inner sep=\ssep, color=\pocolor, }, }
\tikzset{Yonepart/.pic={ \node[bb={1}{2},bb name = {\tiny$X_{11}$}] (X11) {}; \node[bb={2}{2},below right=of X11,bb name = {\tiny$X_{12}$}] (X12) {}; \node[bb={2}{1}, above right=of X12,bb name = {\tiny$X_{13}$}] (X13) {}; \node[bb={2}{2}, fit={($(X11.north west)+(.3,1.5)$) (X12) ($(X13.east)+(-.3,0)$)},bb name = {\scriptsize $Y_1$}] (Y1) {}; \draw (Y1_in1') to (X11_in1); \draw (Y1_in2') to (X12_in2); \draw (X11_out1) to (X13_in1); \draw (X11_out2) to (X12_in1); \draw (X12_out1) to (X13_in2); \draw (X12_out2) to (Y1_out2'); \draw (X13_out1) to (Y1_out1'); \coordinate (bottombox) at ($(X12.south)$); \coordinate (rightbox) at ($(X13.east)$); \coordinate (Y1northwest) at ($(Y1.north west)$); } }
\tikzset{Ytwopart/.pic={ \node[bb={2}{2}, bb name = {\tiny$X_{21}$}] (X21) {}; \node[bb={1}{2},above right=-1 and 1 of X21,bb name = {\tiny$X_{22}$}] (X22) {}; \node[bb={1}{2}, fit={($(X21.south west)+(-.25,0)$) ($(X22.north east)+(.25,3.5)$)},bb name = {\scriptsize$Y_2$}] (Y2){}; \draw (Y2_in1') to (X21_in2); \draw (X21_out1) to (X22_in1); \draw (X22_out2) to (Y2_out1'); \draw let \p1=(X22.south east), \p2=($(Y2_out2)$), \n1={\y1-\bby}, \n2=\bbportlen in (X21_out2) to (\x1+\n2,\n1) -- (\x1+\n2,\n1) to (Y2_out2'); \draw let \p1=(X22.north east), \p2=(X21.north west), \n1={\y1+\bby}, \n2=\bbportlen in
(X22_out1) to[in=0] (\x1+\n2,\n1) -- (\x2-\n2,\n1) to[out=180] (X21_in1); }
}
\tikzset{SmallNeuronPic/.pic={
\node[bb={3}{1}] (N1) {$\scriptstyle N_1$}; \node[bb={2}{1}, above right=.7 and 3.5 of N1] (N2) {$\scriptstyle N_2$}; \node[bb={2}{1}, below =of N2] (N3) {$\scriptstyle N_3$}; \node[bb={3}{1}, below =of N3] (N4) {$\scriptstyle N_4$}; \node[bb={6}{8}, fit={($(N1.west)-(.5,0)$) ($(N2.north)+(0,2)$) ($(N3.east)+(1.5,0)$) ($(N4.south)-(0,1)$)}, bb name={$\scriptstyle X$}] (X) {}; \draw (X_in1') to (N2_in1); \draw (X_in2') to (N1_in1); \draw (X_in3') to (N1_in2); \draw (X_in4') to (N1_in3); \draw (X_in6') to (N4_in2); \draw (N1_out1) to (N2_in2); \draw (N1_out1) to (N3_in1); \draw (N1_out1) to (N4_in1); \draw (N2_out1) to (X_out1'); \draw (N2_out1) to (X_out2'); \draw (N2_out1) to (X_out3'); \draw (N3_out1) to (X_out4'); \draw (N3_out1) to (X_out5'); \draw (N3_out1) to (X_out6'); \draw (N4_out1) to (X_out7'); \draw (N4_out1) to (X_out8'); \draw (X_in5') to[looseness=2] (N3_in2); \draw let \p1=(N4.south east), \p2=(N4.south west), \n1={\y2-\bby}, \n2=\bbportlen in (N3_out1) to[in=0] (\x1+\n2,\n1) -- (\x2-\n2,\n1) to[out=180] (N4_in3);
} }
\tikzset{SmallNeuronDashed/.pic={
\node[bb={3}{1}] (N1) {$\scriptstyle N_1$}; \node[bb={2}{1}, above right=.7 and 3.5 of N1] (N2) {$\scriptstyle N_2$}; \node[bb={2}{1}, below =of N2] (N3) {$\scriptstyle N_3$}; \node[bb={3}{1}, below =of N3] (N4) {$\scriptstyle N_4$}; \node[bb={6}{8}, fit={($(N1.west)-(.5,0)$) ($(N2.north)+(0,2)$) ($(N3.east)+(1.5,0)$) ($(N4.south)-(0,1)$)}, bb name={$\scriptstyle X$}] (X) {}; \draw[dashed] (X_in1') to (N2_in1); \draw[dashed] (X_in2') to (N1_in1); \draw[dashed] (X_in3') to (N1_in2); \draw[dashed] (X_in4') to (N1_in3); \draw[dashed] (X_in6') to (N4_in2); \draw[dashed] (N1_out1) to (N2_in2); \draw[dashed] (N1_out1) to (N3_in1); \draw[dashed] (N1_out1) to (N4_in1); \draw[dashed] (N2_out1) to (X_out1'); \draw[dashed] (N2_out1) to (X_out2'); \draw[dashed] (N2_out1) to (X_out3'); \draw[dashed] (N3_out1) to (X_out4'); \draw[dashed] (N3_out1) to (X_out5'); \draw[dashed] (N3_out1) to (X_out6'); \draw[dashed] (N4_out1) to (X_out7'); \draw[dashed] (N4_out1) to (X_out8'); \draw[dashed] (X_in5') to[looseness=2] (N3_in2); \draw[dashed] let \p1=(N4.south east), \p2=(N4.south west), \n1={\y2-\bby}, \n2=\bbportlen in (N3_out1) to[in=0] (\x1+\n2,\n1) -- (\x2-\n2,\n1) to[out=180] (N4_in3);
} }
\tikzset{SmallNestingPic/.pic={ \path (0,0) pic [purple] {Yonepart}; \path ($(rightbox)+(5,-5)$) pic [orange] {Ytwopart};
\node[bb={1}{2}, fit={($(Y1northwest)+(-.5,4)$) ($(Y2.south east)+(1,0)$)}, bb name={\small $Z$}] (Z) {}; \draw (Z_in1') to (Y1_in2); \draw let \p1=(Y2.north west),\p2=(Y2.north east),\n1={\y2+\bby},\n2=\bbportlen in
(Y1_out1) to (\x1+\n2,\n1)--(\x2+\n2,\n1) to (Z_out1');
\draw (Y1_out2) to (Y2_in1); \draw (Y2_out2) to (Z_out2'); \draw let \p1=(Y2.north east), \p2=(Y1.north west), \n1={\y2+\bby}, \n2=\bbportlen in
(Y2_out1) to[in=0] (\x1+\n2,\n1) -- (\x2-\n2,\n1) to[out=180] (Y1_in1); }
}
\tikzset{Zredgreen/.pic={ \node[bb={2}{2}, green!50!black, bb name = $\scriptstyle Y_1$] (YY1) {}; \node[bb={1}{2}, red, below right=-1 and 2 of YY1, bb name=$\scriptstyle Y_2$] (YY2) {}; \node[bb={1}{2}, fit={($(YY1.north west)+(-.5,4)$) ($(YY2.south east)+(.5,-2)$)}, bb name={\scriptsize $Z$}] (Z) {}; \draw (Z_in1') to (YY1_in2); \draw (YY1_out1) to (Z_out1'); \draw (YY1_out2) to (YY2_in1); \draw (YY2_out2) to (Z_out2'); \draw let \p1=(YY2.north east), \p2=(YY1.north west), \n1={\y2+\bby}, \n2=\bbportlen in
(YY2_out1) to[in=0] (\x1+\n2,\n1) -- (\x2-\n2,\n1) to[out=180] (YY1_in1);
} }
\tikzset{Zcombined/.pic={ \node[bb={1}{2},green!25!black,bb name = {\tiny$X_{11}$}] (X11) {}; \node[bb={2}{2},green!25!black,below right=of X11,bb name = {\tiny$X_{12}$}] (X12) {}; \node[bb={2}{1}, green!25!black,above right=of X12,bb name = {\tiny$X_{13}$}] (X13) {}; \draw (X11_out1) to (X13_in1); \draw (X11_out2) to (X12_in1); \draw (X12_out1) to (X13_in2);
\node[bb={2}{2}, red!30!black, below right = 0 and 1.25 of X12, bb name = {\tiny$X_{21}$}] (X21) {}; \node[bb={1}{2}, red!30!black, above right=-1 and 1 of X21,bb name = {\tiny$X_{22}$}] (X22) {}; \draw (X21_out1) to (X22_in1); \draw let \p1=(X22.north east), \p2=(X21.north west), \n1={\y1+\bby}, \n2=\bbportlen in
(X22_out1) to[in=0] (\x1+\n2,\n1) -- (\x2-\n2,\n1) to[out=180] (X21_in1); \node[bb={1}{2}, fit = {($(X11.north east)+(-1,3)$) (X12) (X13) ($(X21.south)+(0,-1)$) ($(X22.east)+(.5,0)$)}, bb name ={\scriptsize $Z$}] (Z) {};
\draw (Z_in1') to (X12_in2); \draw (X13_out1) to (Z_out1'); \draw (X12_out2) to (X21_in2); \draw let \p1=(X22.south east),\n1={\y1-\bby}, \n2=\bbportlen in (X21_out2) to (\x1+\n2,\n1) to (Z_out2'); \draw let \p1=(X22.north east), \p2=(X11.north west), \n1={\y2+\bby}, \n2=\bbportlen in
(X22_out2) to[in=0] (\x1+\n2,\n1) -- (\x2-\n2,\n1) to[out=180] (X11_in1);
} }
%\setmainfont{Tex Gyre Pagella} \settrims{0pt}{0pt}
\settypeblocksize{*}{35pc}{*} \setlrmargins{*}{*}{1} \setulmarginsandblock{1in}{1in}{*} \setheadfoot{\onelineskip}{2\onelineskip} \setheaderspaces{*}{1.5\onelineskip}{*} \checkandfixthelayout
\setcounter{tocdepth}{1} \setcounter{secnumdepth}{1} \pagestyle{ruled} \renewcommand*{\chaptitlefont}{\bfseries\Large} \setsecheadstyle{\bfseries\large\raggedright} \setsubsecheadstyle{\bfseries\raggedright}
\begin{document}
\title{Collected Wiring diagrams} \author{David I. Spivak}
\maketitle
%%%% Chapter %%%% \chapter{Boxes}
\begin{equation}\label{box1}\tag{box1} \begin{tikzpicture}[oriented WD, bbx = 1cm, bby =.5cm, bb min width=1cm, bb port length=4pt, bb port sep=1] \node[bb={1}{1}] (X) {$M$}; \draw[label] node [left=2pt of X_in1] {$A$} node [right=2pt of X_out1] {$B$} ; \end{tikzpicture} \end{equation}
\begin{equation}\label{box2}\tag{box2} \begin{tikzpicture}[oriented WD, bb small, bb port sep=2.2, baseline = (lone.center)] \node[bb={3}{1}, inner sep=10pt] (lone) {$\dot{u}$}; \node[above right=-3 and 1.5 of lone] (a) {}; \node[below right=-3 and 1.5 of lone] (b) {}; \draw (lone_out1) to (a); \draw (lone_out1) to (b); \draw [label] \foreach \i in {1,2,3} { node[anchor=east] at (lone_in\i) {\small $a_\i$} node[above right=1 and 0 of lone_out1] {\small $u$} } ; \end{tikzpicture} \end{equation}
\begin{equation}\label{box3}\tag{box3} \begin{tikzpicture}[oriented WD, bbx = .25cm, bby =.1cm, bb min width=.25cm, bb port length=2pt, bb port sep=1] \node[bb={1}{1}] (X) {}; \draw[label] node [left=2pt of X_in1] {$\inp{X}$} node [right=2pt of X_out1] {$\outp{X}$} ; \end{tikzpicture} \end{equation}
\begin{equation}\label{box32}\tag{box32} \begin{tikzpicture}[oriented WD, bbx = 1cm, bby =.3cm, bb min width=1cm, bb port length=4pt, bb port sep=1] \node[bb={2}{3}] (X) {}; \draw[label] node [left=2pt of X_in1] {$A$} node [left=2pt of X_in2] {$B$} node [right=2pt of X_out1] {$C$} node [right=2pt of X_out2] {$D$} node [right=2pt of X_out3] {$E$} ; \end{tikzpicture} \end{equation}
\begin{equation}\label{lots}\tag{lots} \begin{tikzpicture}[oriented WD, bb small] \node[bb={0}{0}] (00) {}; \node[bb={0}{1}, right=2 of 00] (01) {}; \node[bb={0}{2}, right=2 of 01] (02) {}; \node[bb={1}{0}, right=2 of 02] (10) {}; \node[bb={1}{1}, right=2 of 10] (11) {}; \node[bb={1}{2}, right=2 of 11] (12) {}; \node[bb={2}{0}, right=2 of 12] (20) {}; \node[bb={2}{1}, right=2 of 20] (21) {}; \node[bb={2}{2}, right=2 of 21] (22) {}; \node[ right=2 of 22] (dt) {$\cdots$}; \node[bb={3}{5}, right=2 of dt] (35) {}; \node[ right=2 of 35] (et) {etc.}; \end{tikzpicture} \end{equation}
%%%% Chapter %%%% \chapter{Simple Wiring Diagrams}
\begin{equation}\label{simple3}\tag{simple 3} \begin{tikzpicture}[oriented WD, bbx = .3cm, bby =.3cm, bb min width=.5cm, bb port length=2pt, bb port sep=1] \node[bb={1}{1}] (X1) {\tiny$\sigma_1$};
\node[bb={1}{1}, right=of X1] (X2) {\tiny$\sigma_2$};
\node[bb={1}{1}, right=of X2] (X3) {\tiny$\sigma_3$}; \node[bb={1}{1}, fit=(X1) (X2) (X3)] (Y) {}; \draw (Y_in1') to (X1_in1); \draw (X1_out1) to (X2_in1); \draw (X2_out1) to (X3_in1); \draw (X3_out1) to (Y_out1'); \end{tikzpicture} \end{equation}
\begin{equation}\label{no_exterior}\tag{no exterior} \begin{tikzpicture}[oriented WD, bbx=1em, bby=1ex]
\node[bb={0}{1},bb name=$X_1$] (X1) {}; \node[bb={1}{0},right =2 of X1, bb name=$X_2$] (X2) {}; \draw[ar] (X1_out1) to (X2_in1);
\end{tikzpicture} \end{equation}
\begin{equation}\label{simple2}\tag{simple 2} \begin{tikzpicture}[oriented WD, bbx=1em, bby=1ex]
\node[bb={1}{1},bb name=$X_1$] (X1) {}; \node[bb={1}{1},right =2 of X1, bb name=$X_2$] (X2) {}; \node[bb={1}{1}, fit={($(X1.north west)+(-1,3)$) ($(X1.south)+(0,-3)$) ($(X2.east)+(1,0)$)}, bb name = $Y$] (Y) {};
%
\draw[ar] (Y_in1') to (X1_in1); \draw[ar] (X1_out1) to (X2_in1); \draw[ar] (X2_out1) to (Y_out1'); \draw[label]
node at ($(Y_in1')!.5!(X1_in1)+(0,7pt)$) {$A$} node at ($(X1_out1)!.5!(X2_in1)+(0,7pt)$) {$B$} node at ($(X2_out1)!.5!(Y_out1')+(0,7pt)$) {$C$}; \end{tikzpicture} \end{equation}
\begin{equation}\label{no_passing_wires}\tag{no passing wires} \begin{tikzpicture}[oriented WD, bb small] \node[bb={1}{1}] (x) {}; \node[bb={2}{2}, fit={(x.north west) ($(x.south east)+(0,-2)$)}] (y) {}; \draw (y_in1') to (x_in1); \draw (x_out1) to (y_out1'); \draw (y_in2') to (y_out2'); \draw[red, thin] ($(y.north west)+(-1,1)$) -- ($(y.south east)+(1,-1)$); \draw[red, thin] ($(y.north east)+(1,1)$) -- ($(y.south west)+(-1,-1)$); \end{tikzpicture} \end{equation}
\begin{equation}\label{digging1}\tag{digging1} \begin{tikzpicture}[oriented WD] \node[bb={1}{0}] (shovel) {$\;shovel\;$}; \node[bb={1}{0}, below=2 of shovel] (person) {$\;person\;$}; \node[bb={1}{2}, left=4 of $(shovel)!.5!(person)$] (dig) {};
\draw[label] node [left=2pt of dig_in1] {digging} node [right=2pt of dig_out1] {scooping} node [right=2pt of dig_out2] {levering} node [left=2pt of shovel_in1] {scooping} node [left=2pt of person_in1] {levering} ; \end{tikzpicture} \end{equation}
\begin{equation}\label{digging2}\tag{digging2} \begin{tikzpicture}[oriented WD,bb small, font=\tiny] \node[bb={1}{0}] (shovel) {S}; \node[bb={1}{0}, below=2 of shovel] (person) {P}; \node[bb={1}{2}, left=3 of $(shovel)!.5!(person)$] (dig) {D}; \node[bb={1}{0}, fit=(dig) (shovel) (person)] (outer) {}; \draw (outer_in1') to (dig_in1); \draw (dig_out1) to (shovel_in1); \draw (dig_out2) to (person_in1); \end{tikzpicture} \end{equation}
%%%% Chapter %%%% \chapter{Oriented Wiring Diagrams}
% Section % \section{No Tracing}
\subsection{No splitting} \begin{equation}\label{parallel}\tag{parallel} \begin{tikzpicture}[oriented WD, bbx=1em, bby=1ex]
\node[bb={1}{1},bb name=$X_1$] (X1) {}; \node[bb={1}{1},below =2 of X1, bb name=$X_2$] (X2) {}; \node[bb={2}{2}, fit={($(X1.north west)+(-1,3)$) ($(X2.south)+(0,-3)$) ($(X2.east)+(1,0)$)}, bb name = $Y$] (Y) {};
%
\draw[ar] (Y_in1') to (X1_in1); \draw[ar] (Y_in2') to (X2_in1); \draw[ar] (X1_out1) to (Y_out1'); \draw[ar] (X2_out1) to (Y_out2'); \draw[label]
node at ($(Y_in1')!.5!(X1_in1)+(0,7pt)$) {$A_1$} node at ($(Y_in2')!.5!(X2_in1)+(0,7pt)$) {$B_1$} node at ($(X1_out1)!.5!(Y_out1')+(0,7pt)$) {$A_2$} node at ($(X2_out1)!.5!(Y_out2')+(0,7pt)$) {$B_2$}; \end{tikzpicture} \end{equation}
\end{document} </tex>