# Crop a float containing subfigures horizontally with Preview or Standalone package

I have a float containing subfigures. I want to "export" it as a single item so that I can include it elsewhere as one object. I tried to use the Preview package with [active,floats,tightpage], but it only makes the page tight vertically - it leaves significant white space to the right of the second subfigure in the example below:

\documentclass{article}
\usepackage{mwe}

\usepackage{subcaption}

\usepackage[active,floats,tightpage]{preview} % include this last

\begin{document}

\def\FigSize{.33}
\begin{figure}
\begin{subfigure}[b]{\FigSize \textwidth}
\includegraphics[width=\FigSize \textwidth]{example-image-a}
\end{subfigure}
\begin{subfigure}[b]{\FigSize \textwidth}
\includegraphics[width=\FigSize \textwidth]{example-image-b}
\end{subfigure}
\end{figure}

\end{document}


Is there a way to make it crop in the horizontal direction as well?

I have also tried this:

\documentclass[preview]{standalone}
\usepackage{mwe}
\usepackage[]{subfig}

\begin{document}

\def\FigSize{.23}
\begin{figure}[htbp!]
\centering
\subfloat[]
{
\includegraphics[width=3cm]{example-image-a}
}
\subfloat[]
{
\includegraphics[width=3cm]{example-image-b}
}
\end{figure}

\end{document}


but the result is similar in that the output is still the full page width rather than being tight to the two subfigures.

------------- Edit ----------------

As Peter Grill suggested, adding 'varwidth' does indeed fix the simple demo. In my real case, I am using subfloats from subfig:

\documentclass[preview,varwidth]{standalone}
\usepackage{mwe}
\usepackage[]{subfig}

\begin{document}

\def\FigSize{.23}
\begin{figure}[htbp!]
\centering
\subfloat[]
{
\includegraphics[width=3cm]{example-image-a}
}
\subfloat[]
{
\includegraphics[width=3cm]{example-image-b}
}
\end{figure}

\end{document}


The cropping is still correct, but the (a) and (b) below the figures are not centered on the figures as they should be?

• Using \documentclass[preview, varwidth]{standalone} will fix the version using the standalone package. Oct 31, 2012 at 17:13
• @PeterGrill - Ah, getting closer! Please see my edit in the question for a problem with using subfig's subfloat with 'standalone'. Oct 31, 2012 at 17:27
• I think floats are problematic. I believe that standalone has special handling for figure. If you don't get a satisfactory answer here, I would recommend you crop only the actual figures via standalone. If the need is to ensure identical formatting of a group of figures, then you could just have a separate .tex file, and import that in your main document. Sorry that I am unable to help you more with this. Oct 31, 2012 at 18:01
• Ooops, wrong thread. Oct 31, 2012 at 18:14

As pointed out by Peter Grill, using varwidth as an option to standalone helps fitting images with boundary as per the width. But for reasons beyond my understanding, the captions are aligned to the left. As a cure, one can load caption package with the option justification=centering.

Code:

\documentclass[varwidth]{standalone}
\usepackage{mwe}
\usepackage[labelfont=normalsize,labelformat=parens,
justification=centering]{caption,subfig} %%% Center the caption
\begin{document}

%\def\FigSize{.23}
\begin{figure}[htb!]
\subfloat[]{%
\includegraphics[width=3cm]{example-image-a}
}%
\hspace{3pt}%
\subfloat[]{%
\includegraphics[width=3cm]{example-image-b}%
}%
\end{figure}
\end{document}


Or

\documentclass[varwidth]{standalone}
\usepackage{mwe}
\usepackage[labelfont=normalsize,labelformat=parens,
justification=centering]{caption,subfig} %%% Center the caption
\begin{document}
\begin{minipage}{6.2cm}
\begin{minipage}{3cm}
\includegraphics[width=3cm]{example-image-a}
\captionof{subfigure}{}
\end{minipage}%
\hfill
\begin{minipage}{3cm}
\includegraphics[width=3cm]{example-image-b}%
\captionof{subfigure}{}
\end{minipage}%
\end{minipage}
\end{document}


• @DavidDoria: Besides accepting answers you should also upvote answers/questions that you find useful. Nov 1, 2012 at 0:03
• @Harish Kumar, Excellent. Why would you ever not want that behavior haha? Also, when I do this, then if I just '\includegraphics{output}' in another document, the caption labels should appear the same size as if this \begin{figure} was directly in that new document, correct? Nov 1, 2012 at 23:17
• @DavidDoria: figure floats! I want it to sink sometimes ;-)
– user11232
Nov 1, 2012 at 23:22
• @Harish Kumar Haha sorry, I think I missed your joke? You're saying that behavior is indeed bizarre and would never be wanted? Also, did I make the situation of trying to use these outputs clear? Nov 1, 2012 at 23:32
• @DavidDoria: It all depends on how you input!. If you use pdf files then you should properly scale/size the pictures. Or you may simply use \input{figure-file.tex}. Here font attributes will blend seemlessly with those of main document. :-)
– user11232
Nov 1, 2012 at 23:52