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

\logicand[ninputs=2,invertinputa=true, invertinputb=true](0,0){Name}


Error: ! Undefined control sequence.
<recently read> \c@lor@to@ps 

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



\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)

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, 2011 at 2:02
  • 2
    Please include complete minimal working examples (MWE) (with preambles) so that the code can be simply copy&pasted to diagnose problems.
    – Caramdir
    Oct 27, 2011 at 2:03
  • Run your example with xelatex instead of pdflatex
    – user2478
    Oct 27, 2011 at 8:56
  • How can I draw a negative-and gate? Thanks.
    – user15772
    Jun 20, 2012 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, 2012 at 0:47

4 Answers 4


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.


\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);


  • 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, 2011 at 1:24
  • 2
    That should probably be asked as a separate question.
    – Jake
    Oct 28, 2011 at 1:38
  • tex.stackexchange.com/questions/32939
    – A T
    Oct 28, 2011 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:


\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$};


enter image description here

  • 1
    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, 2011 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, 2011 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, 2011 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




\psset{logicWidth=1, logicHeight=2, dotsize=0.15}
\psset{logicType=nor, logicLabelstyle=\normalsize}


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




and then you can run

pdflatex -shell-escape myfile.tex

enter image description here

  • 3
    looks horrible, why square ...
    – alicef
    Jul 6, 2016 at 6:26

Try this generator will work for simple cases and it uses Tikz

for your case latex code will be like this:

\usetikzlibrary{shapes.geometric, arrows}


\usetikzlibrary {circuits.logic.US}
\begin{tikzpicture}[circuit logic US]
\matrix[column sep=4mm, row sep=4mm] {
\node [and gate ] (f5fbb9ac-ba33-4ab5-96bc-d0af34cee8a1) {}; & & \\ 
& & \node [xnor gate ] (a6b7442d-0f66-4798-9db4-25a81365166e) {}; \\ 
\node [and gate ] (ca150480-f272-4220-9df1-14d5893b1ae2) {}; & & \\ 

\draw (f5fbb9ac-ba33-4ab5-96bc-d0af34cee8a1.output) -- ++(right:2mm) |- (a6b7442d-0f66-4798-9db4-25a81365166e.input 1);
\draw (ca150480-f272-4220-9df1-14d5893b1ae2.output) -- ++(right:2mm) |- (a6b7442d-0f66-4798-9db4-25a81365166e.input 2);


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .