6

I am using the memoir class and I have two subfigures of different width that I would like to lay out vertically. Ideally, these two subfigures should be aligned so that

  • The widest subfigure is centered (to \textwidth).
  • Other subfigures have their right edge aligned to the right edge of the widest subfigure.

How can I achieve this? The code below can work as a starting point.

\documentclass{memoir}
\usepackage{lipsum}
\newsubfloat{figure}

\begin{document}
\lipsum[1]
\begin{figure}
\centering
\subbottom[]{\framebox{Narrow subfig}}
\\
\subbottom[]{\framebox{Wide subfigure with lots of stuff}}
\end{figure}
\end{document}

I first tried using a minipage inside the figure environment, where content inside the minipage was right justified. The problem was setting the width of the minipage to exactly the width of the widest subfigure; if that problem has a solution, I think that would work.

One solution would be to compile the figure as a separate file using standalone. I wish to avoid that.

josteinb
  • 299

2 Answers2

5

I took my answer at Looking for three images on top of each other with text underneath each, changed the figure (rule) widths to make them different, and added a [r] optional specifier to the \Shortstack for right alignment. The cited answer explains how the use of the \subcaptionbox variant in conjunction with a package like stackengine can allow great flexibility in the figure layout.

\documentclass{article}
\usepackage{hyperref}
\usepackage[usestackEOL]{stackengine}
\usepackage{subcaption}
\begin{document}
\begin{figure}[ht]
  \centering
  \def\figa{\rule{1.5in}{1.1in}}
  \def\figb{\rule{1.2in}{1.5in}}
  \def\figc{\rule{1in}{0.9in}}
  \def\capa{subfig a caption}
  \def\capb{subfig b caption}
  \def\capc{subfig c caption which may be longer}
  \savestack{\capfiga}{\subcaptionbox{\capa\label{fg:a}}{\figa}}
  \savestack{\capfigb}{\subcaptionbox{\capb\label{fg:b}}{\figb}}
  \savestack{\capfigc}{\subcaptionbox{\capc\label{fg:c}}{\figc}}
  \setstackgap{S}{12pt}
  \Shortstack[r]{\capfiga\\ \capfigb\\ \capfigc}%
  \caption{This is my figure\label{fg:}}
\end{figure}
In figure \ref{fg:}, \ref{fg:a}, \ref{fg:b} and \ref{fg:c}...
\end{document}

enter image description here

To amplify on the flexibility I tout, one merely add the specifier \def\useanchorwidth{T} anywhere prior to the \Shortstack and the bottom (narrow) image of the stack will be centered (rather than the widest image), with the others protruding left:

enter image description here

3

You can use the varwidth environment from the package with the same name.

enter image description here

\documentclass{memoir}
\usepackage{lipsum,varwidth}
\newsubfloat{figure}

\begin{document}
\lipsum[1]
\begin{figure}
\centering
\begin{varwidth}{\linewidth}
\raggedleft
\subbottom[]{\framebox{Narrow subfig}}
\\
\subbottom[]{\framebox{Wide subfigure with lots of stuff}}
\end{varwidth}
\end{figure}
\end{document}
Torbjørn T.
  • 206,688
  • Simple, and exactly the effect I was looking for. Thanks! – josteinb Apr 08 '14 at 11:19
  • I am afraid I was a little too quick: The subfigures contain tikz figures. I get incorrect alignment for those, and the warning "Package varwidth Warning: Failed to reprocess entire contents". Is varwidth not applicable for tikz pictures somehow? – josteinb Apr 08 '14 at 11:35
  • @josteinb Worked fine here with a very simple tikzpicture (just a rectangle), can you incorporate the tikzpictures in your MWE? The following worked: \begin{varwidth}{\linewidth} \raggedleft \subbottom[]{\framebox{\begin{tikzpicture} \draw (0,0) rectangle (2,1); \end{tikzpicture}}} \\ \subbottom[]{\framebox{\begin{tikzpicture} \draw (0,0) rectangle (5,1); \end{tikzpicture}}} \end{varwidth} – Torbjørn T. Apr 08 '14 at 11:46
  • I also made it work in the MWE with a simple tikzpicture. However, the "real" figure still causes problems. I will have to do some more testing to figure out exactly what is causing the problem. – josteinb Apr 08 '14 at 16:50
  • @josteinb You also could post the code of a troublesome tikzpicture if you like. – Torbjørn T. Apr 08 '14 at 17:21
  • @josteinb Any luck making a new MWE? – Torbjørn T. Apr 24 '14 at 08:15
  • No, unfortunately. It would have required some sort of bisection, which I just can't find time for at the moment. Aligning the tikzpictures proved to be easier by simply creating one large tikzpicture and specifying the alignment manually. – josteinb Apr 26 '14 at 09:12