While there’s a number of software applications for building circuit diagrams, they tend to focus on running fancy simulations, while the schematics themselves are ugly to look at. So when writing papers (and ECE201 exam problems) where nice looking diagrams are important, something specifically made for quality drawings is required. I use LaTeX for writing research papers (and hopefully soon a PhD dissertation), so a LaTeX extension would be perfect.

After some searching around, it appears there are really two viable options for incorporating schematics into LaTeX documents: the circuit_macros package, or the circuitikz package. Which one to use? After a few tests, here’s what I found.

circuitikz

The circuitikz package comes already installed as part of the MacTex installation, which is the easiest way to get LaTeX up and running on Mac OS. I’m sure Google can tell Windows users how to install it. One nice thing about this package is the code for drawing a schematic goes right into the .tex file. For example, to draw a figure with a single resistor, the schematic code is put into the figure environment just like if it was an image. Don’t forget to include the circuitikz package in the document first

\usepackage{circuitikz}

Then, anywhere in the document, you can add a schematic just like a figure:

\begin{figure}[htbp]
\centering
\begin{circuitikz}[ scale=1.2]\draw
(0,0) to [R, l=$R_1$] (2,0)
;\end{circuitikz}
\caption{A useful resistor}
\end{figure}

And you end up with:

circuitikz resistor

Be careful if copying/pasting examples from the manual, that document seems to convert hyphens into emdashes, and sometimes adds extra spaces, both of which can break the code unless you take them out.

circuit_macros

The circuit_macros package does not come as part of MacTex or Tex Live, so it must be installed manually. Copy the source to some permanent location, such as /usr/local/texlive/texmf-local/circuit_macros/. Follow the installation instructions in the README file (To refresh the LaTeX database, run “mktexlsr” at the terminal).

The code to generate the schematic is not saved directly in a .tex file, but in a separate .m4 file that must first be compiled into an image which is then inserted into the document. If willing to play with the scripts enough, one could probably get it to work with inline code. But as-is, it will require the extra compile step. This does, however, have the advantage of creating a standalone image file which could also be used in presentations, webpages, etc.

So to recreate the previous resistor using circuit_macros, first make an .m4 file with the following code, call the file resistor.m4:

.PS
resistor( right_ elen_ );
llabel(,R_1,);
.PE

Then, using the Makefile provided in the circuit_macros installation, run “make resistor.eps” to generate an Encapsulated Post Script file. You could alternatively use .png or .jpg, but vector graphics like eps are always better. Then you’ll have an image to include in LaTeX just like any other image.

circuitmacros resistor

Comparison

Now let’s compare the two methods as far as aesthetics and ease of coding, starting with an op-amp circuit. This is example 18 from the circuit_macros example set. The circuit_macros m4 file looks like this:

.PS
cct_init
 "$v_i$" rjust; dot
 resistor(right_ dimen_) ; llabel(,R_i)
T: dot
 line right_ linewid/4
A: opamp with .In1 at Here
 line from A.In2 to (T,A.In2)
 resistor(down_ dimen_) ; rlabel(,R_g)
 ground(,T)
 line right_ dimen_ from A.Out
 diode(up_ dimen_); dot
 {line right_ linewid/2; dot; "$v_o$" ljust }
 resistor(left_ to (A.Out,Here)) ; rlabel(,R_o); dot
 { diode(down_ to A.Out); dot }
 line to (T,Here) then to T
.PE

All the components are placed at the current drawing location, with a given direction and length. Then the drawing location moves to the other end of the component. No x-y coordinates are required. Curly braces group components together so that after the braces are closed, the drawing location jumps back to where it was before the braces. Parts can be labeled using the colon notation, so individual pins can be referred to and connected later. Seems to work pretty well.

Now we’ll re-create this example using the circuitikz format. The following was pasted into a LaTeX figure:

\begin{circuitikz}[ scale=1.2]\draw
(3,0) node[op amp](opamp) {}
 (opamp.+) node[left] {}
 (opamp.-) node[left] {}
 (opamp.out) node[right] {}
(opamp.+) to [R, l_=$R_g$] (2,-2)
(2,-2) node[ground] {}
(0,.4) node[anchor=east]{$v_i$}
to[short, *-] 
(0,.4) to [R, l=$R_i$] (opamp.-)
to [short, *-] (2,1.8)
to [short, -*] (4,1.8)
(4,1.8) [D*, -*] to (opamp.out)
(4,1.8) to [R, l=$R_o$, -*] (6,1.8)
(6,0) to [D*] (6,1.8)
(6,1.8) to [short, -*] (7,1.8)
(7,1.8) node[anchor=west]{$v_o$}
(6,0) to [short] (opamp.out)
;\end{circuitikz}

Immediately you’ll see lots of absolute x-y coordinate pairs in the code. Anyone who’s done anything with computer graphics knows absolute coordinates are never a good thing. If one wanted to insert a single component somewhere, almost every coordinate position would need to be recalculated! Maybe there’s a way around this by assigning variables or something, but there was nothing demonstrated in the circuitikz manual other than using raw coordinates. It makes it too easy to mess up a single number and end up with something stupid like I accidentally did here. Oops.

opamp crooked

Comparing the full output of the example, here’s what we get. (Note the images here are screen-grabs of the LaTeX output pdf, so they have been pixelized.)

comparison example 18

Man, those are some big diodes. Here are two more examples of other circuits drawn with both tools:

comparison example 7

comparison example 5

As far as the drawing quality and style, I prefer the circuit_macros versions. It has more consistent 2-pixel wide lines, and components are sized a little more proportionally correct.

The source files, both m4 and tex, required for these examples can be downloaded here

Summary

Here I’ll list some pros and cons of each tool.

circuitikz

Pros:

  • Source is inline with .tex file, so no additional compile is required.
  • Included in MacTex package, making installation easy.

Cons:

  • Drawing quality not as high in my opinion. Some lines seem to be 1-pixel wide, others 2-pixel. Some components seem out of proportion.
  • Absolute coordinate system makes it very hard to maintain schematics.
  • Fewer electrical symbols available.
  • Little tutorial or documentation available.
  • No real-time preview or GUI.

circuit_macros

Pros:

  • Separate compile produces an image file which can be used outside of LaTeX.
  • Cleaner looking graphics output.
  • No absolute coordinates required, only component direction and length.
  • More symbols available.

Cons:

  • Code is separate from tex file, so additional compile is necessary.
  • Must be manually installed.
  • Little tutorial or documentation available.
  • No real-time preview or GUI.

As you can see from my list, I concluded that circuit_macros is the best choice for me. Notice neither tool has much documentation out there, other than a manual which doesn’t really exhaustively describe the features. Perhaps as I learn more about how to use it, I’ll post additional tutorial examples here.

Download Example Source Files


EDIT 4/28/2013: To remedy the lack of preview/GUI on Mac OS, I developed a bare-bones python app to generate and view the image in near-real-time. See the post here.


EDIT 08/29/2014: To continue my obsession with drawing nice circuit diagrams programatically, I developed an alternative using Python. See the post here.