[Tex/LaTex] side by side minipage figures

horizontal alignmentminipage

I have this code where the part marked A is the same as the part marked B except that .4 in A is .45 in B. Even though they are so similar the first one puts the images side by side with the rightmost one flush right but the second one does not do that. I don't understand why there is a difference. Could someone explain that.

\documentclass{article}
\usepackage{colortbl}
\usepackage{graphicx}
\begin{document}

\begin{center}
\end{center}

\noindent\begin{minipage}{\textwidth}

%A
\noindent\begin{minipage}[b]{.4\textwidth}
\includegraphics[width=2in]{elephant}
Some text.
\end{minipage}
\hfill
\begin{minipage}[b]{.4\textwidth}
\includegraphics[width=2in]{elephant}
Some more text.
\end{minipage}

\end{minipage}

\vspace{5ex}

%B
\noindent\begin{minipage}{\textwidth}

\noindent\begin{minipage}[b]{.45\textwidth}
\includegraphics[width=2in]{elephant}
Some text.
\end{minipage}
\hfill
\begin{minipage}[b]{.45\textwidth}
\includegraphics[width=2in]{elephant}
Some more text.
\end{minipage}

\end{minipage}

\end{document}


Output:

Creative commons info on elephant image is here

In A) you declare a width of .4\textwidth for the miniboxes and a width of 2in for the images, but 2in > .4\textwidth (with default margins); your images in fact are wider that the space reserved for them and the images overflow over the right margin (check this using the draft class option and looking at the .log file).

In B) the images have now enough space since 2in <.45\textwidth, the minipages fill the text width but not the images; the image in the second minipage is typeset starting the minipage so you will have a white space of width .45\textwidth-2in (you can verify this using \fbox around each minipage setting \fboxsep to 0pt).

In your header you are not taking into account \fboxsep (the spacing between the box and its contents) and this will cause a overfull box (the width for the \parbox must be \textwidth-2\fboxsep).

In the following code I used the draft class option (overfull \hboxes will be signaled using a black rule) and enclosed the minipages using a red frame so you can see what is really going on:

\documentclass[draft]{article}
\usepackage{colortbl}
\usepackage{graphicx}

\newcommand\Mybox[1]{%
\setlength\fboxsep{0pt}\fcolorbox{red}{white}{#1}
}

\begin{document}

\begin{center}
\end{center}

\noindent\begin{minipage}{\textwidth}

%A
\noindent\Mybox{\begin{minipage}[b]{.4\textwidth}
\includegraphics[width=2in]{elephant}
Some text.
\end{minipage}}
\hfill
\Mybox{\begin{minipage}[b]{.4\textwidth}
\includegraphics[width=2in]{elephant}
Some more text.
\end{minipage}}

\end{minipage}

\vspace{5ex}

%B
\noindent\begin{minipage}{\textwidth}

\noindent\Mybox{\begin{minipage}[b]{.45\textwidth}
\includegraphics[width=2in]{elephant}
Some text.
\end{minipage}}
\hfill
\Mybox{\begin{minipage}[b]{.45\textwidth}
\includegraphics[width=2in]{elephant}
Some more text.
\end{minipage}}

\end{minipage}

\end{document}


And the result:

Here's a modified version of your code; using width=\linewidth guarantees that the images will take exactly the available space inside the minipages:

\documentclass{article}
\usepackage{colortbl}
\usepackage[demo]{graphicx}
\usepackage{lipsum}

\begin{document}

\begin{center}
\end{center}

\lipsum[2]

\noindent\begin{minipage}[b]{.4\textwidth}
\includegraphics[width=\linewidth]{elephant}
Some text.
\end{minipage}%
\hfill
\begin{minipage}[b]{.4\linewidth}
\includegraphics[width=\textwidth]{elephant}
Some more text.
\end{minipage}

\end{document}