Putting Figures in LaTeX Documents

Figures are known as floating bodies as they float to the most suitable place rather than necessarily appearing exactly in the text where you put the commands. There are essentially 2 main ways of including figures you should know.

EPS (for standard latex) or PDF (for pdflatex) graphics

If you have created an EPS or PDF picture using another packages (e.g. Maple, Matlab, xfig) you can include it in your LaTeX with commands of the form

\begin{figure} [ht]
\centering
\includegraphics{filename}
\caption{My figure}
\label{the-label-for-cross-referencing}
\end{figure}

which tells LaTeX to put the figure here or at the top of the next page, gives it a caption and a label for later use to produce cross references. For this to work at the beginning of your LaTeX include the graphicx package, e.g. you need a line

\usepackage{graphicx}

Figures with LaTeX on

Using Inkscape or Xfig you can draw figures and write on them using LaTeX. Other packages can also put LaTeX on to their output, e.g. Mathematica. Matlab can only fake very basic LaTeX and so it is best to use xfig to draw on graphs produced in most other packages. These days people are more likely to use pdflatex and hence generate PDF+latex images but you can equally produce EPS+latex in which you just need to change the file name include to have the different extension.

If using PDF images then you will need the graphicx package as above. If using EPS images you will instead need the epsfig package (which is a small wrapper around the graphicx package that adds some additional commands to handle epsfig files).

Images produced with LaTeX on them via Inkscape are inserted as a single PDF file as with normal images.

Images produced with LaTeX on them via Xfig result in an image file and a latex file for which one then includes them with an appropriate LaTeX command explained later.

How to put LaTeX on Inkscape pictures

One way to produce images with LaTeX on them via Inkscape is to use the inkscape-figures python package through which one creates two part images in a similar fashion to the method below for Xfig. One then includes the pdf_t latex file which contains the latex and further includes the image overlaying them appropriately.

An easier approach though is to use the Inkscape TexText plugin (installed by default on department Linux systems). In Inkscape whenever you wish to insert some text written in LaTeX invoke the extension from 'Extension -> Text -> TexText' menu (or use the shortcut Alt-q to invoke the last extension used if using it repeatedly). The extension brings up a dialog where you can write your latex and it compiles it to then place within the document. However, it stores the underlying latex code too so you can edit the text previously insert even though it compiled it to an image. When finished creating your image you can then export it as a PDF file to include in your document as normal. To edit the image further you open the SVG file for the image.

How to put LaTeX on Xfig pictures

  • In xfig draw your picture.
  • Add the text: when you click on the text button (the T) lots of options appear at the bottom of the screen. The really important one is the one marked Text Flags. Click on this and change the option TeX Flag (formerly known as the Special Flag) from normal to TeX. Now any text you write on the figure is magically marked. You can write text as if it were LaTeX, e.g. you could put $\alpha$ to put an alpha on the picture etc.. If you add the text and forget to set the flag you can set it later on pieces of text by using the edit button and clicking on the text. This produces a window of options of which one is the TeX flag which you can set to TeX.
  • Having drawn the picture and added the LaTeX text save the figure using the file button at the top. Name the file something like myfile.fig
  • Export the figure: Because the figure contains LaTeX you need to export it in 2 (or 3) pieces. Clicking on the export menu will bring up a box of options. You need to choose to export it as one of
    Combined PS/LaTeX (both parts)
    Combined PDF/LaTeX (both parts)
    Combined PS/PDF/LaTeX (3 parts)
  • This should create 2 or 3 files called myfile.pstex + myfile.pstex_t, myfile.pdf + myfile.pdf_t, or myfile.pstex + myfile.pdf + myfile_pspdftex_t and these are what are used in your (pdf)latex. However don't delete the .fig file as that is what is needed by xfig if you wish to re-edit the picture.

To import an image file into xfig so that you can label it up with LaTeX you use the picture object button.

Note when you select a tool in xfig at the top right a window displays what each of the mouse buttons will do (once you move the mouse over the canvas).

 

\begin{figure} [ht]
\centering
\input{filename}
\caption{My figure made with LaTeX on it}
\label{the-label-for-cross-referencing}
\end{figure}

where the filename would have extension pstex_t if output from Xfig with the Combined PS/LaTeX option, pdf_t if output with the Combined PDF/LaTeX option, or pspdftex_t if output with the PS/PDF/LaTeX option. The last option gives you 3 files and thus covers compiling the document with latex or pdflatex.

The file myfile.pstex_t/myfile.pdf_t/myfile.pspdftex_t is LaTeX and contains the text part of the picture and also contains a line to include the picture part.

How to store the images in a different folder than the main LaTeX file (without including a full path to that location each time)

By default includegraphics and input commands look for a file in the current directory/folder, so unless you include full paths to other locations your files would have to be in the same directory as your LaTeX in order for LaTeX to find them. However it is possible to put them in other directories to make things clearer. By using a command of the form

\graphicspath{{./}{Figs/}}

near the beginning of your LaTeX you can instruct LaTeX to look in other directories for postscript files. The above example says to look in this directory and the subdirectory called Figs.

Furthermore, on the Maths network, if you put all your LaTeX files in subdirectories of a directory called ~/TeX then they will be automatically found.

Thus with both these features both parts of the exported figure can be found even if they are not in the same directory as the LaTeX.

Please contact us with feedback and comments about this page. Last updated on 04 Feb 2023 14:13.