TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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}
share|improve this question
    
have a look at align-captions-of-subfigures – cmhughes Nov 20 '12 at 1:03
    
floatrow clashes with memoir. And I can't see a way to align several rows horizontally, I want several different sized subfigures aligned both horizontally and vertically – vonbrand Nov 20 '12 at 12:33
    
did you see my answer to in the linked question? it uses subcaption, not floatrow – cmhughes Nov 20 '12 at 15:57
    
subcaption should not be used with memoir either, @cmhughes – Mr. V Nov 28 '13 at 9:32

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

share|improve this answer
1  
This is a lot of code for something memoir should do "out of the box", to be honest. – Mr. V Nov 28 '13 at 9:30

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.

share|improve this answer
    
using \centering is usually preferable to the center environment; the center environment adds extra vertical space (as you saw); see should-i-use-center-or-centering-for-figures-and-tables for reference – cmhughes Nov 23 '12 at 5:56

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.