[Tex/LaTex] Aligning subfigures both horizontally and vertically with memoir

horizontal alignmentmemoirsubfloatsvertical alignment

I'm trying to place 4 subfigures of different sizes aligned. Here the box-k.pdf files contain boxes with marks at the midsides. The result has box-1 and box-3 aligned at the left margin, not centered (but the captions are centered).

texlive-2012, as distributed for Fedora.

\documentclass{memoir}
\usepackage{pgf}
\usepackage{subfig}

\begin{document}
  \begin{figure}[htbp]
    \setbox1=\hbox{\pgfimage{box-1}}
    \setbox2=\hbox{\pgfimage{box-2}}
    \setbox3=\hbox{\pgfimage{box-3}}
    \setbox4=\hbox{\pgfimage{box-4}}
    \centering
    \subfloat[Box 1]{
      \begin{minipage}[c]{1.0\wd3}
        \begin{center}
          \copy1
        \end{center}
      \end{minipage}
    }
    \hspace{4em}
    \subfloat[Box 2]{
      \begin{minipage}[c]{1.0\wd4}
         \begin{center}
            \copy2
         \end{center}
      \end{minipage}
    }
    \\[2ex]
    \subfloat[Box 3]{
      \begin{minipage}[c][1.0\ht4]{1.0\wd3}
        \begin{center}
          \copy3
        \end{center}
      \end{minipage}
    }
    \hspace{4em}
    \subfloat[Box 4]{
      \begin{minipage}[c]{1.0\wd4}
        \begin{center}
          \copy4
        \end{center}
      \end{minipage}
    }
    \caption{Some boxen}
  \end{figure}
\end{document}

Best Answer

You should compute the width and heights, so as to catch the maximum and minimum widths:

\documentclass{memoir}
\usepackage{graphicx}
\usepackage[caption=false]{subfig}
\newlength{\alignheight}
\newlength{\alignwidth}
\newcommand{\fakeheight}[3]{%
  \makebox[#1][c]{\rule[-.5\dimexpr#2\relax]{0pt}{#2}\raisebox{-.5\height}{#3}}%
}

\begin{document}
\begin{figure}[htbp]

\sbox0{\includegraphics[width=3cm,height=2.5cm]{example-image-a}}
\setlength\alignwidth{\wd0}
\setlength\alignheight{\ht0}
\sbox2{\includegraphics[width=4cm,height=3cm]{example-image-b}}
\ifdim\wd2>\alignwidth \setlength\alignwidth{\wd2}\fi
\ifdim\ht2>\alignheight \setlength\alignheight{\ht2}\fi
\sbox4{\includegraphics[width=5cm,height=2cm]{example-image-c}}
\ifdim\ht4>\alignwidth \setlength\alignwidth{\wd4}\fi
\ifdim\ht2>\alignheight \setlength\alignheight{\ht4}\fi
\sbox6{\includegraphics[width=3.5cm,height=3.5cm]{example-image}}
\ifdim\ht6>\alignwidth \setlength\alignwidth{\wd6}\fi
\ifdim\ht2>\alignheight \setlength\alignheight{\ht6}\fi

\centering
  \subfloat[Box 1]{\fakeheight{\alignwidth}{\alignheight}{\usebox0}}\hspace{4em}%
  \subfloat[Box 2]{\fakeheight{\alignwidth}{\alignheight}{\usebox2}}\\[2ex]
  \subfloat[Box 3]{\fakeheight{\alignwidth}{\alignheight}{\usebox4}}\hspace{4em}%
  \subfloat[Box 4]{\fakeheight{\alignwidth}{\alignheight}{\usebox6}}
\caption{Some boxen}
\end{figure}
\end{document}

The command \pdfimage is deprecated by the same PGF package, in favor of \includegraphics. Of course here the maximum width and height are known, but I just used the explicit dimensions to mock up an example.

enter image description here

Related Question