# Drawing a circuit structure in LaTeX

I want to create the following structure in LaTeX:

Unfortunately I couldn't find a similar example in the pgf manual using the same symbols. An answer just showing basically how to produce a structure like the image shows, meaning a bit of code from which I can deduce the rest, would be fine.

EDIT: After taking a look at the circuitikz documentation, I came to the following result:

\documentclass{article}
\usepackage{circuitikz}

\begin{document}

\begin{tikzpicture}
\node [ocirc, label=left:1]       (1) at (0,3) {};
\node [ocirc, label=left:0]       (0) at (0,2) {};
\node [ocirc, label=left:{$x_1$}] (x) at (0,1) {};
\node [ocirc, label=left:{$\overline{x_1}$}] (nx) at (0,0) {};
\node [ocirc, label=right:{$y$}] (y) at (5,1.5) {};

\node [circ] (c1) at (4,2) {};
\node [circ] (c2) at (4,1) {};
\node [circ] (c3) at (4,1.5) {};

\draw (c3) to (y);
\draw (1) to[cspst] (4,3);
\draw (0) to[cspst] (c1);
\draw (x) to[cspst] (c2);
\draw (nx)to[cspst] (4,0);
\draw (4,3) to (4,0);
\end{tikzpicture}

\end{document}


Resulting in:

Now my more precise questions are:

• How can I draw lines with multiple cspst at a specific place?
• How to avoid the line begining in the ocirc?
• Is there a simpler way without using coordinates?
-
When working with circuitikz you ought to look into the specific manual which is the circuitikz manual (since circuitikz is a distinguished package). The above elements are called closing switches cspst which are defined in circuitikz. The circuitikz manual can be accessed by texdoc –  bloodworks Jan 8 at 16:53
@bloodworks Thanks for the hint using the circuitikz documentation, I made an edit on my post including an MWE and more specific questions. –  xfoo Jan 8 at 17:54
+1 this question went from draw-it-for-me to help-me-tweak, very nice! –  cmhughes Jan 8 at 18:34

1. Is there a simpler way without using coordinates?

Yes. You can use positioning tikz library which allows you to specify the placement of a new node or coordinate relative to the position to another node, with a simple syntax: \node[below=1cm of 0], for example, asuming that (0) is the name of a known node. In fact, the use of absolute coordinates can be avoided for the the whole figure, as the example at the end shows.

In adition, you can use intersection coordinate system, which allows to define the placement of a new coordinate as the intersection of a horizontal and a vertical line passing through other two nodes. For example (node1-|node2) is the coordinate at the intersection of a horizontal line passing through node1 and a vertical one pasing through node2. You can use this trick to specify the positions of your "black circles".

Finally, it is also very convenient the use of interpolated coordinates as a way to specify a point which lays in a line bewteen any other two given nodes. The syntax is ($(node1)!<factor>!(node2)$), being factor a real number between 0 and 1. This syntax requires usually \usetikzlibrary{calc}, but apparently circuitikz already includes it.

2. How to avoid the line begining in the ocirc?

I assume you mean how to avoid the line intersecting the circle. I would avoid completly the use of the node shape ocirc, using instead a "shapeless" node, and the connection style o-. You can also use the connection style -* to get the black circles.

3. How can I draw lines with multiple cspst at a specific place?

I would "break" the path into several sub-paths, inserting cspst in those sub-paths. Interpolating coordinates (as explained above) for the intermediate points looks the most convenient solution.

The following MWE shows all these ideas in action:

\documentclass{article}
\usepackage{circuitikz}
\usetikzlibrary{positioning}

\begin{document}
\begin{tikzpicture}
\node [label=left:1]                       (1) {};
\node [label=left:0, below=1cm of 1]       (0) {};
\node [label=left:{$x_1$}, below=1cm of 0] (x) {};
\node [label=left:{$\overline{x_1}$}, below=1cm of x]
(nx) {};

% Auxiliary coordinate: the point where all branches join
\coordinate[above right=5mm and 5cm of x] (right side);
\node [label=right:{$y$}, right=1cm of right side]
(y) {};

% Auxiliary coordinates, right end of each branch
\coordinate (1r)  at (1  -|right side);
\coordinate (0r)  at (0  -|right side);
\coordinate (xr)  at (x  -|right side);
\coordinate (nxr) at (nx -|right side);

\draw (1) to[cspst, o-]  ($(1)!.3!(1r)$) to[cspst]     (1r);
\draw (0) to[cspst, o-]  ($(0)!.7!(0r)$) to[cspst, -*] (0r);
\draw (y) to[short, o-*] (y-|right side);
\draw (x) to[cspst, o-]  ($(x)!.3!(xr)$)
to[short]      ($(x)!.7!(xr)$) to[cspst,-*] (xr);
\draw (nx)to[short,o-]   ($(nx)!.3!(nxr)$)
to[cspst]      ($(nx)!.5!(nxr)$)
to[cspst]      ($(nx)!.7!(nxr)$) to [short] (nxr);
\draw (1r) -- (nxr);
\end{tikzpicture}
\end{document}


-

The circuitkiz manual show that you can draw the entire graphic in one single \draw command which looking at the example seems failry simple but positioning can be a bit of an issue.

Using the [short] nodes you can pretty much position the elements where you want time (after a bit of trial and error)

I eventually got this code out:

\documentclass[tikz,preview]{standalone}

\usepackage{circuitikz}
\begin{document}

\begin{circuitikz}
\draw (0,0) node[anchor=east] {1} to [cspst=$$X_2$$, o-] (2,0) -- (3,0) to [cspst=$$X_3$$] (4,0) -- (6,0)
(0,-1) node[anchor=east] {0} to [short,o-] (1,-1) to  [cspst=$$\overline{X_2}$$] (3,-1) to [cspst=$$\overline{X_3}$$] (6,-1) -- (6,-1)
(0,-2) node[anchor=east] {$$X_1$$} to [cspst=$$X_2$$, o-] (2,-2) -- (4,-2) to [cspst=$$\overline{X_3}$$] (5,-2) -- (6,-2)
(0,-3) node[anchor=east] {$$\overline{X_1}$$} to [short,o-] (1,-3) to [cspst=$$\overline{X_2}$$] (3,-3) to [cspst=$$X_3$$] (4.1,-3) -- (6,-3)
(6,0) to [short] (6,-1) to [short,*-*] (6,-2) to [short] (6,-3)
(6,-1.5) to [short,*-o] (7,-1.5) node[anchor=west] {Y};

\end{circuitikz}

\end{document}


-

In my eyes, a drawing like that looks like a good job for a matrix.

And honestly, the positioning with the circuitikz package seems complicated as the other answers prove …

The advantage of a TikZ matrix and its library is that all nodes in it are named automatically: (<matrix name>-<row>-<column>).
We can use these names later to connect the individual nodes in the matrix.

As labels are nodes, too, the matrix of nodes style (and the automatic naming, see above) applies to the labels, too. This is avoided by giving all labels another name (even though we don’t reference them in this example).

## Code

\documentclass[tikz,convert=false]{standalone}
\usepackage{circuitikz,etoolbox}
\usetikzlibrary{matrix,calc}
\tikzset{
switch with/.style 2 args={
cspstshape,
label={$#1{x}_{#2}$}
},
yes/.style={switch with={}{#1}},
no/.style={switch with={\bar}{#1}},
}
\begin{document}

\begin{circuitikz}
\matrix[
name=c,
nodes={anchor=center},
matrix of nodes,
row sep={1cm,between origins},
column sep={1cm,between origins},
every label/.append style={
name=\tikzmatrixname-\the\pgfmatrixcurrentrow-\the\pgfmatrixcurrentcolumn-label}
] {
|[ocirc,label=left:$1$       ]| & |[yes=2]| &          & |[yes=3]| &                          \\
|[ocirc,label=left:$0$       ]| &           & |[no=2]| &           & |[no=3]| & |[circ]| & {} \\
|[ocirc,label=left:$x_1$     ]| & |[yes=2]| &          &           & |[no=3]| & |[circ]| & {} \\
|[ocirc,label=left:$\bar x_1$]| &           & |[no=2]| & |[yes=3]| &                          \\
};

\node [circ]                  (c-middle) at ($(c-2-6)!.5!(c-3-6)$) {};
\node [ocirc,label=right:$y$] (y-out)    at ($(c-2-7)!.5!(c-3-7)$) {};

\draw (c-1-1) -- (c-1-2) -- (c-1-4) -| (c-2-6);
\draw (c-2-1) -- (c-2-3) -- (c-2-5) -- (c-2-6) -- (c-middle) -- (c-3-6);
\draw (c-3-1) -- (c-3-2) -- (c-3-5) -- (c-3-6);
\draw (c-4-1) -- (c-4-3) -- (c-4-4) -| (c-3-6);
\draw (c-middle) -- (y-out);
\end{circuitikz}
\end{document}


-