# TikZ logic alignment

I'm trying to create various logic circuit diagrams using the circuit library of TikZ (it allows multi-input gates).

I'm using a matrix, but this causes issues with the input wires not lining up with the input nodes (A and B). Also, the wires in general don't line up as nicely as they should do.

There must be a better way of doing this without using matrix, but I don't know what it would be.

Code:

\documentclass[tikz,border=2mm]{standalone}
\usetikzlibrary{circuits.logic.US}

\begin{document}
\begin{tikzpicture}[circuit logic US, huge circuit symbols]
\matrix[column sep=10mm]
{
\node (a) {A}; & \node [nand gate] (nand) {};        &                                        & \\
\node (b) {B}; &                                     &                                        & \\
\node (c) {C}; &                                     & \node [nor gate, inputs=nnn] (nor) {}; & \node [or gate, inputs=nnn] (out) {}; \\
& \node[not gate,rotate=90] (not) {}; &                                        & \\
\node (d) {D}; &                                     &                                        & \\
};
\draw (a.east) -- ++(right:5mm)       |- (nand.input 1);
\draw (b.east) -- ++(right:5mm)       |- (nand.input 2);
\draw (c.east) -- ++(right:5mm)       |- (nor.input 2);
\draw (d.east) -- ++(right:52mm)      |- (out.input 3);
\draw (d.east)                        -| (not.input);
\draw (not.output)                    |- (nor.input 3);
\draw (nand.output) -- ++(right:30mm) |- (out.input 1);
\draw (nand.output) -- ++(right:5mm)  |- (nor.input 1);
\draw (nor.output) -- ++(right:5mm)   |- (out.input 2);
\draw (out.output) -- ++(right:10mm);
\end{tikzpicture}
\end{document}

• Welcome to TeX.sx! – Peter Jansson Feb 27 '13 at 22:09
• Can you be more precise what you want to change? You may add a “paint”ed picture if you will. Do you rather want straight lines or even-spaced inputs (A, B, …)? For example, you could add anchor=input 1 as an option to the nand gate. There is also the logic gate input sep option where you can set the space between inputs (default: .125cm). – Qrrbrbirlbel Feb 27 '13 at 22:52
• Please, it would be better if you provide a complete MWE from \documentclass{} to \end{document}. This way is easier to test your code and propose solutions. – Ignasi Feb 28 '13 at 10:03
• Sorry for not providing the MWE form or picture, thanks for your comments. – SuborbitalPigeon Feb 28 '13 at 21:33

If you place input nodes inside the matrix they will be placed as any other cell, so your B node defines a new row (the second one), while nand gate is still on first row.

One possibility to avoid this problem is to use \matrix to place only the gates and after that place your input nodes where you want. To place the nodes you can use every input and output of your already defined circuit.

To obtain next figure

I've started placing node C with

\node (c) at ([xshift=-10mm]nand.west|-nor.input 2) {C};


and using it to place nodes A, B and D. In this case I placed them after "backdrawing" from circuit input to node place

 \draw (nand.input 2) -- (nand.input 2-|c.east) node[left] (b) {B};


Same happen with connections between output inputs 1 and 3 with nand output or not input.

The complete code is:

\documentclass[tikz,border=2mm]{standalone}
\usetikzlibrary{circuits.logic.US}

\begin{document}
\begin{tikzpicture}[circuit logic US, huge circuit symbols]
\matrix[column sep=10mm]
{
\node [nand gate] (nand) {}; &  & \\
& \node [nor gate, inputs=nnn] (nor) {}; &
\node [or gate, inputs=nnn] (out) {}; \\
\node[not gate,rotate=90] (not) {}; &  & \\
};

\node (c) at ([xshift=-10mm]nand.west|-nor.input 2) {C};
\draw (nor.input 2) -- (c);
\draw (nand.input 2) -- (nand.input 2-|c.east) node[left] (b) {B};
\draw (nand.input 1) -- (nand.input 1-|c.east) node[left] (a) {A};
\draw (not.input) --++(270:3mm) coordinate (Daux) --(Daux-|c.east) node[left] (d) {D};

\draw (not.output) |- (nor.input 3);
\draw (out.input 3) --++(180:5mm) |- (Daux);
\draw (out.input 1) --++(180:5mm) |- (nand.output);
\draw (nor.input 1) --++(180:5mm) |- (nand.output);
\draw (nor.output) -- ++(right:5mm)   |- (out.input 2);
\draw (out.output) -- ++(right:10mm);
\end{tikzpicture}
\end{document}

• That's great, now I have good looking logic circuits. Now I just have to write the rest of the report... – SuborbitalPigeon Mar 1 '13 at 12:06