3

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}
vonbrand
  • 5,473

3 Answers3

2

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

egreg
  • 1,121,712
0

Based on egreg's answer, making use of memoir's "out-of-the-box" subfloat features and etoolbox to automate the answer a little more:

\documentclass{memoir}
\usepackage{pgf,etoolbox}
\newsubfloat{figure}
\newcommand{\fourimagesbox}[3]{\begin{minipage}[c][#2]{#3}
                                \centering
                                \makebox{\copy#1}
                                \end{minipage}
}

\begin{document}
  \begin{figure}[htbp]
    \setbox1=\hbox{\includegraphics[width=.3\linewidth]{example-image-a}}
    \setbox2=\hbox{\includegraphics[width=.35\linewidth]{example-image-b}}
    \setbox3=\hbox{\includegraphics[width=.45\linewidth]{example-image-c}}
    \setbox4=\hbox{\includegraphics[width=.25\linewidth]{example-image}}
    \newlength{\himgskip}\setlength{\himgskip}{0pt}
    \newlength{\vimgskip}\setlength{\vimgskip}{0pt}
    \newlength{\subcapskip}\setlength{\subcapskip}{0pt}
    \newlength{\lwidth}\deflength{\lwidth}{\ifdimgreater{\wd1}{\wd3}{\wd1}{\wd3}}
    \newlength{\rwidth}\deflength{\rwidth}{\ifdimgreater{\wd2}{\wd4}{\wd2}{\wd4}}
    \newlength{\theight}\deflength{\theight}{\ifdimgreater{\ht1}{\ht2}{\ht1}{\ht2}+\subcapskip}
    \newlength{\bheight}\deflength{\bheight}{\ifdimgreater{\ht3}{\ht4}{\ht3}{\ht4}+\subcapskip}
    \centering
    \subbottom[Box 1]{\fourimagesbox{1}{\theight}{\lwidth}}\hspace{\himgskip}
    \subbottom[Box 2]{\fourimagesbox{2}{\theight}{\rwidth}}\vspace{\vimgskip}
    \subbottom[Box 3]{\fourimagesbox{3}{\bheight}{\lwidth}}\hspace{\himgskip}
    \subbottom[Box 4]{\fourimagesbox{4}{\bheight}{\rwidth}}
    \caption{Some boxen}
  \end{figure}
\end{document}

This way both images and captions are aligned and some lengths were created for user adjustion purposes.

0

The weird misalignment inside columns goes away if each minipage is like

\begin{minipage}[c]{1.0\wd2}
  \centering
  \makebox{\copy7}
\end{minipage}

Using minipage's [c] is a great way to align side-by-side figures vertically.

vonbrand
  • 5,473