3

I have two tikzpictures next to each other and I want to align them with respect to a baseline, but also align the captions beneath. Originally, the figures were vertically centered, meaning that neither the baseline nor the captions were aligned (the second figure is taller). I have seen a couple of answers here that address one of these issues, but not both at the same time:

  • One solution for caption alignment was to use a savebox to store the height of the larger image and use that to align the smaller one. It centered figures while aligning captions, but I need to align figures according to a baseline.
  • Using subcaptionbox instead of subfigure environment aligned captions, but ignored baseline.
  • Using baseline option on the tikzpicture environment aligned my grids, but not captions.
  • Using the t option on subfigure is interesting and is explained further below.

Here is a small example that goes half-way (I couldn't find out how to render the result into this question though):

\documentclass{article}

\usepackage{tikz}
\usepackage{subcaption}

\begin{document}

\begin{figure}[tb]
    \begin{subfigure}[t]{.5\textwidth}
        \centering
        \begin{tikzpicture}[x=0.5cm, y=0.5cm, baseline=(head.base)]
            \draw[step=1.0] (0,0) grid (10,1);
            \node (head) at (0,-0.5) {head};
            \node (b1_lower) at (1.5,0) {};
            \draw[->, bend right] (head) to (b1_lower);
        \end{tikzpicture}
        \caption{Caption A.}
    \end{subfigure}
    \begin{subfigure}[t]{.5\textwidth}
        \centering
        \begin{tikzpicture}[x=0.5cm, y=0.5cm, baseline=(head.base)]
            \draw[step=1.0] (0,0) grid (10,1);
            \node (head) at (0,-0.5) {head};
            \node (b4_lower) at (4.5,0) {};
            \draw[->, out=0, in=-90] (head) to (b4_lower);
            \node at (4,2.5) {tall figure};
            \node at (4,-2.5) {tall figure};
        \end{tikzpicture}
        \caption{Caption B.}
    \end{subfigure}
    \caption{Some other caption concerning the figure in its entirety.}
\end{figure}
\end{document}

If I understand correctly, the t option of subfigure aligns using the baseline, which works, although the captions are not aligned. If I change to b instead, the captions are aligned, but the baseline is ignored. The funny thing is that in the latter case, the size of the second figure is taken into consideration because the captions are on the same level, so I don't understand why caption A can't just be brought down a bit.

My questions are:

  • Both alignments work by themselves, but how do I apply them at the same time (i.e., I want the grids on the same level, and caption A brought down to that of caption B)?
  • What's the explanation as to why the current approach doesn't result in my desired behaviour?
3

Subfigures are basically minipages and only have one baseline for the image and caption combined. This solution uses a tabular instead.

Note: Captions like to be inside minipages. More precisely, they expand to \textwidth and use \par. The \leavevmode restores the gap between the figure and caption.

\documentclass{article}

\usepackage{tikz}
\usepackage{subcaption}

\begin{document}

\begin{figure}[tb]
    \centering
    \begin{tabular}{cc}
        \begin{tikzpicture}[x=0.5cm, y=0.5cm, baseline=(head.base)]
            \draw[step=1.0] (0,0) grid (10,1);
            \node (head) at (0,-0.5) {head};
            \node (b1_lower) at (1.5,0) {};
            \draw[->, bend right] (head) to (b1_lower);
        \end{tikzpicture}
        &
        \begin{tikzpicture}[x=0.5cm, y=0.5cm, baseline=(head.base)]
            \draw[step=1.0] (0,0) grid (10,1);
            \node (head) at (0,-0.5) {head};
            \node (b4_lower) at (4.5,0) {};
            \draw[->, out=0, in=-90] (head) to (b4_lower);
            \node at (4,2.5) {tall figure};
            \node at (4,-2.5) {tall figure};
        \end{tikzpicture}
        \\
        \begin{minipage}{.45\textwidth}
          \leavevmode\subcaption{Caption A.}
        \end{minipage}&
        \begin{minipage}{.45\textwidth}
          \leavevmode\subcaption{Caption B.}
        \end{minipage}
    \end{tabular}
    \caption{Some other caption concerning the figure in its entirety.}
\end{figure}
\end{document}

demo


Actually, the tabular really isn't doing anything useful since the widths are set by the caption minipages.

\documentclass{article}

\usepackage{tikz}
\usepackage{subcaption}

\begin{document}

\begin{figure}[tb]
    \centering
    \begin{tikzpicture}[x=0.5cm, y=0.5cm, baseline=(head.base)]
        \draw[step=1.0] (0,0) grid (10,1);
        \node (head) at (0,-0.5) {head};
        \node (b1_lower) at (1.5,0) {};
        \draw[->, bend right] (head) to (b1_lower);
    \end{tikzpicture}\hfil
    \begin{tikzpicture}[x=0.5cm, y=0.5cm, baseline=(head.base)]
        \draw[step=1.0] (0,0) grid (10,1);
        \node (head) at (0,-0.5) {head};
        \node (b4_lower) at (4.5,0) {};
        \draw[->, out=0, in=-90] (head) to (b4_lower);
        \node at (4,2.5) {tall figure};
        \node at (4,-2.5) {tall figure};
    \end{tikzpicture}\par
    \begin{minipage}{.45\textwidth}
      \leavevmode\subcaption{Caption A.}
    \end{minipage}\hfil
    \begin{minipage}{.45\textwidth}
      \leavevmode\subcaption{Caption B.}
    \end{minipage}
    \caption{Some other caption concerning the figure in its entirety.}
\end{figure}
\end{document}
  • I like the updated solution because it's not too verbose and automagically fixes alignment without resorting to tweaks. I now also understand why my original approach didn't work. Much appreciated. On another note, what do you (and others) use to generate the pictures embedded in the answer? – Andreas Flöjt Sep 24 '16 at 15:01
  • I use acrobat to display and convert PDF to PNG. Then I use the GIMP photo editor to crop it. There is a button to add it here. – John Kormylo Sep 24 '16 at 21:05
1

Define the bounding box in both pictures explicitely, making it big enough to let it contain all picture elements in both pictures. E.g., if you add the line

\draw[draw=none, use as bounding box] (-0.5,-3.5) rectangle (10,2.5);

in both pictures, you obtain

enter image description here

The important number is -3.5 since it defines how far the bounding box extends to the south.

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.