# side by side minipage figures

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 addition to what @Gonzalo said, note that you have a word space (as well as the \hfill between the two minipages, because of a missing % after the \end{minipage} This doesn't matter if you do want the gap down the middle but if you try to increase the width up towards 0.5\textwidth you will find that you can not fit in the two minipages side by side without removing the word space between them. – David Carlisle Aug 21 '12 at 0:28
Thanks for pointing this out. It seems strange that one needs a % after end{minipage} but not after \hfill . There seem so many small points like this that are useful but difficult to really know. Since its difficult to know when you really need % and when not it seems safer to use % liberally -- although its a bit disappointing that one must program defensively like this because the language itself is too difficult to really understand. – user1189687 Aug 21 '12 at 11:33
white space after command names is always ignored (it is just used to terminate the command) so the linebreak after \hfill contributes nothing. after a } there is no automatic white space removal. It os a pain though which is why the LaTeX3 proposed programming setup makes all white space ignored (and you have to explicitly use ~ to get a space anywhere. – David Carlisle Aug 21 '12 at 11:54

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}


-
Thanks. The \fboxsep part seems particularly tricky. Now that you explained it I think I will just set it to zero: \setlength{\fboxsep}{0pt}. I also had to look up the difference between \linewidth and \textwidth and found it here: public.asu.edu/~rjansen/latexdoc/textwidth.html – user1189687 Aug 21 '12 at 4:49

For putting figures side by side you can try this one (everything is handled automatically and you may use more or less number of figures: 2, 3, 4, etc.):

\usepackage{graphicx}
\usepackage{subfigure}
\begin{document}

\begin{figure}[ht]
\centering
\subfigure[Subfigure 1 title]{
\rule{2.8cm}{2cm}
\label{fig:subfig1}
}
\subfigure[Subfigure 2 title]{
\rule{2.8cm}{2cm}
\label{fig:subfig2}
}
\subfigure[Subfigure 3 title]{
\rule{2.8cm}{2cm}
\label{fig:subfig3}
}
\subfigure[Subfigure 4 title]{
\rule{2.8cm}{2cm}
\label{fig:subfig4}
}
\caption[Optional caption for list of figures]{General Caption of subfigures 1-4}
\label{fig:subfigureExample1}
\end{figure}

Reference to figure1:
\ref{fig:subfigureExample1}
and
\ref{fig:subfig4}.

\end{document}


And the referencing:


Or this one (you have to provide images subfig5.jpg to subfig8.jpg):

\usepackage{graphicx}
\usepackage{subfigure}
\begin{document}
\begin{figure}[ht]
\centering
\subfigure[Subfigure 5 title]{
\includegraphics[width=0.4\columnwidth, keepaspectratio]{subfig5.jpg}
\label{fig:subfig5}
}
\subfigure[Subfigure 6 title]{
\includegraphics[width=0.4\columnwidth, keepaspectratio]{subfig6.jpg}
\label{fig:subfig6}
}
\subfigure[Subfigure 7 title]{
\includegraphics[width=0.4\columnwidth, keepaspectratio]{subfig7.jpg}
\label{fig:subfig7}
}
\subfigure[Subfigure 8 title]{
\includegraphics[width=0.4\columnwidth, keepaspectratio]{subfig8.jpg}
\label{fig:subfig8}
}
\caption[Optional caption for list of figures 5-8]{General Caption of subfigures 5-8}
\label{fig:subfigureExample2}
\end{figure}

Reference to figure:
\ref{fig:subfigureExample2}
and
\ref{fig:subfig6}.

\end{document}


And the referencing:


Hint: "Subfigure" package may be old, but does anything automatically. I tried several options and found it the most useful one.

-
The problem is that subfigure is not only old but an obsolete package which shouldn't be used anymore; one should use subfig or subcaption instead. There are already plenty examples using subcaption or subfig (for example tex.stackexchange.com/a/37597/3954), so I would suggest you to consider rewriting the answer so that it provides new information. – Gonzalo Medina Jul 24 '14 at 21:43
Thank you for the comment, Gonzalo. Added another answer. – Alisa Jul 24 '14 at 22:47

If you don't like using subfigure, then use "caption" and "subcaption":

\usepackage{caption}
\usepackage{subcaption}

\begin{document}

\begin{figure}
\centering
\begin{subfigure}[t]{0.4\columnwidth}
\centering
\includegraphics[width=\columnwidth, keepaspectratio]
{frog.jpg}
\caption{Subfigure 1 title}\label{fig:subfig1}
\end{subfigure}
\begin{subfigure}[t]{0.4\columnwidth}
\centering
\includegraphics[width=\columnwidth, keepaspectratio]
{frog.jpg}
\caption{Subfigure 2 title}\label{fig:subfig2}
\end{subfigure}
\begin{subfigure}[t]{0.4\columnwidth}
\centering
\includegraphics[width=\columnwidth, keepaspectratio]
{frog.jpg}
\caption{Subfigure 3 title}\label{fig:subfig3}
\end{subfigure}
\begin{subfigure}[t]{0.4\columnwidth}
\centering
\includegraphics[width=\columnwidth, keepaspectratio]
{frog.jpg}
\caption{Subfigure 4 title}\label{fig:subfig4}
\end{subfigure}
\caption{General Caption of subfigures 1-4}\label{fig:subfigureExample1}
\end{figure}

Testing Reference to figure:
Figure \ref{fig:subfigureExample1} and Figure \ref{fig:subfig4}.

\end{document}


-