16

I have a float with two subfloats:

A float with two subfloats

I want the circles to be vertically aligned from their top such as in the following figure. But I don't want the subcaptions to be placed above the subfloats but vertically aligned below as in the figure above.

A float with two subfloats with subcaptions above them

I'm not bound to any package in particular (even if I used subfig for this examples). I have tried http://gicl.cs.drexel.edu/people/tjkopena/wiki/pmwiki.php?n=SWAT.VerticallyAligningSubfigures, http://www.howtotex.com/tips-tricks/vertical-alignment-of-subfigures/ and https://stackoverflow.com/questions/2328403/vertical-alignment-of-subfigures-latex without success.

\documentclass{article}

\usepackage{tikz}
\usepackage{subfig}

%\captionsetup[subfloat]{position=top}% Uncomment this to generate the second figure

\begin{document}

\begin{figure}
\centering
\subfloat[Small circle]{%
  \begin{tikzpicture}
    \draw circle (1.25cm) {};
  \end{tikzpicture}%
}
\qquad
\subfloat[Big circle]{%
  \begin{tikzpicture}
    \draw circle (2cm) {};
  \end{tikzpicture}%
}
\caption{Circles}
\end{figure}

\end{document}
9

As suggested by Mico in his answer it can be done with the floatrow package. To make subcaptions it depends on subcaption which depends on caption.

\documentclass{article}

\usepackage{tikz}
\usepackage{caption}
\usepackage{subcaption}
\usepackage{floatrow}
\usepackage{calc}% To calculate width for \FBwidth

\floatsetup{ 
  heightadjust=object,
  valign=t
}

\begin{document}

\begin{figure}
\ffigbox
{%
  \begin{subfloatrow}
    \ffigbox[\FBwidth+0.5cm]% Width of subfloat
    {%
      \begin{tikzpicture}
        \draw circle (1.25cm) {};
      \end{tikzpicture}%
    }
    {%
      \subcaption{Small circle}%
    }
    \ffigbox[\FBwidth+0.5cm]% Width of subfloat
    {%
      \begin{tikzpicture}
        \draw circle (2cm) {};
      \end{tikzpicture}%
    }
    {%
      \subcaption{Big circle}%
    }
  \end{subfloatrow}
}
{%
  \caption{Circles}%
}
\end{figure}

\end{document}

Vertically top-aligned circles with vertically aligned subcaptions below

  • 2
    floatrow isn't compatible with the memoir class. Is it possible to achieve this another way when using memoir? – hertzsprung Aug 2 '14 at 11:33
5

Not an automatic procedure, but not so difficult to use:

\documentclass{article}

\usepackage{tikz}
\usepackage{subfig}

\newcount\bsubfloatcount
\newtoks\bsubfloattoks
\newdimen\bsubfloatht

\makeatletter
\newcommand{\bsubfloat}[2][]{%
  \sbox\z@{#2}%
  \ifdim\bsubfloatht<\ht\z@
    \bsubfloatht=\ht\z@
  \fi
  \advance\bsubfloatcount\@ne
  \@namedef{bsubfloat\romannumeral\bsubfloatcount}{%
    \subfloat[#1]{\vbox to\bsubfloatht{\hbox{#2}\vfill}}}%
}
\newcommand{\resetbsubfloat}{\bsubfloatcount\z@\bsubfloatht=\z@}
\makeatother

\begin{document}

\begin{figure}
\centering
\bsubfloat[Small circle]{%
  \begin{tikzpicture}
    \draw circle (1.25cm) {};
  \end{tikzpicture}%
}
\bsubfloat[Big circle]{%
  \begin{tikzpicture}
    \draw circle (2cm) {};
  \end{tikzpicture}%
}
\bsubfloati\qquad\bsubfloatii
\caption{Circles}
\end{figure}

\end{document}

The \bsubfloat commands are just for doing the calculation, then you can use \bsubfloati, \bsubfloatii, \bsubfloatiii, \bsubfloativ and so on (they are reset on each figure environment) to set up the row of subfloats.

If you have multiple rows, you should use \resetbsubfloat between them and use again \bsubfloati and so on for the placement.

More automatic procedure

Here is one, it has some weaknesses, but it should work.

\documentclass{article}

\usepackage{tikz}
\usepackage{subfig}

\newtoks\bsubfloattoks
\newdimen\bsubfloatht

\makeatletter
\newenvironment{bsubfloatrows}[1][\quad]
  {\def\bsubfloatspace{#1}\resetbsubfloatrows
   \def\\{\printbsubfloatrow\resetbsubfloatrows\par
     \@ifnextchar[{\bsubfloatvspace}{}}%
   \def\bsubfloatvspace[##1]{\vspace{##1}}%
  }
  {\printbsubfloatrow}
\newcommand{\bsubfloat}[2][]{%
  \sbox\z@{#2}%
  \ifdim\bsubfloatht<\ht\z@
    \bsubfloatht=\ht\z@
  \fi
  \bsubfloattoks=\expandafter{\the\bsubfloattoks
    \bsubfloatspace\subfloat[#1]{\vbox to\bsubfloatht{\hbox{#2}\vfill}}}%
}
\newcommand\resetbsubfloatrows{\bsubfloatht\z@\bsubfloattoks={\@gobble}}
\newcommand{\printbsubfloatrow}{\the\bsubfloattoks}
\makeatother



\begin{document}

\begin{figure}
\centering
\begin{bsubfloatrows}[\hfill]
\bsubfloat[Small circle]{%
  \begin{tikzpicture}
    \draw circle (1.25cm) {};
  \end{tikzpicture}%
}
\bsubfloat[Big circle]{%
  \begin{tikzpicture}
    \draw circle (2cm) {};
  \end{tikzpicture}%
}
\\[36pt]
\bsubfloat[Small circle]{%
  \begin{tikzpicture}
    \draw circle (1cm) {};
  \end{tikzpicture}%
}
\bsubfloat[Big circle]{%
  \begin{tikzpicture}
    \draw circle (1.2cm) {};
  \end{tikzpicture}%
}
\end{bsubfloatrows}
\caption{Circles}
\end{figure}

\end{document}

As you see, subfloats are enclosed in an environment, where each row is processed separately as regards the height. The horizontal spacing between two subfloats is given as optional argument to the environment (that's one weakness).

3

A quick introductory remark: According to the latest edition of the l2tabu document, both the subfigure and subfig packages should no longer be used; instead, one should use the subcaption package (from the same author of the caption package).

The problem you describe might best be solved using the floatrow package, as it provides all kinds of ways of aligning both the contents of side-by-side (sub)figures and their respective captions. Unfortunately, I find the writing style of the package's manual rather hard to penetrate, so I wasn't able to come up with a working MWE. Hopefully you'll have better luck. :-)

  • Thanks for the tip! The floatrow manual was not an easy read but finally I got it working, see my answer. – N.N. Sep 26 '11 at 20:02
  • I'm delighted that my tip turned out to be so useful! I'll keep your answer among my "favorites." :-) – Mico Sep 26 '11 at 20:55
  • Why should the subfigure and subfig package no longer be used? Beside the hyperref support they are working fine and are well-documented. Furthermore they even work with document classes the caption package is not supporting - as opposite to the/my subcaption package. So IMHO l2tabu is wrong here. – Axel Sommerfeldt Sep 27 '11 at 6:23
  • You ask an excellent question -- I suggest you take it up with the author(s) of the l2tabu document. To be honest, I was a bit surprised when I saw that claim in the latest issue of l2tabu, but I thought it may nevertheless be important information for some people. – Mico Sep 27 '11 at 13:37
0

The question was solved by replacing subfig with another package.

For those of us that need or want to use subfig, here is my simple/hack solution after much frustration:

\documentclass{article}
\usepackage{subfig,tikz,adjustbox}

\begin{document}
\begin{figure}
\subfloat[Some caption]{\raisebox{5ex}[0pt][0pt]{\tikz \node [fill=green, minimum width=0.4\linewidth, minimum height=5ex] {};}}
\hfill
\subfloat[Some caption]{\tikz \node [fill=blue, minimum width=0.4\linewidth, minimum height=10ex] {};}
\end{figure}
\end{document}

\subfloat does not allow line breaks or some such which would allow adding some vertical space. Any attempt at increasing the height of the tikz picture will also move the caption. Using \raisebox is the only way I found to workaround these issues.

Furthermore, \raisebox should be given [0pt][0pt] to avoid any inner border.

Of course, this is a hack because you need to eyeball the vertical shift. But it was the only solution I could fathom if you want to keep using subfig.

If you use \includegraphics rather than a tikzpicture, see this answer of how to get this working more elegantly. Unfortunately, this won't work for tikzpicture, even when using \adjustbox. It moves the caption as well.

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.