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.


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


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.

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


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.

No comments:

Post a Comment