[Tex/LaTex] tikz-figures in minipage: Captions at the same height and top alignment

minipagetikz-pgfvertical alignment

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?

Best Answer

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}