Sunday, July 1, 2012

Using In-text Fonts in LaTeX Figures

One thing that has always frustrated me about putting figures into Word documents is the mismatch between the fonts in the figures and the running text.

Drafting images in Word is a hassle, and it’s hard to retain their look when exporting them to other programs. More importantly, figures drafted in Word don’t look very sharp or professional. If you create the image in another program like MS Visio and then import it, the font size rarely matches the font size in the rest of the document. And if you change the font type in the Word document, you have to go back and rework all the text in the Visio file. Some of line breaks may have changed because the new font is larger or smaller. You may need to change the alignment and indents to get the right look. And so on. It’s a time-consuming nuisance.

And if you use another program that only gives you an image file as output (e.g., a PDF or JPEG), it’s even harder to match the font size in the rest of the document. All you can do is resize the entire image until the font looks about the same as the surrounding text. If the figure is larger than the margins of the document when the font sizes finally match, you’ll have to redraft it and try again.

If you’re using LaTeX, there’s an elegant solution to this problem, and it’s one of the reasons I love preparing documents in LaTeX: You can use Inkscape to prepare the figure and then save a copy as a PDF with this option checked: “PDF+LaTeX: Omit Text in PDF, and create LaTeX file”.

First, you need to draft the figure in Inkscape. I won’t go into details about how to use Inkscape except to provide the following tip on horizontally and vertically centering text in a box or rectangle:

  1. Create a shape like a box or rectangle and create a text object.
  2. Open the “Align and distribute...” dialogue in the “Object” menu (or press Shift+Ctrl+A).
  3. Choose a “Relative to:” mode that refers to an object. I prefer “Biggest object” as the text object ought to be smaller than the shape it appears in.
  4. Select both the text and the shape.
  5. Press both horizontal and vertical align buttons in the “Align and distribute...” dialogue.

Now the text will appear vertically and horizontally centered in the shape. The following image is an example I created for illustrative purposes.

Example figure

Example figure

After drafting the figure in Inkscape:

  1. Go to the “File” menu, select “Save a copy...
  2. Choose “Portable Document Format (*.pdf)”.
  3. On the following screen, check the box for “PDF+LaTeX: Omit text in PDF, and create LaTeX file”. (See the figure below.)

Inkscape Screenshot: File -> Save a copy... -> Portable Document Format (*.pdf)

Inkscape Screenshot: File -> Save a copy... -> Portable Document Format (*.pdf)

Inkscape separates the graphics and the text into two files: One is a PDF with the graphics and the other is a LaTeX file (with a *.pdf_tex extension) which contains instructions for placing the text and the text itself.

To insert this image into the LaTeX file, simply insert the following code in your LaTeX document:

\input{filename.pdf_tex}

You can set the width of the figure with \svgwidth, and LaTeX will maintain the aspect ratio from the original image. For example, insert the following code in your LaTeX document:

\def\svgwidth{\textwidth}

So the full code for inserting a figure like this could be the following:

\begin{figure}
  \centering
  \def\svgwidth{\textwidth}
  \input{filename.pdf_tex}
  \caption{the caption}
  \label{fig:thelabel}
\end{figure}

And you need to include the pstricks package in the preamble of your LaTeX document with the following code:

\include{pstricks}

(Note that you don’t need the graphicx package.)

There are a lot of ways to get figures into LaTeX documents, but I like this approach and promote it here, because it’s relatively easy. With other approaches like PGF/TikZ, PSTricks and Xfig, you need to either learn an extensive new set of commands and directly code the figure or use graphical user interfaces (GUIs) that will output code for you. But in my experience, GUI programs for those packages tend to be awkward and not highly developed, so you’re left with the unappealing and time-consuming chore of digging through instruction manuals to learn how to code your figure. Inkscape, on the other hand, is relatively easy to use, and if you don’t want to draft the figure in Inkscape directly, you can import it from another program. I have imported PDFs from Visio into Inkscape, for example, touched them up and then exported them as described above.

Here are some example files to see this approach in action. Put all three in the same directory and then typeset the *.tex file for the document using XeLaTeX.

Here is the output, and here is the SVG file for the image in case you want to play around with it in Inkscape.

And finally I have to admit that this is a post with poor attribution. I learned about this approach several months ago, and the sources that I used to figure it out have since been lost to my foggy memory. If people have sources and post them in the comments, that would be welcome and appreciated.

No comments:

Post a Comment