# Drawing circuit diagrams (with logic gates) in LaTeX

I'm having some difficult drawing circuit diagrams with logic gates in LaTeX. I've tried with circuitikz and pst-circ.

\begin{pspicture}(-1,-1)(5,3)
\logicand[ninputs=2,invertinputa=true, invertinputb=true](0,0){Name}
\end{pspicture}


gives

Error: ! Undefined control sequence.

l.75 ...inputa=true, invertinputb=true](0,0){Name}

?


And

\begin{circuitikz} \draw
(0,2) node[and port] (myand1) {}
(0,0) node[and port] (myand2) {}
(2,1) node[xnor port] (myxnor) {};
(myand1.out) | (myxnor.in 1)
(myand2.out) | (myxnor.in 2)
\end{circuitikz}


produces no error, but also no lines appear between logic gates.

Please help me generate logic diagrams from LaTeX code. (I need AND, OR, NOT, NOR and NAND gates.)

-
You're never going to get pst-circ to work with PDFLaTeX. Run texdoc pstricks and read the section on PDFTeX workarounds. – kahen Oct 27 '11 at 2:02
Please include complete minimal working examples (MWE) (with preambles) so that the code can be simply copy&pasted to diagnose problems. – Caramdir Oct 27 '11 at 2:03
Run your example with xelatex instead of pdflatex – Herbert Oct 27 '11 at 8:56
How can I draw a negative-and gate? Thanks. – user15772 Jun 20 '12 at 0:40
@AlbertO This space is reserved strictly for answers however you can ask a new question with a minimum working example demonstrating your effort. It doesn't need to be directly showing possible solution but people can copy/paste your example and work on it without coming up a code from scratch. – percusse Jun 20 '12 at 0:47

For the circuitikz approach: You need to tell TikZ what to do with (myand1.out), (myxnor.in 1), and so on. At the moment, you just mention the nodes, but don't tell TikZ to actually connect them, because you ended the previous \draw command already with the ;. Also, to connect nodes with straight lines, you need to use --, not |.

Here's your example in a full MWE.

\documentclass{article}
\usepackage{circuitikz}
\begin{document}

\begin{circuitikz} \draw
(0,2) node[and port] (myand1) {}
(0,0) node[and port] (myand2) {}
(2,1) node[xnor port] (myxnor) {}
(myand1.out) -- (myxnor.in 1)
(myand2.out) -- (myxnor.in 2);
\end{circuitikz}

\end{document}


-
Thanks (voted up). How do I show the progression of variables? - So lets say I have an x,y and z, how would I show the progressing to an f(x,y,z), as it goes through the circuit? – A T Oct 28 '11 at 1:24
That should probably be asked as a separate question. – Jake Oct 28 '11 at 1:38
tex.stackexchange.com/questions/32939 – A T Oct 28 '11 at 3:28

I've never used circuitikz but shapes.gates.logic tikzlibrary. You have AND, NOT, NOR and all logic gates, place them as regular tikz nodes and draw connections. That's all. Here you have an example:

\documentclass[border=3mm]{standalone}

\usepackage{tikz}
\usetikzlibrary{arrows,shapes.gates.logic.US,shapes.gates.logic.IEC,calc}
\begin{document}
\thispagestyle{empty}
\tikzstyle{branch}=[fill,shape=circle,minimum size=3pt,inner sep=0pt]
\begin{tikzpicture}[label distance=2mm]

\node (x3) at (0,0) {$x_3$};
\node (x2) at (1,0) {$x_2$};
\node (x1) at (2,0) {$x_1$};
\node (x0) at (3,0) {$x_0$};

\node[not gate US, draw, rotate=-90] at ($(x2)+(0.5,-1)$) (Not2) {};
\node[not gate US, draw, rotate=-90] at ($(x1)+(0.5,-1)$) (Not1) {};
\node[not gate US, draw, rotate=-90] at ($(x0)+(0.5,-1)$) (Not0) {};

\node[or gate US, draw, logic gate inputs=nnn] at ($(x0)+(2,-2)$) (Or1) {};
\node[or gate US, draw, logic gate inputs=nnnn] at ($(Or1)+(0,-1)$) (Or2) {};
\node[or gate US, draw, logic gate inputs=nnn] at ($(Or2)+(0,-1)$) (Or3) {};
\node[xor gate US, draw, logic gate inputs=nn] at ($(Or3)+(0,-1)$) (Xor1) {};
\node[and gate US, draw, logic gate inputs=nn, anchor=input 1] at ($(Or3.output)+(1,0)$) (And1) {};
\node[nor gate US, draw, logic gate inputs=nn, anchor=input 1] at ($(Or2.output -| And1.output)+(1,0)$) (Nor1) {};
\node[and gate US, draw, logic gate inputs=nn, anchor=input 1] at ($(Or1.output -| Nor1.output)+(1,0)$) (And2) {};

\foreach \i in {2,1,0}
{
\path (x\i) -- coordinate (punt\i) (x\i |- Not\i.input);
\draw (punt\i) node[branch] {} -| (Not\i.input);
}
\draw (x3) |- (Or2.input 1);
\draw (x3 |- Or1.input 1) node[branch] {} -- (Or1.input 1);
\draw (x2) |- (Xor1.input 1);
\draw (x2 |- Or3.input 1) node[branch] {} -- (Or3.input 1);
\draw (Not2.output) |- (Or2.input 2);
\draw (x1) |- (Or3.input 2);
\draw (x1 |- Or1.input 2) node[branch] {} -- (Or1.input 2);
\draw (Not1.output) |- (Xor1.input 2);
\draw (Not1.output |- Or2.input 3) node[branch] {} -- (Or2.input 3);
\draw (x0) |- (Or2.input 4);
\draw (Not0.output) |- (Or3.input 3);
\draw (Not0.output |- Or1.input 3) node[branch] {} -- (Or1.input 3);
\draw (Or3.output) -- (And1.input 1);
\draw (Xor1.output) -- ([xshift=0.5cm]Xor1.output) |- (And1.input 2);
\draw (Or2.output) -- (Nor1.input 1);
\draw (And1.output) -- ([xshift=0.5cm]And1.output) |- (Nor1.input 2);
\draw (Or1.output) -- (And2.input 1);
\draw (Nor1.output) -- ([xshift=0.5cm]Nor1.output) |- (And2.input 2);
\draw (And2.output) -- ([xshift=0.5cm]And2.output) node[above] {$f_1$};

\end{tikzpicture}
\end{document}


-
That looks quite nice, and is the kind of thing I'm looking for. Is there anyway to make the code more concise? - I am typing up lectures in realtime, so need to keep up... – A T Oct 28 '11 at 1:25
@AT: I don't know how to do it more concise. I just can think in using some style like and2/.style={and gate US, draw, logic gate inputs=nn} and later use \node[and2], but in any case you need to place gates and draw their connections between outputs and particular inputs. matrix and, maybe, chain libraries can help a little bit. – Ignasi Oct 28 '11 at 7:09
Mmm... maybe I should try something in Python with graphviz and plasTeX... basically I want to just use variables and gates naturally. i.e.: x-[and1]-[or1]-f1, y-[and2]-[or1](-f1)|(-[and1]), z-[f1] – A T Oct 29 '11 at 9:45

I'm not entirely sure what you're after; here is an example taken directly from the documentation, which you can find by running

texdoc pst-circ


When using PSTricks if you use

latex myfile.tex
dvips myfile.dvi
ps2pdf myfile.ps


then the following file will work

\documentclass{article}

\usepackage{pst-circ}

\begin{document}

\begin{pspicture}(-4,0)(5,7)
\psset{logicWidth=1, logicHeight=2, dotsize=0.15}
\logic[logicWireLength=0](-2,0){A0}
\logic[logicWireLength=0](-2,5){A1}
\ncbar[angleA=-180,angleB=-180,arm=0.5]{A11}{A02}
\psline[dotsize=0.15]{-*}(-3.5,3.5)(-2.5,3.5)
\uput[180](-3.5,3.5){$T$}
\psline(-3.5,0.5)(A01)\uput[180](-3.5,0.5){$S$}
\psline(-3.5,6.5)(A12)\uput[180](-3.5,6.5){$R$}
\psset{logicType=nor, logicLabelstyle=\normalsize}
\logic(1,0.5){nor1}
\logic(1,4.5){nor2}
\psline(nor2Q)(4,0|nor2Q)
\uput[0](4,0|nor2Q){$Q$}
\psline(nor1Q)(4,0|nor1Q)
\uput[0](4,0|nor1Q){$\overline{Q}$}
\psline{*-}(3,0|nor2Q)(3,4)(1,4)(0,3)(0,0|nor12)(nor12)
\psline{*-}(3,0|nor1Q)(3,3)(1,3)(0,4)(0,0|nor21)(nor21)
\psline(A0Q)(nor11)
\psline(A1Q)(nor22)
\end{pspicture}

\end{document}


If you would prefer to use pdflatex then your preamble should look like

\documentclass{article}

\usepackage[pdf]{pstricks}
\usepackage{pst-circ}

\begin{document}
...


and then you can run

pdflatex -shell-escape myfile.tex


-