Thursday, November 15, 2012

Subfigures in LaTeX

How can you create subfigures or subfloats in LaTeX? It's often easy to combine multiple figures from within a statistical package or image software, but it's generally best not to if you want to include subcaptions as text, for improved searchability.

Fortunately the subcaption package in LaTeX allows us to do this easily. (The subfigure and subfig packages have been deprecated, so it's best to use subcaption instead.)

How can you make more than one image or table be part of a LaTeX figure while still being able to create text captions for each?

Using the word clouds from my R word cloud tutorial, let's look at an example:


In order to make the figure containing the subfigures, we'll need to decide a few things first. Then it's just a matter of letting LaTeX know our preferences:

We also need to let LaTeX know to use the caption and subcaption packages. The full code for the example image above is included below, and also in a gist.


\documentclass{article}

\usepackage{graphicx}
\usepackage{caption}
\usepackage{subcaption}

\begin{document}

\begin{figure}
 \centering
 \begin{subfigure}{0.4\textwidth} % width of left subfigure
  \includegraphics[width=\textwidth]{rncalt.png}
  \caption{RNC} % subcaption
 \end{subfigure}
 \vspace{1em} % here you can insert horizontal or vertical space
 \begin{subfigure}{0.4\textwidth} % width of right subfigure
  \includegraphics[width=\textwidth]{dncalt.png}
  \caption{DNC} % subcaption
 \end{subfigure}
 \caption{Wordcloud of national conventions} % caption for whole figure
\end{figure}

\end{document}

For more information, this wikibooks article is useful.


--
This post is one part of my series on LaTeX.

No comments:

Post a Comment