[Tex/LaTex] Boolean AND Gate

circuitikz

I cannot figure out how I can to draw and label a single AND gate. All of the online examples are horizontally aligned. I drew the image. Please anyone show me how do this in LaTeX.

Edited to clarify: Also, I want to label the wires in 3 places each as I show in the image.

enter image description here

Best Answer

Following Mythio's suggestion, you can use "pure" TikZ code to locate the labels.

Here is how it works:

  • draw the AND port and remember to give it a name because in such a way later on you can access the inputs and output pins; for example:
    \draw (0,0) node[rotate=90,and port] () {};

enter image description here

  • the blue circle represents the exact coordinates of the inputs and output pin: this means that to locate the labels a,b,c you need to shift a bit that positions:
    % labels a, b, c
    \node[right=0.01cm,yshift=4pt] at (andp.in 1) {a};
    \node[right=0.01cm,yshift=4pt] at (andp.in 2) {b};
    \node[right=0.01cm,yshift=-4pt] at (andp.out) {c};
    
  • always starting from that coordinates, the last step is to extend the wires in order to have space to locate the last labels representing the bits entering/exiting the port:

    % other labels
    \draw (andp.in 1) --++(0,-0.5) node[pos=0.65]{y0 y1};
    \draw (andp.in 2) --++(0,-0.5) node[right,pos=0.65]{z0 z1};
    \draw (andp.out) --++(0,0.5) node[right,pos=0.65]{x0 x1};
    

With the syntax --++(<x,y>) we are saying that the wire ends in a position that is the sum of the initial one plus <x,y> specified.

The whole code:

\documentclass[tikz,png,border=10pt]{standalone}
\usepackage{circuitikz}    

\begin{document}
\begin{circuitikz}[inner sep=0pt,font=\tiny,text height=3pt,text width=15pt]
\draw (0,0) node[rotate=90,and port] (andp) {};

% labels a, b, c
\node[right=0.01cm,yshift=4pt] at (andp.in 1) {a};
\node[right=0.01cm,yshift=4pt] at (andp.in 2) {b};
\node[right=0.01cm,yshift=-4pt] at (andp.out) {c};

% other labels
\draw (andp.in 1) --++(0,-0.5) node[pos=0.65]{y0 y1};
\draw (andp.in 2) --++(0,-0.5) node[right,pos=0.65]{z0 z1};
\draw (andp.out) --++(0,0.5) node[right,pos=0.65]{x0 x1};
\end{circuitikz}
\end{document}

and the result:

enter image description here

Some other options have been set in the code:

  1. inner sep=0pt to let nodes do not occupy too much space (look in the pgfmanual for the exact use of this option);
  2. font=\tiny sets the font size of the labels;
  3. text height=3pt in this example allows labels a,b be vertical aligned on the same baseline;
  4. text width=15pt is used in order to have automatically labels x0,x1 - y0,y1 - z0,z1 on new line (again, see on the pgfmanual how to use this option in general);
  5. pos=0.65 represents the position of the labels within the extended wire.

To connect this port with extended wires to other logic ports, using coordinates is of help. To mark exactly the end position of the extended wires one could do:

\draw (andp.in 1) --++(0,-0.5) coordinate (a) node[pos=0.65]{y0 y1};
\draw (andp.in 2) --++(0,-0.5) coordinate (b) node[right,pos=0.65]{z0 z1};

and later on use (a) and (b) as reference.

An example:

\documentclass[tikz,png,border=10pt]{standalone}
\usepackage{circuitikz}    

\begin{document}
\begin{circuitikz}[inner sep=0pt,font=\tiny,text height=3pt,text width=15pt]
\draw 
 (1,2.5) node[rotate=90,and port] (andp) {}
 (2,0) node[rotate=90,and port] (andp1) {}
 (0,0) node[rotate=90,and port] (andp2) {};

% labels a, b, c
\node[right=0.01cm,yshift=4pt] at (andp.in 1) {a};
\node[right=0.01cm,yshift=4pt] at (andp.in 2) {b};
\node[right=0.01cm,yshift=-4pt] at (andp.out) {c};

% other labels
\draw (andp.in 1) --++(0,-0.5) coordinate (a) node[pos=0.65]{y0 y1};
\draw (andp.in 2) --++(0,-0.5) coordinate (b) node[right,pos=0.65]{z0 z1};
\draw (andp.out) --++(0,0.5) node[right,pos=0.65]{x0 x1};

% connections
\draw (andp1.out)-|(b)
 (andp2.out)-|(a);

\end{circuitikz}
\end{document}

The result:

enter image description here