Tuesday, December 18, 2012

A solution for drawing circuit diagrams

Anyone who has ever tried to draw a circuit diagram digitally knows it's tedious. Using a vector drawing program usually requires a library of components to copy and paste from, and labeling the elements is always a pain. And using programs like Eagle or other circuit design software gives you ugly looking schematics that are more meant for creating PCBs, not for presenting.  Finally, I've found a solution that auto-generates the diagrams from code, and outputs any number of file formats for a LaTeX file, or even an SVG for editing in other vector graphics software.

Problem

Creating professional looking circuit diagrams for presentation is not easy with most drawing programs.

Solution

Circuit Macros, written by Professor J.D. Aplevich of Waterloo, is an amazing set of utilities used to render circuits using dpic, an interpreter for creating line drawings.  All of the necessary files can be found at his website, linked above, or archived at the CTAN website.  Before you get started, it's very useful to browse through the Getting Started section of the documentation.

But if you're slightly too lazy to do that, I'll give you a quick run-down of what's needed to create circuit diagrams.  First, you need both m4 and dpic (or gpic) to render the circuits you draw.  m4 is a general purpose macro processor widely available for Linux machines, (it's in the Ubuntu repositories), but a windows version also exists, found by a quick Google search for m4 windows.... (link for the lazy).  dpic is Aplevich's own implementation of the pic interpreter, and can be found on his website linked in the above paragraph (both Windows and Linux versions are available).

The basic method is to use m4 to process the macros that you execute for your circuit, and pass the result into dpic to create a tex (or svg or eps) file.  I wrote a simple windows batch file to automate this process for me, shown below.  A shell script or Make file in Linux will easily provide the same functionality.

ECHO OFF
CLS
TITLE Creating Circuit TEX File
ECHO What file would you like to convert?
SET /P filename=

CALL C:\Users\David\Documents\Circuit_macros\m4\bin\m4.exe -I C:\Users\David\Documents\Circuit_macros liblog.m4 pgf.m4 libcct.m4 %filename%.m4 > %filename%.pic
CALL C:\Users\David\Documents\Circuit_macros\m4\dpic.exe -g %filename%.pic > %filename%.tex
CALL C:\Users\David\Documents\Circuit_macros\m4\dpic.exe -r %filename%.pic > %filename%.eps

Pause

As you can see from this batch script, the compile process just requires calling m4 and dpic in order on the circuit file you've written.  My implementation creates both a tex file and an eps, so I can use it in both presentations and LaTeX documents.  To include the figure in a LaTeX document, the command is as easy as \input myfile.  So get a LaTeX document ready that you want to put a circuit diagram into, and get started.  I highly recommend copying the quick start example given in the documentation, as it's simple and easy.  Once you're there, just edit and expand using the examples given starting in section 4.2 of the documentation.

Once you're more familiar with drawing basic circuits, very complex things can be drawn quickly using the macro references at the very end of the doc PDF.  It might take some time to get started and familiar, but it's saved me so much time in worrying about the attractiveness or professional looks of my circuits.  I drew on the order of 10 complex circuits for my thesis work, and even used dpic to create some awesome flow charts.  Stick to the documentation though, as it's very well written, and will prove to be vital to your success.  Thanks to Prof Aplevich for putting these macros together.

Friday, December 7, 2012

Figure Labeling in LaTeX

I recently finished writing my master's thesis, which is easily the biggest document I've ever composed.  But because of all my time in the past few weeks in LaTeX, I've learned a lot of little things about it.  This post is a quick note about labeling figures, but I'll post more tips and tricks in the next week or so.

One of the best features about LaTeX is the ability to label the figures, and reference to them by name so that all the references are dynamic.  This makes it much easier to go insert a figure in your document, so you don't have to change all the figure numbers that occur after the inserted one.  This is done by using \label{} inside the figure environment, and using \ref{} in the document text.

Problem

When I was writing my thesis, I noticed that this didn't always work, sometimes I'd get a "figure ??" for a reference I definitely knew I'd typed in correctly.  However, I missed one small point about labeling figures.

Solution

It's very important to place the label after the caption in the figure, otherwise LaTeX will not know what you're labeling.  Here's the proper way to insert a figure:

\begin{figure}[htbp]
\centering
\includegraphics{figures/Foo_img}
\caption{Foo}
\label{Foo}
\end{figure}

If you place the label before the caption, LaTeX will render a broken reference.  If you have what appear to be random broken references, go through and make sure your label is the last thing before you close the environment.