Take the 2-minute tour ×
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.

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?

share|improve this question
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? –  Loop Space Mar 9 '11 at 12:19
    
@Andrew Stacey: Yes that's correct! –  RoflcoptrException Mar 9 '11 at 12:21

2 Answers 2

up vote 9 down vote accepted
\documentclass{article}
\usepackage{graphicx}
\newsavebox\IBoxA \newsavebox\IBoxB \newlength\IHeight
\newcommand\TwoFig[6]{% Image1 Caption1 Label1 Image2 ...
  \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}%
}

\begin{document}

\TwoFig{methods}      {Methods}                  {fg:methods}
       {method_detail}{Method detail information}{fg:method_detail}

\end{document}

enter image description here

share|improve this answer
    
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]{...} –  Herbert Mar 10 '11 at 16:08

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

share|improve this answer
    
Thanks a lot, this is exactly what I was looking for. –  RoflcoptrException Mar 9 '11 at 12:38
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 9 '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 9 '11 at 12:42

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.