# 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. –  Peter Grill Oct 31 '12 at 17:13
@PeterGrill - Ah, getting closer! Please see my edit in the question for a problem with using subfig's subfloat with 'standalone'. –  David Doria Oct 31 '12 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. –  Peter Grill Oct 31 '12 at 18:01
Ooops, wrong thread. –  David Doria Oct 31 '12 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}


-
Perfect, thanks! –  David Doria Nov 1 '12 at 0:01
@DavidDoria: Besides accepting answers you should also upvote answers/questions that you find useful. –  Peter Grill Nov 1 '12 at 0:03
@Harish Kumar - actually - the (a) seems quite a bit bigger than the (b) there (and in my real work). Any suggestions on how to make them the same size? –  David Doria Nov 1 '12 at 19:52
@DavidDoria: See the updated answer. –  Harish Kumar Nov 1 '12 at 22:51
@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? –  David Doria Nov 1 '12 at 23:17