20

I've some problems with aligning some drawings I made with TikZ: I want these two TikZ pictured below to be displayed side by side with the two captions being on the same horizontal level. Unfortunately, right now the caption of the second drawing appears well below the first caption because the second drawing is a bit larger. How can I add some blank space above the first drawing so that the two captions are vertically aligned?

\documentclass{article}
\usepackage{tikz}
\usepackage{caption}
\begin{document}
\begin{minipage}{0.6\textwidth}
\begin{center}
\begin{tikzpicture}
[level distance=20mm,
every node/.style={fill=blue!75,circle, draw, inner sep=2pt},
level 1/.style={sibling distance=40mm,nodes={fill=blue!60}},
level 2/.style={sibling distance=20mm,nodes={fill=blue!45}},
level 3/.style={sibling distance=10mm,nodes={fill=blue!35}}]
\node {5}
      child {node {2}}
      child {node {9}};
\end{tikzpicture}
\captionsetup{font=footnotesize}
\captionof{figure}{Caption 1}
\end{center}
\end{minipage}
\begin{minipage}{0.6\textwidth}
\begin{center}
\begin{tikzpicture}
[level distance=20mm,
every node/.style={fill=blue!75,circle, draw, inner sep=2pt},
level 1/.style={sibling distance=40mm,nodes={fill=blue!60}},
level 2/.style={sibling distance=20mm,nodes={fill=blue!45}},
level 3/.style={sibling distance=10mm,nodes={fill=blue!35}}]
\node {1}
      child {node {5}
         child {node {1}}
         child[missing]
      }
      child {node {8}};
\end{tikzpicture}
\captionsetup{font=footnotesize}
\captionof{figure}{Caption 2}
\end{center}
\end{minipage}

\end{document}
IronMan12
  • 201
  • Do the real captions have the same number of lines? My compliments for the really good minimal example! – egreg Jan 26 '14 at 22:09

2 Answers2

17

This is exactly what the \subcaptionbox command (from the subcaption package) was designed for:

\documentclass{article}
\usepackage{tikz}
\usepackage{caption}
\usepackage{subcaption}

\begin{document}

\begin{figure}
\captionsetup[subfigure]{font=footnotesize}
\centering
\subcaptionbox{Caption for first subfigure}[.5\textwidth]{%
\begin{tikzpicture}
[level distance=20mm,
every node/.style={fill=blue!75,circle, draw, inner sep=2pt},
level 1/.style={sibling distance=40mm,nodes={fill=blue!60}},
level 2/.style={sibling distance=20mm,nodes={fill=blue!45}},
level 3/.style={sibling distance=10mm,nodes={fill=blue!35}}]
\node {5}
      child {node {2}}
      child {node {9}};
\end{tikzpicture}}%
\subcaptionbox{Caption for the seconf figure. This will span several lines for the example; in fact, it will span three lines}[.5\textwidth]{\begin{tikzpicture}
[level distance=20mm,
every node/.style={fill=blue!75,circle, draw, inner sep=2pt},
level 1/.style={sibling distance=40mm,nodes={fill=blue!60}},
level 2/.style={sibling distance=20mm,nodes={fill=blue!45}},
level 3/.style={sibling distance=10mm,nodes={fill=blue!35}}]
\node {1}
      child {node {5}
         child {node {1}}
         child[missing]
      }
      child {node {8}};
\end{tikzpicture}}
\end{figure}

\end{document}

enter image description here

Gonzalo Medina
  • 505,128
14

One option is to use [b] position specifier to the minipages.

\documentclass{article}
\usepackage{tikz}
\usepackage{caption}
\begin{document}
\begin{minipage}[b]{0.6\textwidth}
\centering
\begin{tikzpicture}
[level distance=20mm,
every node/.style={fill=blue!75,circle, draw, inner sep=2pt},
level 1/.style={sibling distance=40mm,nodes={fill=blue!60}},
level 2/.style={sibling distance=20mm,nodes={fill=blue!45}},
level 3/.style={sibling distance=10mm,nodes={fill=blue!35}}]
\node {5}
      child {node {2}}
      child {node {9}};
\end{tikzpicture}
\captionsetup{font=footnotesize}
\captionof{figure}{Caption 1}
\end{minipage}
\begin{minipage}[b]{0.6\textwidth}
\centering
\begin{tikzpicture}
[level distance=20mm,
every node/.style={fill=blue!75,circle, draw, inner sep=2pt},
level 1/.style={sibling distance=40mm,nodes={fill=blue!60}},
level 2/.style={sibling distance=20mm,nodes={fill=blue!45}},
level 3/.style={sibling distance=10mm,nodes={fill=blue!35}}]
\node {1}
      child {node {5}
         child {node {1}}
         child[missing]
      }
      child {node {8}};
\end{tikzpicture}
\captionsetup{font=footnotesize}
\captionof{figure}{Caption 2}
\end{minipage}

\end{document}

enter image description here

You can also specify a height to the minipages and use \vfill after \end{tikzpicture} so that both the pictures are aligned at the top line: \begin{minipage}[b][0.28\textheight]{0.6\textwidth}

Constraint: If two captions span different number of lines, then first lines are not top aligned.

Using tabular*:

\documentclass{article}
\usepackage{tikz}
\usepackage{caption}
\begin{document}
\noindent
\begin{tabular*}{\textwidth}{@{}cc@{}}
\begin{minipage}{\dimexpr0.5\textwidth-2\tabcolsep}
\centering
\begin{tikzpicture}
[level distance=20mm,
every node/.style={fill=blue!75,circle, draw, inner sep=2pt},
level 1/.style={sibling distance=40mm,nodes={fill=blue!60}},
level 2/.style={sibling distance=20mm,nodes={fill=blue!45}},
level 3/.style={sibling distance=10mm,nodes={fill=blue!35}}]
\node {5}
      child {node {2}}
      child {node {9}};
\end{tikzpicture}
\end{minipage}% 
&
\begin{minipage}{\dimexpr0.5\textwidth-2\tabcolsep}
\centering
\begin{tikzpicture}
[level distance=20mm,
every node/.style={fill=blue!75,circle, draw, inner sep=2pt},
level 1/.style={sibling distance=40mm,nodes={fill=blue!60}},
level 2/.style={sibling distance=20mm,nodes={fill=blue!45}},
level 3/.style={sibling distance=10mm,nodes={fill=blue!35}}]
\node {1}
      child {node {5}
         child {node {1}}
         child[missing]
      }
      child {node {8}};
\end{tikzpicture}
\end{minipage}
\\
\begin{minipage}[t]{\dimexpr0.6\textwidth-2\tabcolsep}
\captionsetup{font=footnotesize}
\captionof{figure}{Caption 1}
\end{minipage}
&
\begin{minipage}[t]{\dimexpr0.4\textwidth-2\tabcolsep}
\captionsetup{font=footnotesize}
\captionof{figure}{Caption 2 is some long caption that may go for a second line}
\end{minipage}
\end{tabular*}%

\end{document}

enter image description here

You can also use p{...} column specifiers and regular tabular instead of tabular*.

Since you have used \captionof macro, I will presume that you won't try to float the pictures and hence attempts using subcaption and subfig will not be presented.