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

screenshot

Creative commons info on elephant image is here

share|improve this question
1  
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
2  
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
add comment

1 Answer

up vote 13 down vote accepted

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

enter image description here

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

enter image description here

share|improve this answer
1  
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
add comment

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.