# side by side minipages with keepaspectratio=true

This question may seem superficially similar to side by side minipage figures but in fact I believe its different.

This code produces a result which is messed up (the text should be under the respective image left justified and everything should be lined up nicely and the images should be flush left and right) as can be seen from the screenshot that follows this code. If we remove the keepaspectratio=true as shown in the commented out lines then the layout does look a lot better but the images become distorted. I want to use this as a template for different images so its important that the specifics of the image not be used.

Note that the three last sections of the code separated by \vspace{1ex} are each identical so there is actually a lot less unique code than might appear from the length.

Firstly, how can I fix this? Secondly, how can I change it so that not only is it fixed but that every time I make some small change the entire layout and look changes dramatically as has already happened twice? This is so frustrating. Thanks.

\documentclass{article}
\usepackage{colortbl}
\usepackage[top=1in,bottom=1in,left=0.5in,textwidth=7.5in]{geometry}
\usepackage{graphicx}
\usepackage{lipsum}

\setlength{\fboxsep}{0pt}
\begin{document}

\begin{center}
\end{center}

\lipsum[2]

\noindent\begin{minipage}[b]{.5\textwidth}
%\includegraphics[width=\linewidth, height=.25\textheight]{elephant}
\includegraphics[width=\linewidth, height=.25\textheight, keepaspectratio=true]{elephant}
Some text.
\end{minipage}
\hfill
\begin{minipage}[b]{.5\linewidth}
%\includegraphics[width=\linewidth, height=.25\textheight]{elephant}
\includegraphics[width=\linewidth, height=.25\textheight, keepaspectratio=true]{elephant}
Some more text.
\end{minipage}

\vspace{1ex}

\noindent\begin{minipage}[b]{.5\textwidth}
%\includegraphics[width=\linewidth, height=.25\textheight]{elephant}
\includegraphics[width=\linewidth, height=.25\textheight, keepaspectratio=true]{elephant}
Some text.
\end{minipage}
\hfill
\begin{minipage}[b]{.5\linewidth}
%\includegraphics[width=\linewidth, height=.25\textheight]{elephant}
\includegraphics[width=\linewidth, height=.25\textheight, keepaspectratio=true]{elephant}
Some more text.
\end{minipage}

\vspace{1ex}

\noindent\begin{minipage}[b]{.5\textwidth}
%\includegraphics[width=\linewidth, height=.25\textheight]{elephant}
\includegraphics[width=\linewidth, height=.25\textheight, keepaspectratio=true]{elephant}
Some text.
\end{minipage}
\hfill
\begin{minipage}[b]{.5\linewidth}
%\includegraphics[width=\linewidth, height=.25\textheight]{elephant}
\includegraphics[width=\linewidth, height=.25\textheight, keepaspectratio=true]{elephant}
Some more text.
\end{minipage}

\end{document}


For creative commons info on image look here

-

Change lines after \includegraphics and avoid superfluous blank spaces; you can measure the width of the image(s) and use this width for the minipages to get the proper alignment for the text:

\documentclass{article}
\usepackage{colortbl}
\usepackage[top=1in,bottom=1in,left=0.5in,textwidth=7.5in]{geometry}
\usepackage{graphicx}
\usepackage{lipsum}

\newlength\imageheight
\newlength\imagewidth

\settowidth\imagewidth{\includegraphics[width=\linewidth, height=.25\textheight, keepaspectratio=true]{elephant}}

\setlength{\fboxsep}{0pt}
\begin{document}

\begin{center}
\end{center}

\lipsum[2]

\noindent\begin{minipage}[b]{\imagewidth}
\includegraphics[width=\linewidth, height=.25\textheight, keepaspectratio=true]{elephant}\\
Some text.
\end{minipage}%
\hfill
\begin{minipage}[b]{\imagewidth}
\hfill
\includegraphics[width=\linewidth, height=.25\textheight, keepaspectratio=true]{elephant}\\
\hfill Some more text.
\end{minipage}

\vspace{1ex}

\noindent\begin{minipage}[b]{\imagewidth}
\includegraphics[width=\linewidth, height=.25\textheight, keepaspectratio=true]{elephant}\\
Some text.
\end{minipage}%
\hfill
\begin{minipage}[b]{\imagewidth}
\includegraphics[width=\linewidth, height=.25\textheight, keepaspectratio=true]{elephant}\\
Some more text.
\end{minipage}

\vspace{1ex}

\noindent\begin{minipage}[b]{\imagewidth}
\includegraphics[width=\linewidth, height=.25\textheight, keepaspectratio=true]{elephant}\\
Some text.
\end{minipage}%
\hfill%
\begin{minipage}[b]{\imagewidth}
\includegraphics[width=\linewidth, height=.25\textheight, keepaspectratio=true]{elephant}
Some more text.
\end{minipage}

\end{document}


-
Thanks. I played with the solution to understand which changes mattered and which not and it seems the key elements are (1) to add \\ at the end of each \includegraphics line and (2) to make each minipage the width of the image contained in it. The % at the end of \end{minipage} does not do anything here but perhaps would be helpful if there were other changes. There was some other unnecessary code which the responder has now indicated has been removed. –  user1189687 Aug 21 '12 at 17:04
@user1189687 yes; I removed the unnecessary code. –  Gonzalo Medina Aug 21 '12 at 17:06