8

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);
    \fill (-0.5,-0.5) circle[radius=1.5pt];
    \fill (0.7,0.7) circle[radius=1.5pt];
    \end{tikzpicture}
}
\caption{Graphical interpretation of convex sets.}
\label{fig:convexSet}
\end{figure}

\end{document}

which results in: convexSet

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
  • 2
    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
7

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:

enter image description here

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);
    \fill (-0.5,-0.5) circle[radius=1.5pt];
    \fill (0.7,0.7) circle[radius=1.5pt];
    %\draw (current bounding box.south east) rectangle (current bounding box.north west);
    \end{tikzpicture}
}
\caption{Graphical interpretation of convex sets.}
\label{fig:convexSet}
\end{figure}
\end{document}

enter image description here

  • Do you mind if I use this code in a publicly posted document? – user217285 May 15 '16 at 21:00
  • @Nitin In general, all code on the site is published under a Creative Commons Attribution-Share Alike license (tex.stackexchange.com/help/licensing), so you are free to reuse it under the terms of that license. For my contribution, go right ahead. – Torbjørn T. May 15 '16 at 21:40

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.