4

I have some complicated tikz pictures that I don't want to manually resize (therefore tikzscale) - but putting several in a figure doesn't seem to work with \subcaptionbox. It does work with subfigure (also from the subcaption package), but then the captions are not aligned if the number of lines per caption differ.

For the MWE one needs to create a square.tikz-file with the following content (obviously this is a simplification to illustrate the problem ;-))

\begin{tikzpicture}
\draw (-10,-10) rectangle (10,10);
\end{tikzpicture}

Then the code is

\documentclass{article}
\usepackage[english]{babel}
\usepackage{tikz}
\usepackage{tikzscale}
\usepackage[justification=centering]{subcaption}

\begin{document}

\begin{figure}
    \subcaptionbox{One-line caption}[0.5\textwidth]{\includegraphics[width=\textwidth]{square.tikz}}
    \subcaptionbox{Two-line caption \\ is aligned}[0.5\textwidth]{\includegraphics[width=\textwidth]{square.tikz}}
    \caption{With \texttt{subcaptionbox}}
\end{figure}

\begin{figure}
    \begin{subfigure}{0.5\textwidth}
        \includegraphics[width=\textwidth]{square.tikz}
        \caption{One-line caption}
    \end{subfigure}
    \begin{subfigure}{0.5\textwidth}
        \includegraphics[width=\textwidth]{square.tikz}
        \caption{Two-line caption; \\ should be aligned with one-line caption}
    \end{subfigure}
    \caption{With \texttt{subfigure}}
\end{figure}

\end{document}
  • \includegraphics[width=\textwidth]{square.tikz} is better be \includegraphics[width=\linewidth]{square.tikz}. – user11232 Jan 8 '14 at 12:51
5

The subfigure environment is actually a minipage where \textwidth is reset to the stated width. The same is not true for \subcaptionbox; the optional argument to \subcaptionbox is not required, if you just want it has the same dimension as the object inside.

\begin{filecontents*}{square.tikz}
\begin{tikzpicture}
\draw (-10,-10) rectangle (10,10);
\end{tikzpicture}
\end{filecontents*}

\documentclass{article}
\usepackage[english]{babel}
\usepackage{tikz}
\usepackage{tikzscale}
\usepackage[justification=centering]{subcaption}

\begin{document}

\begin{figure}
\centering
\subcaptionbox{One-line caption}{%
  \includegraphics[width=.45\textwidth]{square.tikz}%
}\hfill
\subcaptionbox{Two-line caption \\ is aligned}{%
  \includegraphics[width=.45\textwidth]{square.tikz}%
}

\caption{With \texttt{subcaptionbox}}
\end{figure}

\end{document}

enter image description here

  • Thanks a lot! Since subfigure needs a width (and includegraphics needs to do the work of resizing for tikzscale), it didn't occur to me to use subcaptionbox without the optional argument. It just caught me off-guard that the same construction didn't work - I guess I should have experimented more before asking. – Axel Jan 8 '14 at 12:45

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.