2

I have the same issue as in the thread Crop a float containing subfigures horizontally with Preview or Standalone package but I want to place a subcaption next to the subfigure numbering. However this causes some strange behavior (see example). When I change the documentclass to article it works as expected.

Modified example:

\documentclass[varwidth]{standalone}

\usepackage{mwe}
\usepackage[labelfont=footnotesize,labelformat=parens,
justification=centering]{caption,subfig}

\begin{document}

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

enter image description here

  • I'm not sure how the [varwidth] option woks, but it seems to have a number of peculiar results. Put the figure inside a minipage (default article is 345pt wide) instead. This will also show you when you figure is too wide. – John Kormylo Sep 13 '16 at 15:00
  • @JohnKormylo I put the figure inside a minipage but it changed only the width of the output. The colon is still present. – hzhr Sep 13 '16 at 16:01
  • Did you lose the [varwidth] option? – John Kormylo Sep 13 '16 at 22:32
  • @JohnKormylo Thank you for your answer. Removing the [varwidth] option fixes the formatting and colon but now the pdf page size does not fit with the figure size. – hzhr Sep 14 '16 at 8:37
  • BTW, (almost) every time you end a line with { or } instead of {% or }% you wind up adding a space (hence the gaps around your images). – John Kormylo Sep 16 '16 at 12:33
1

I tried to create my own version of varwidth. The hard part was getting it to work on things like \newline (not actually used here). \\ seems to work, but generates an error message.

As far as I can tell, \FigSize does nothing. It is certainly not used by the subfig package.

\documentclass{standalone}

\usepackage{mwe}
\usepackage[labelfont=footnotesize,labelformat=parens,
justification=centering]{caption,subfig}
\usepackage{environ}

\newlength{\maxwidth}

\makeatletter
\NewEnviron{fakefigure}[1][]{\def\@captype{figure}%
  \def\newline{\egroup% end of previous \sbox1
    \ifdim\wd1>\maxwidth \global\maxwidth=\wd1\fi
    \sbox1\bgroup}
  \let\par=\newline
  \sbox1\bgroup\BODY\egroup
  \ifdim\wd1>\maxwidth \global\maxwidth=\wd1\fi
  \setcounter{subfigure}{0}}
\makeatother

\newcommand{\fakecaption}[2][]{\relax}

\NewEnviron{varwidth}{\bgroup% compute max width
  \let\figure=\fakefigure
  \let\endfigure=\endfakefigure
  \let\caption=\fakecaption
  \global\maxwidth=0pt
  \sbox0{\BODY}%
  \ifdim\wd0>\maxwidth \global\maxwidth=\wd0\fi
\egroup% restore defaults
\begin{minipage}{\maxwidth}
\BODY
\end{minipage}}

\begin{document}
\begin{varwidth}
\def\FigSize{.23}% no known function
\begin{figure}[htbp!]
  \centering
  \subfloat[A]
  {
    \includegraphics[width=3cm]{example-image-a}
  }
  \subfloat[B]
  {
    \includegraphics[width=3cm]{example-image-b}
  }
\end{figure}
\end{varwidth}
\end{document} 

demo

| improve this answer | |
  • Thank you! It's a bit beyond my TeX level but it works. :) – hzhr Sep 16 '16 at 8:18
1

The varwidth package redefines the LaTeX kernel macros \@bsphack and \@esphack in a way incompatible to the original definition. Especially \@bsphack opens a TeX group and \@esphack closes a TeX group now. Unfortunately \captionsetup uses these macros internally so it doesn't matter what caption options you set, they are always set within an own group now and therefore are worthless since at the end of \captionsetup the options are gone when the varwidth package (which is actually more a dirty hack than a package) is used.

An example document:

\documentclass[varwidth]{standalone}
\usepackage{caption}
\begin{document}
\begin{figure}
  \captionsetup{textfont=bf,labelsep=space,labelformat=parens}% This is all without effect!
  \caption{Hello!}
\end{figure}
\end{document}

If anybody is interested in a fix, please fill a bug report at https://gitlab.com/axelsommerfeldt/caption/issues and I'll adapt the caption package to the varwidth package within the very next days.

| improve this answer | |
0

Just a note, subcaption works with \pbox.

Embracing the figure inside a \pbox should work, but not in standalone. There is an additional : with varwidth

\documentclass[a4paper]{article}

\usepackage{mwe}
\usepackage{varwidth,pbox}
\usepackage[labelformat=simple]{subcaption}
\renewcommand{\thesubfigure}{(\alph{subfigure})}

\begin{document}

\begin{figure} % Figure larger than the page
\centering
\makebox[\textwidth][c]{\mbox{%
\begin{subfigure}{8cm}
\includegraphics[width=\textwidth]{example-image-a}
\subcaption{A}
\end{subfigure}
\begin{subfigure}{8cm}
\includegraphics[width=\textwidth]{example-image-b}
\subcaption{B}
\end{subfigure}
}}
\caption{fig}
\end{figure}

\begin{figure} % Figure larger than the page
\centering
\makebox[\textwidth][c]{\pbox{1.3\textwidth}{%
\begin{subfigure}{8cm}
\includegraphics[width=\textwidth]{example-image-a}
\subcaption{A}
\end{subfigure}
\begin{subfigure}{8cm}
\includegraphics[width=\textwidth]{example-image-b}
\subcaption{B}
\end{subfigure}
}}
\caption{fig}
\end{figure}

\begin{figure} % Figure larger than the page
\centering
\makebox[\textwidth][c]{\begin{varwidth}{1.3\textwidth}
\begin{subfigure}{8cm}
\includegraphics[width=\textwidth]{example-image-a}
\subcaption{A}
\end{subfigure}
\begin{subfigure}{8cm}
\includegraphics[width=\textwidth]{example-image-b}
\subcaption{B}
\end{subfigure}
\end{varwidth}}
\caption{fig}
\end{figure}
\end{document}

enter image description here enter image description here

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.