# How to draw a boolean circuit diagram in circuitikz?

How would I draw this image:

using circuitikz?

What I have so far is

\begin{circuitikz} \draw
(0,2) node[and port] (myand) {}
(2,1) node[or port] (myor) {}
(myand.in 1) node[anchor=east] {A}
(myand.in 2) node[anchor=east] {B}
(myand.out) -| (myor.in 1)
;\end{circuitikz}


However I'm having trouble attaching the C while maintaining the alignment of A, B, and C.

-
As a side note, I would suggest using circuits.logic.US library of TikZ instead. Its gates are much more beautiful. – Ali Mehrizi May 10 '12 at 7:09

Hope the code itself is explanatory:

\documentclass{standalone}
\usepackage{circuitikz}
%
\begin{document}
\begin{circuitikz} \draw
(0,2) node[and port] (myand) {}
(2,1) node[or port] (myor) {}
(myand.in 1) node[above left=.5cm](a) {A}
(myand.in 2) node[below left = .5cm](b) {B}
(myand.out) -| (myor.in 1)
(a) -| (myand.in 1)
(b) -| (myand.in 2)
(b) node[below=1cm](c){C}
(c) -| (myor.in 2)
(c) node[draw, rectangle,inner sep=1pt,below=.5cm, right=.9cm](c){A$\cdot$B$+$C};
\end{circuitikz}
%
\end{document}


-
That inner sep on the box at the bottom seems a little small. Otherwise, very nice! +1 – qubyte May 10 '12 at 7:37
@MarkS.Everitt: Thanks, That is only personal preference. – Harish Kumar May 10 '12 at 7:41
Of course. I couldn't find anything actually wrong with it, and couldn't just say "very nice! +1" (not enough characters). ;) – qubyte May 10 '12 at 7:43
@MarkS.Everitt: Thanks a lot for your kind words. – Harish Kumar May 10 '12 at 7:45

This is another solution using the positioning library from tikz.

The main idea is to name the reference node, in this case the B node, and then just position C below that.

\documentclass{article}

\usepackage{circuitikz}
\usepackage{tikz}
\usetikzlibrary{positioning}

\begin{document}

\begin{circuitikz} \draw
(0,2) node[and port] (myand) {}
(2,1) node[or port] (myor) {}
(myand.in 1) node[anchor=east] {A}
(myand.in 2) node[anchor=east] (bnode) {B}
(myand.out) -| (myor.in 1)
node[below=of bnode] (cnode) {C}
(cnode) -| (myor.in 2)
;\end{circuitikz}

\end{document}


-

Here's how you can do this using \matrix.

\documentclass{standalone}

\usepackage{tikz}
\usetikzlibrary{circuits.logic.US}
\usetikzlibrary{positioning}

\begin{document}

\begin{tikzpicture}[circuit logic US,
line width=0.8pt,line cap=round,line join=round]

\matrix[column sep=7mm]
{
\node (A) {$A$}; &                                  & & \\
& \node [and gate] (and1) {};      & & \\
\node (B) {$B$}; &                                  & \node [or gate,yshift=-1mm] (or1) {}; & \node[yshift=-1mm] (out) {};\\
\node (C) {$C$}; &                                  & & \\
};

\draw
% AND gate inputs
(A) -- ++(right:5mm) |- (and1.input 1)
(B) -- ++(right:5mm) |- (and1.input 2)
% OR gate inputs
(or1.input 1) -- ++(left:3mm) |- (and1.output)
(or1.input 2) -- ++(left:3mm) |- (C)
% Output
(or1.output) -- (out);
\end{tikzpicture}

\end{document}


## Result:

PS. If you want, you can use circuitikz's logic gates too; just replace and gate with and port and similarly for the OR gate. (But somehow circuitikz's gates are not as well-designed as those of TikZ itself, although it's other electrical elements are.)

-

Another solution with TiKZ circuits library, but without matrix.

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

\begin{document}
\begin{tikzpicture}[circuit logic US,
tiny circuit symbols,
every circuit symbol/.style={fill=white,draw, logic gate input sep=4mm}
]

\node [and gate, inputs = nn] at (0,0) (and1) {};
\node [or gate, inputs = nn, anchor=input 1] at ($(and1.south)+(1.5cm,0)$) (or1) {};
%
\draw (and1.input 1) -- ++(left:5mm) node[left] (A) {$A$};
\draw (and1.input 2) -- ++(left:5mm) node[left] {$B$};
\node (C) at (A|-or1.input 2) {$C$};
\draw (or1.input 2) -- (C);
\draw (and1.output) -- ++(right:3mm) |- (or1.input 1);
\draw (or1.output) -- ++(right:5mm);
\end{tikzpicture}
\end{document}


-