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.

Monday, November 5, 2012

Using Zotero with BibTeX

In the past few weeks I've been writing a paper, which means lots of formatting of images and references and things. But I've finally found an amazingly good solution to my reference management. Zotero is free software built to manage research references similar to the way iTunes manages music tracks. And it's wonderful.

I've been familiar with using BibTeX with my LaTeX papers for a while. It's pretty simple to go download the BibTeX citation from the hosting website, copy it into my .bib file, give it a meaningful key, and cite it in LaTeX. But Zotero can simplify this process even more. Zotero comes in two forms: the broswer version for Firefox, and the standalone version, extended with browser plug-ins. Since I use Chrome as my main browser, I chose the standalone version and installed the Zotero connector for Chrome.

Once that's all installed, the easy part comes (the research!). Whenever you find a new paper, video, news article, or anything you want to cite, a small icon pops up in the corner. By clicking on that icon, the reference is automatically added to your Zotero library, and downloaded for viewing offline (if you have the standalone version, at least).

Once your references are in your Zotero library, they are easy to sort into folders. The best part, however, is the export feature. All or any subset of the library can be exported as a BibTeX file. Unfortunately, I ran in to two problems, both of which I solved after a short bit of Googling.

Problem:

First, I noticed very quickly that all the "keys" were not the same as I written myself. I had a system previously in which I used the author and year, like \cite{Feynman1964}, but Zotero was creating the BibTeX file with keys with author, title, and year. What a pain it would be to go change all the citations in my papers.

Solution:

The solution is to rewrite the translator file, BibTeX.js. Typically found in Users/AppData/Local/Zotero ... keep going until a translators directory shows. In there, edit the file BibTeX.js. 

Find the line:
var citeKeyFormat = "%a_%t_%y";
And change it to whatever you want. I changed mine to:
var citeKeyFormat = "%a%y";

Problem:

Second, my LaTeX compiler could not recognize special characters, (รจ), even though Zotero output them into the BibTeX file. Instead, I found that I could go change them to "\`e" or something similar, but again, I'm not a fan of manually changing everything.

Solution:

The solution is to use Western character encoding on export. The place to change this isn't obvious at first. In export options, it's necessary to check the box that says "Display character encoding option on export." Then a dropdown menu shows up in the export dialog, where you should pick Western character encoding.

Hopefully you too can enjoy the benefits of Zotero to their fullest extent. It sure has made my life easier, and I owe a big thanks to the folks behind the software and the surrounding community.