[Tex/LaTex] side by side minipages with keepaspectratio=true

horizontal alignmentminipage

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} 
\colorbox{blue}{\parbox[t][0.5cm][c]{\textwidth}{\bfseries{HEADER}}}
\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}

screenshot

For creative commons info on image look here

Best Answer

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} 
\colorbox{blue}{\parbox[t][0.5cm][c]{\textwidth}{\bfseries{HEADER}}}
\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}

enter image description here