3

I want to place two tikz pictures next to each other, both of them should be figures I can reference to. Please consider the following MWE:

\documentclass{scrartcl}
\usepackage{tikz}
\begin{document} 
    \begin{figure}
        \centering
        \begin{minipage}{.5\textwidth}
            \centering
            \begin{tikzpicture}
                \draw [fill=black] (0,0) rectangle (1,1);
            \end{tikzpicture}
            \captionof{figure}{a square}
            \label{fig:square}
        \end{minipage}%
        \begin{minipage}{.5\textwidth}
            \centering
            \begin{tikzpicture}
                \draw [fill=black] (0,0) rectangle (1,3);
            \end{tikzpicture}
            \captionof{figure}{a rectangle}
            \label{fig:rect}
        \end{minipage}%
    \end{figure}

    Figures \ref{fig:square} and \ref{fig:rect} demonstrate the difference
    between a square and a rectangle.

\end{document}

which looks like

screenshot1

I need both pictures to be aligned at the top, but the captions to be on the same height as well:

enter image description here

Can you please tell me how those alignments can be realized without loosing the ability to reference the single pictures?

3 Answers 3

7

Here is a solution using minipage alignment, baseline option of TikZ picture and the special node current bounding box.

\documentclass{scrartcl}
\usepackage{tikz}
\begin{document} 
\begin{figure}
  \begin{minipage}[t]{.5\linewidth}
    \centering
    \begin{tikzpicture}[baseline=(current bounding box.north)]
      \draw [fill=black] (0,0) rectangle (1,1);
    \end{tikzpicture}
  \end{minipage}%
  \begin{minipage}[t]{.5\linewidth}
    \centering
    \begin{tikzpicture}[baseline=(current bounding box.north)]
      \draw [fill=black] (0,0) rectangle (1,3);
    \end{tikzpicture}
  \end{minipage}
  \begin{minipage}[t]{.5\linewidth}
    \caption{a square}
    \label{fig:square}
  \end{minipage}%
  \begin{minipage}[t]{.5\linewidth}
    \caption{a rectangle}
    \label{fig:rect}
  \end{minipage}
\end{figure}
Figures \ref{fig:square} and \ref{fig:rect} demonstrate the difference
between a square and a rectangle.
\end{document}

To use this code in a macro (as in Gonzalo Medina's answer), you may use the every picture style:

\newcommand\SideBySide[6]{
  \begin{figure}
    \tikzset{every picture/.style={baseline=(current bounding box.north)}}
    \begin{minipage}[t]{.5\linewidth}
      \centering #1%
    \end{minipage}%
    \begin{minipage}[t]{.5\linewidth}
      \centering #4%
    \end{minipage}
    \begin{minipage}[t]{.5\linewidth}
      \caption{#2}
      \label{#3}
    \end{minipage}%
    \begin{minipage}[t]{.5\linewidth}
      \caption{#5}
      \label{#6}
    \end{minipage}
  \end{figure}
}

Syntax:

\SideBySide{<figure1>}{<caption1>}{<label1>}{<figure2>}{<caption2>}{<label2>}

Example:

\SideBySide{
\begin{tikzpicture}
    \draw [fill=black] (0,0) rectangle (1,1);
  \end{tikzpicture}
}{a square}{fig:square}{
\begin{tikzpicture}
    \draw [fill=black] (0,0) rectangle (1,3);
  \end{tikzpicture}
}{a rectangle}{fig:rect}
1
  • I've deleted my previous comment and will do so with this one shortly. Commented Mar 16, 2014 at 2:09
3

Use the option [t] as

\begin{minipage}[t]{.5\textwidth}

for minipages.

Also, you have to fake and insert an empty rectangle to have both figures with same height. I don't see another option since they were drawn on independent way.

So I used \path (0,0) rectangle (1,-2); to push the square up.

enter image description here

\documentclass{scrartcl}
\usepackage{tikz}
\begin{document} 
    \begin{figure}
        \centering
        \begin{minipage}[t]{.5\textwidth}   %%%% [t] here
        \centering
        \begin{tikzpicture}
            \draw[fill=black] (0,0) rectangle (1,1);
            \path (0,0) rectangle (1,-2);            %% empty rectangle
        \end{tikzpicture}
        \captionof{figure}{a square}
        \label{fig:square}
    \end{minipage}%
        \begin{minipage}[t]{.5\textwidth}   %%%% [t] here
            \centering
            \begin{tikzpicture}
                \draw [fill=black] (0,0) rectangle (1,3);
            \end{tikzpicture}
            \captionof{figure}{a rectangle}
            \label{fig:rect}
        \end{minipage}%
    \end{figure}

    Figures \ref{fig:square} and \ref{fig:rect} demonstrate the difference
    between a square and a rectangle.

\end{document}
2
  • thank you for the answer, but the rectangle and square images were only an example, the actual images are complicated and partially generated code. I am looking for a way to achieve this no matter how the actual images look like
    – muffel
    Commented Mar 15, 2014 at 17:27
  • 1
    So I suggest you to insert your draws inside empty rectangles with the same bounding box. For example, both inside some \path (0,0) rectangle (6,6);.
    – Sigur
    Commented Mar 15, 2014 at 17:29
3

The code:

\documentclass{scrartcl}
\usepackage{tikz}

\newlength\mylen
\newlength\mylena
\newlength\mylenb
\newcommand\SideBySide[6]{%
  \settoheight\mylen{#1}%
  \settoheight\mylena{#1}%
  \settoheight\mylenb{#4}%
  \ifdim\mylenb>\mylena\relax%
    \setlength\mylen{\mylenb}%
  \fi%
  \begin{figure}
  \begin{minipage}[c][\mylen][t]{.5\textwidth}
    \centering#1
  \end{minipage}%
  \begin{minipage}[c][\mylen][t]{.5\textwidth}
    \centering#4
  \end{minipage}\par
  \begin{minipage}[t]{.5\textwidth}
    \caption{#2}
    \label{#3}
  \end{minipage}%
  \begin{minipage}[t]{.5\textwidth}
    \caption{#5}
    \label{#6}
  \end{minipage}%
  \end{figure}%
}

\begin{document} 

\SideBySide{%
\begin{tikzpicture}
    \draw [fill=black] (0,0) rectangle (1,1);
  \end{tikzpicture}%
}{a square}{fig:square}{%
\begin{tikzpicture}
    \draw [fill=black] (0,0) rectangle (1,3);
  \end{tikzpicture}%
}{a rectangle}{fig:rect}

Figures~\ref{fig:square} and~\ref{fig:rect} demonstrate the difference between a square and a rectangle. Figures~\ref{fig:circle} and~\ref{fig:squareb} demonstrate the difference between a square and a circle.


\SideBySide{%
\begin{tikzpicture}
    \draw [fill=cyan] (0,0) circle [radius=2cm];
  \end{tikzpicture}%
}{a circle}{fig:circle}{%
\begin{tikzpicture}
    \draw [fill=orange] (0,0) rectangle (1,1);
  \end{tikzpicture}%
}{another sqaure}{fig:squareb}

\end{document}

enter image description here

Explanation:

The idea is to use two sets of side-by-side minipages; the first one for the figures and the second one, for the captions; all minipages are top aligned.

I defined a command \SideBySide which measures the heights of both images and determines the maximum height; this height is then used as the fixed height for both minipages containing the figures. All calculations are made by the command, so no manual intervention is required. The syntax:

\SideBySide{<figure1>}{<caption1>}{<label1>}{<figure2>}{<caption2>}{<label2>}
3
  • Evaluate twice the code of figures can be problematic. Commented Mar 16, 2014 at 2:12
  • @PaulGaborit I don't quite understand your comment. Could you please elaborate? Commented Mar 16, 2014 at 2:16
  • If figure2 increments a counter, this counter is incremented twice: once to measure the height and once to draw the figure (with figure1, it is three times). Even without global effect, double or triple the computing time can be problematic. Commented Mar 16, 2014 at 8:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .