12

I use this code to display two pictures:

\begin{figure}[htbp]
\minipage{0.45\textwidth}
\begin{center}
\includegraphics[width=1\textwidth]{methods.png}
\caption{Methods}
\label{fg:methods}
\end{center}
\endminipage\hfill
\minipage{0.1\textwidth}
\begin{center}
\end{center}
\endminipage\hfill
\minipage{0.45\textwidth}
\begin{center}
\includegraphics[width=1\textwidth]{method_detail.png}
\caption{Method detail information}
\label{fg:method_detail}
\end{center}
\endminipage\hfill
\end{figure}

Now I want assure that both pictures are not wider than \textwidth. This I have already achieved. But I'd like that the picture with the higher height is reduced to the height of the smaller image (while preserving the aspect ratio). Then the whole width isn't needed any more for the higher picture, so I'd like to use this width for the smaller picture to make it wider.

I think I could do this by trying to use different width parameters in the includegraphics command, but isn't there a way to do that automatically?

  • 4
    Let me see if I understand correctly: you have two pictures, of arbitrary sizes, and you want them rendered to have the same height and then to take up the full width of the page - essentially, so that they are as big as possible with the constraint that they are of the same height (and on the same line) (and without changing the aspect size). Is that right? – Andrew Stacey Mar 09 '11 at 12:19
  • @Andrew Stacey: Yes that's correct! – RoflcoptrException Mar 09 '11 at 12:21

2 Answers2

12
\documentclass{article}
\usepackage{graphicx}
\newsavebox\IBoxA \newsavebox\IBoxB \newlength\IHeight
\newcommand\TwoFig[6]{% Image1 Caption1 Label1 Im2 Cap2 Lab2
  \sbox\IBoxA{\includegraphics[width=0.45\textwidth]{#1}}
  \sbox\IBoxB{\includegraphics[width=0.45\textwidth]{#4}}%
  \ifdim\ht\IBoxA>\ht\IBoxB
    \setlength\IHeight{\ht\IBoxB}%
  \else\setlength\IHeight{\ht\IBoxA}\fi
  \begin{figure}[!htb]
  \minipage[t]{0.45\textwidth}\centering
  \includegraphics[height=\IHeight]{#1}
  \caption{#2}\label{#3}
  \endminipage\hfill
  \minipage[t]{0.45\textwidth}\centering
  \includegraphics[height=\IHeight]{#4}
  \caption{#5}\label{#6}
  \endminipage 
  \end{figure}%
}

This is how you use it:

\begin{document}

  \TwoFig{methods} % image 1
         {Methods} % caption 1
         {fg:methods} % label 1
         {method_detail} % image 2
         {Method detail information} % caption 2
         {fg:method_detail} % label 2

\end{document}

enter image description here

  • I tried to modify your makro to use it with 3 pictures. I thought that would be too difficult, but i wasn't able to solve the problem. Do you have any hints? – RoflcoptrException Mar 10 '11 at 13:54
  • @Roflcoptr: you need a newsavebox\IBoxC and a newcommand\ThreeFig[9]{...} –  Mar 10 '11 at 16:08
  • Is it possible to do this with using the full textwidth (except a small space between the two pictures)? ...besides, the shown is very easy with \subfigure. – Phab Feb 20 '15 at 12:24
  • sure, it is possible –  Feb 21 '15 at 11:56
  • I noticed that the resulting images were not exactly the full \textwidth. I fixed that by setting the width of each \includegraphic to exactly 0.5 rather then 0.45 – lanoxx Dec 11 '16 at 13:35
  • I do not like it, if two images have no space between! –  Dec 11 '16 at 14:14
  • Well there is still the width of the mini pages, I have left that at .45. So in fact there is still a space between the two images. – lanoxx Dec 11 '16 at 14:38
  • @Herbert: I asked a new question, maybe you have some idea? http://tex.stackexchange.com/questions/343855/two-images-with-different-size-side-by-side-automatic-computation – lanoxx Dec 13 '16 at 15:35
10

This compares the heights of the two images and makes the taller one smaller as necessary.

\documentclass{article}
\usepackage{graphicx}
\begin{document}
\begin{figure}
        \setbox0\hbox{%
                \includegraphics[width=.45\textwidth]{methods}%
        }%
        \setbox2\hbox{%
                \includegraphics[width=.45\textwidth]{methodsdetail}%
        }%
        \ifdim\ht0>\ht2
                \setbox0\hbox{%
                        \includegraphics[height=\ht2]{methods}%
                }%
        \else
                \setbox2\hbox{%
                        \includegraphics[height=\ht0]{methodsdetail}%
                }%
        \fi
        \noindent
        \parbox{.45\textwidth}{%
                \centering
                \unhbox0
                \caption{Methods}
                \label{fg:methods}
        }%
        \hfil
        \parbox{.45\textwidth}{%
                \centering
                \unhbox2
                \caption{Method detail information}
                \label{fg:method_detail}
        }%
\end{figure}
\end{document}

Using some free clipart, without the \if...\fi, it looks like this. enter image description here

With the above code, it looks like this. enter image description here

TH.
  • 62,639
  • 1
    @Roflcoptr: And check out Herbert's answer (which is nearly identical to mine) to see how to abstract all of this away into an easy to use macro. – TH. Mar 09 '11 at 12:40
  • To be honest I thought I was writing this comment to Herbert's answer ;) Both answers are looking good but i prefer the macro way. – RoflcoptrException Mar 09 '11 at 12:42