[Tex/LaTex] How to align both baseline and caption in a horizontal tikzpicture subcaption

subcaptiontikz-pgf

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?

Best Answer

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}