# Alignment of TikZ pictures in subfigures

I have made two figures using TikZ that I would like to place side by side. This is done in this MWE:

\documentclass{memoir}
\usepackage{graphicx}
\usepackage{tikz}
\newsubfloat{figure}

\begin{document}

\begin{figure}[htbp]
\centering
\subbottom[Convex set]{
\begin{tikzpicture}
\draw[rotate=-45,fill=gray!30] (0,0) ellipse (30pt and 45pt);
\end{tikzpicture}
}
\hspace{1cm}
\subbottom[Non-convex set]{
\begin{tikzpicture}
\draw[fill=gray!30] (0,0) to [out=140,in=90] (-1,-1)
to [out=-90,in=240] (0.8,-0.6)
to [out=60,in=-60] (1.2,1.2)
to [out=120,in=90] (0.3,0.7)
to [out=-90,in=20] (0.3,0)
to [out=200,in=-40] (0,0);
\draw (-0.5,-0.5) -- (0.7,0.7);
\end{tikzpicture}
}
\caption{Graphical interpretation of convex sets.}
\label{fig:convexSet}
\end{figure}

\end{document}


which results in:

The problem is that neither the top, center, or bottom of the subfigures are aligned. How is this resolved? And what causes this misalignment? A faulty use of the TikZ coordinate system?

• Try to add the option \begin{tikzpicture}[baseline,remember picture] for both tikzpictures – JMP Apr 5 '16 at 9:37
• The size of your figures are different in different directions from origo. Drawing the help lines helps (pun intended), as you give both pictures dimensions from (-2,-2) to (2,2). If you don't want them, an easy solution can be to add \path (-2,-2) grid (2,2); to both pictures. But there's bound to be a more elegant way, I believe. – Holene Apr 5 '16 at 9:45
• @JMP It does not work, since your solution messes with the alignment of the captions. – Thomas Apr 5 '16 at 9:53
• @Holene Thanks for the solution. It requires some messy estimation of the vertical spacing, but it work. As you say, a more elegant solution probably exists, which is still desired. – Thomas Apr 5 '16 at 9:55
• @Thomas do you mind if I use your code in a publicly posted document? – user217285 May 15 '16 at 20:32

I think the to paths use some control points that extend the bounding box of the second figure. You can see this by adding \draw (current bounding box.south east) rectangle (current bounding box.north west); at the end of each tikzpicture, which gives you this:

As you can see there is some extra whitespace around the second one. I don't know of an automatic fix for this, but you can set the bounding box manually with \useasboundingbox:

\documentclass{memoir}
\usepackage{graphicx}
\usepackage{tikz}
\newsubfloat{figure}

\begin{document}

\begin{figure}[htbp]
\centering
\subbottom[Convex set]{
\begin{tikzpicture}
\draw[rotate=-45,fill=gray!30] (0,0) ellipse (30pt and 45pt);
%\draw (current bounding box.south east) rectangle (current bounding box.north west);
\end{tikzpicture}
}
\hspace{1cm}
\subbottom[Non-convex set]{
\begin{tikzpicture}
\useasboundingbox (-1,-1.35) rectangle (1.5,1.35); % values found by trial and error
\draw[fill=gray!30] (0,0) to [out=140,in=90] (-1,-1)
to [out=-90,in=240] (0.8,-0.6)
to [out=60,in=-60] (1.2,1.2)
to [out=120,in=90] (0.3,0.7)
to [out=-90,in=20] (0.3,0)
to [out=200,in=-40] (0,0);
\draw (-0.5,-0.5) -- (0.7,0.7);