While preparing a manuscript for publication, it may become necessary to combine several sub-figure to a single figure outside the main LaTeX document. A possible reason may be package conflicts with the journal’s class- or style-file. Therefore, a journal might disallow usage of LaTeX packages such as subfigure, subfig, or subcaption. Another reason may be that the journal processes figures separately and therefore asks for a separate figure file. Here, I describe an approach that combines several sub-figures into a single figure, including sub-labels and sub-captions. Essentially, we combine sub-figures in a separate document, thereby adding sub-captions and labels. The main caption may or may not be added to the figure. This figure can then be included in the main document or processed by the journal later on, without using an additional sub-figure packages. The drawback is that (sub-)figures generated outside the main manuscript can not be automatically referenced in the text.
To combine several sub-figure into a single figure, we make use of the standalone document class. Standalone is a minimal document class that crops the resulting output file to its content, leaving no extra white space. Standalone can also be imported as a package, but we are not going to use it as such here.
First, we set the document class to be “standalone”. For detailed information on this class, see the package documentation.
\documentclass[varwidth, border=10pt]{standalone}
We use the document class with two options: varwidth
and border=10pt
. The varwidth
option wraps the content in a varwidth
environment, which is based on minipage
. It makes paragraphs available, and thus enables line-breaks in the document. Otherwise, any content would just be stacked to the right of previous content.
The border
option adds some white space on every side of the content. This option may be omitted. Alternatively, we can specify horizontal (left/right) and vertical (bottom/top) spacing or each side individually.
border={10pt 5pt} % left/right bottom/top border={10pt 10pt 0pt 10pt} % left right bottom top
Next, we load the packages that are necessary to combine several figures and change the formatting. First, we import the subcaption package to create sub-figures and sub-captions. Alternatively, the subfig package may also be used. But, I chose subcaption because it works really well with the caption package. The latter lets us change the caption formatting by loading the package with a few options. These are described in the package documentation. Finally, we load the graphicx bundle in case we want to define the width of the sub-figure or perform additional figure manipulation.
\usepackage{subcaption} \usepackage[labelformat=parens,labelsep=quad,skip=3pt]{caption} \usepackage{graphicx}
With that, we are now ready to combine several sub-figures to a single figure. This is done in the document body. Any number of columns is possible, although I would limit it to three. We control the number of columns by adding an empty line, which tells LaTeX to insert a line-break. In the example below, we combine four figures in a 2×2 layout.
\begin{document} \begin{figure} \begin{subfigure}{6cm} \centering\includegraphics[width=5cm]{figure-file-1} \caption{Caption text 1} \end{subfigure} \begin{subfigure}{6cm} \centering\includegraphics[width=5cm]{figure-file-2} \caption{Caption text 2} \end{subfigure} \begin{subfigure}{6cm} \centering\includegraphics[width=5cm]{figure-file-3} \caption{Caption text 3} \end{subfigure} \begin{subfigure}{6cm} \centering\includegraphics[width=5cm]{figure-file-4} \caption{Caption text 4} \end{subfigure} \end{figure} \end{document}
I generated a few dummy figures in R, which I then combined to a single figure using the code above. I could have added a main figure caption. However, some journals might ask for all captions to be listed separately or provided withing the main document.
Complete LaTeX code to generate example
\documentclass[varwidth,border=10pt]{standalone} \usepackage{subcaption} \usepackage[labelformat=parens,labelsep=quad, skip=3pt]{caption} \usepackage{graphicx} \begin{document} \begin{figure} \centering \begin{subfigure}{6cm} \centering\includegraphics[width=5cm]{linear} \caption{Linear: $y=x$} \end{subfigure}% \begin{subfigure}{6cm} \centering\includegraphics[width=5cm]{quadratic} \caption{Quadratic: $y=x^2$} \end{subfigure}\vspace{10pt} \begin{subfigure}{6cm} \centering\includegraphics[width=5cm]{square-root} \caption{Square root: $y=\sqrt{x}$} \end{subfigure}% \begin{subfigure}{6cm} \centering\includegraphics[width=5cm]{logarithmic} \caption{Logarithmic: $y=log_{e}(x)$} \end{subfigure} \end{figure} \end{document}
R code for figure creation
x <- 1:50 plotCurve <- function(x,y,ylab,filename) { pdf(paste(filename, 'pdf', sep='.'),width=6,height=6) plot(x,y,ylab=ylab,pch=20) dev.off() } plotCurve(x,x,'y=x','linear') plotCurve(x,x^2,'y=x^2','quadratic') plotCurve(x, sqrt(x), 'y=sqrt(x)','square-root') plotCurve(x,log(x), 'y=log(x)', 'logarithmic')
Alternative approach
An alternative approach is to use any LaTeX standard document class (e.g. article) with an empty
pagestyle and then trim extra whitespace using pdfcrop. pdfcrop is a Perl command line tool that is installed with TeX live/MiKTeX. Some sample code is provided below. Feel free to give it a try.
LaTeX:
\documentclass{article} % Load packages \thispagestyle{empty} \begin{document} % Figure/subfigure code \end{document}
Terminal:
pdfcrop -margins "10 10 10 10" figure.pdf figure-crop.pdf
Kerrigan
Thank you, this was very useful.
Anja
Thank you, this is indeed very useful!
I only have one question:
What should I adjust if I have 3×2 subfigures?
I followed your suggestion with adding an empty line after the third subfigure but I end up with 2 colums and 4 lines instead of 3 colums and 2 lines.
tom
Hi Anja,
Thanks for your question. The code below produces three columns. The trick is to split the line width into three equal parts instead of assigning a fixed width.
Best, Tom
Ben
Many thanks for these tricks!
frank
Doesn’t seem to work.
! LaTeX Error: Not in outer par mode.
tom
Hi Frank,
Thanks for your feedback. The code above works for me. The error you see typically arises if you place a floating environment (e.g.
figure
,table
) within a non floating environment (e.g.minipage
). If that doesn’t help, please provide a minimal working example for me to reproduce the error.Best, Tom
Vincent
The logarithmic graph has base e, not 10
tom
That’s right, thanks for letting me know.
Best, Tom
Hoan
Hi, thanks a lot.
I did as your instruction and it worked. But when I tried to label each sub figure and referenced it did not work (I want it appears 1a 1b 1c…). Do you have any suggestions?
tom
Hi Hoan,
Thanks for your feedback. You are right, the labels are missing in the example above. Please take a look at the examples on my figures code snippet page. There, the labels are set correctly and it shows how to reference the figures. Use either
\ref
or\subref
depending on if you want to get the full label or only the sub-label.HTH, Tom
Fossil Plants
THANKS!!!! This saved me a bunch of time! I ended up using the pdfcrop technique, as it is much simpler to pull out a page with a figure and crop it, then to make the new fig in latex.
tom
Great to hear. Best, Tom