Embedding figure text into a Latex document

Often times we have to create plots and schematic drawings for our publications. These figures are then included in the final document either as bitmaps (png, jpeg, bmp) or as vectorized images (ps, eps, pdf). Some inconveniences that arise due to this process and are noticed in the final document are:

  • Loss of image quality due to resizing the figure (bitmaps only)
  • Different font type and size from the rest of the text
  • Limited resizing possibility due to text readability
  • No straight-forward method to add equations to the figure

If the document is being created in LaTeX, it is possible to overcome all these inconveniences by exporting your figure into either svg or postscript formats and converting it into pdf+Latex format with Inkscape. This format allows the LaTeX engine to understand and treat figure text as any other text in the document and the lines and curves as a vectorized image.

EXPORTING FIGURE

The process for creating of a PDF+LaTeX figure is described below:

1 – Create your figure and save it in either svg or postscript format. Inkscape, Matlab, GNUPlot, and Python are examples of software that can export at least one of these formats. If your figure has any equations, remember to type them in LaTeX format in the figure.

2 – Open your figure with Inkscape, edit it as you see necessary (figure may need to be ungrouped), and save it.

3.0 – If you are comfortable with using a terminal and the figure does not need editing, open a terminal pointing to the folder where the figure is and type the following the command (no $). If this works, you can skip steps 3 and 4 and go straight to step 5.

$ inkscape fig1.svg --export-pdf fig1.pdf --export-latex

3 – Click on File -> Save As…, select “Portable Document Format (*.pdf)” as the file format, and click on Save.

ss1

4 – On the Portable Document Format window that will open, check the option “PDF+LaTeX: Omit text in PDF, and create LaTeX file” and click on OK.

ss2

Inkscape will then export two files, both with the same name but one with pdf and the other with pdf_tex extension. The pdf file contains all the drawing, while the pdf_tex contains all the text of the figure and calls the pdf file.

5 – On your latex document, include package graphicx with the command \usepackage{graphicx}.

6 – To include the figure in your document, use \input{your_figure.pdf_tex}. Do not use the conventional \includegraphics command otherwise you will end up with an error or with a figure with no text. If you want to scale the figure, type \def\svgwidth{240bp} (240 is the size of your figure in pixels) in the line before the \input command. Do not use the conventional [scale=0.5] command, which would cause an error. Some instructions are available at the first lines of the pdf_tex file, which can be opened with a regular text editor such as notepad.

Below is a comparison of how the same figure would look like in the document if exported in PDF+LaTeX and png formats. It can be seen that the figure created following the procedure above looks smoother and its text style matches that of the paragraphs above and below, which is more pleasant to the eyes. Also, the text can be marked and searched with any pdf viewer. However, the user should be aware that, since text font size is not affected by the scaling of the figure, some text may end up bigger than boxes that are supposed to contain it, as well as too close or to far from lines and curves. The former can be clearly seen in the figure below. This, however, can be easily fixed with software such as Inkscape and/or with the editing tips described in the following section.

ss3

TIPS FOR TEXT MANIPULATION AFTER FIGURE IS EXPORTED

If you noticed a typo of a poorly positioned text in the figure after the figure has been exported and inserted in your document, there is a easier way of fixing it other than exporting the figure again. If you open the pdf_tex file (your_figure.pdf_tex) with a text editor such as notepad, you can change any text and its position by changing the parameters of the \put commands inside the \begin{picture}\end{picture} LaTeX environment.

For example, it would be better if the value 1 in the y and x axes of the figures above would show as 1.0, so that its precision is consistent with that of the other values. The same applies to 2 vs. 2.0 in the x axis. This can be fixed by opening file fig1.pdf_tex and replacing lines:

\put(0.106,0.76466667){\makebox(0,0)[rb]{\smash{1}}}%
\put(0.53916667,0.0585){\makebox(0,0)[b]{\smash{1}}}%
\put(0.95833333,0.0585){\makebox(0,0)[b]{\smash{2}}}%

by:

\put(0.106,0.76466667){\makebox(0,0)[rb]{\smash{1.0}}}%
\put(0.53916667,0.0585){\makebox(0,0)[b]{\smash{1.0}}}%
\put(0.95833333,0.0585){\makebox(0,0)[b]{\smash{2.0}}}%

Also, one may think that the labels of both axes are too close to the axes. This can be fixed by replacing lines:

\put(0.02933333,0.434){\rotatebox{90}{\makebox(0,0)[b]{\smash{$x\cdot e^{-x+1}$}}}}%
\put(0.539,0.0135){\makebox(0,0)[b]{\smash{x}}}%

by:

\put(0.0,0.434){\rotatebox{90}{\makebox(0,0)[b]{\smash{$x\cdot e^{-x+1}$}}}}%
\put(0.0,0.0135){\makebox(0,0)[b]{\smash{x}}}%

With the modifications described above and resizing the legend box with Inkscape, the figure now would look like this:

ss4

Don’t forget to explore all the editing features of inkscape. If you export a figure form GNUPlot or Matlab and ungroup it with Inkscape into small pieces, Inkscape would give you freedom to rearrange and fine tune your plot.

AeroVis: Turning a Glyph Plot Into a Figure

This post assumes you’ve already figured out which variables to assign to which axes and found a good camera angle.  I explain how to use AeroVis, a raster graphics editor, and a vector graphics editor to make a good-looking vector graphic that you can improve iteratively.  It’s important to be able to do this because the first version of your figure will always need improvement: your glyphs were too small, or too large, or the wrong color.  Or perhaps you need two similar figures: brushed and unbrushed, or the same glyphs with two different colorings.  In any case, being able to make changes to your figures without starting from scratch will save you a lot of time.

Let’s take an example where you want to make two versions of a plot: one showing all of the glyphs, and another highlighting glyphs with a particular characteristic.  You could do this purely in AeroVis, and it looks pretty good.  But for really detailed control over the appearance of the figure, I recommend the procedure below.

Start in AeroVis with the view you like.

AeroVis glyph plot showing all glyphs.

Starting glyph plot.

Camera View Settings

Before you go capturing any screenshots, carefully record the camera view settings. Do this by finding the camera settings icon on the view menu. Click it.  It opens up the camera view settings dialog.  Write everything down.

Camera icon from AeroVis

Camera Settings icon

Camera Settings dialog

Camera Settings dialog

Double-check.  Once you’ve captured the camera information, you can close AeroVis, come back later, and generate exactly the same image.

Capturing the Image

Open the Snapshot Controls dialog by clicking the snapshot icon in AeroVis.

Snapshot Icon

AeroVis snapshot controls

AeroVis snapshot controls

Resize by a big factor.  I like 5.0, but suit yourself and don’t be embarrassed to make a huge file.

Now turn off the axes and repeat.  We’re going to make better-looking axes by hand, but we need to know where they are relative to the glyphs, which is why we made the first plot.

The same glyph plot with no axes

Creating the Vector Graphics File

Open up Illustrator or Inkscape.  Make a layer called “reference.”  Import the version of the image that has axes into your reference layer.

Now make a layer called “axes.”  Using the reference layer as a guide, draw axes.  You’re doing this because, while you can’t make the glyphs into smooth, beautiful vector graphics, you can fix the axes.  Include whatever axes labels you want.  I like to draw the back walls, but you could just do the principal axes, or the whole box like AeroVis does.

Vector graphics axes

Vector graphics axes

See how they aren’t all jagged like in the snapshot?  That’s why we did this.

Add Transparency

Open up the image that doesn’t have axes using a raster graphics editor like Photoshop or the Gimp.  Add transparency to the current layer.  Select the color white, and delete it.  Now all that’s left is the glyphs and the legend. Note from Joe: This transparency idea is great. One benefit is you can draw things behind the glyphs if you’d like, such as gridlines. Also, you can use the “Lasso” tool to trace around the figure to remove previous labels, and if you use transparency to get rid of the whitespace, when you import it into another program, it will have nice and “clean” edges.

Over in your vector graphics editor, make a new layer.  Call it “glyphs”.  In Gimp/Photoshop, select the rectangle containing just the glyphs, then copy and paste it into the glyphs layer of your vector graphics image.  Line up these glyphs with the glyphs in the reference layer, then hide the reference layer.  Now you have a version of your glyph plot with nice axes!

Note from Joe: If you have problems copy/pasting like Matt suggests, you can save your copy/paste in a png (which preserves transparency) and turn compression completely off. I don’t think you use much quality doing this, and you have a copy of the file with no axes for your reference later on.

Glyph plot with nice axes

Glyph plot with nice axes

Second Figure

This is where it helps to have written down the camera settings. We’re going to make a second figure with certain glyphs emphasized.  Go back to AeroVis, and enter your camera settings. Brush the data so that only your chosen glyphs are visible.  Hide the axes, and take a snapshot.

Brushed Data with No Axes

Brushed Data with No Axes

Open the snapshot in your raster editor (Gimp / Photoshop).  As before, add transparency, select and delete the color white.  Make a new layer in your vector editor (Inkscape / Illustrator) above the axes layer but below the glyphs layer.  Paste the glyphs there and line them up.  You may need to hide the glyphs layer and use the reference layer instead, if you can’t see the ones you just pasted in.  Adjust the transparency of the glyphs layer until there’s a clear distinction between your emphasized glyphs and your de-emphasized glyphs.  The reason we put the new layer under the glyphs layer is that, with the transparency, it preserves the illusion of depth.  Any glyphs that were obscuring the emphasized glyphs in the original screenshot still appear to be in front of the emphasized glyphs.

Final Plot with Emphasized Glyphs

Final Plot with Emphasized Glyphs