[Tex/LaTex] Why is text being placed beyond the specified line width

line-breakingminipage

I thought that I knew a few things, but looks like I have some basic misunderstanding on how TeX is supposed to typeset material. I thought that settings by the geometry package would force the text to fit inside the given \geomerty{textwidth=6.0in}. It seems to work sometime, but not all the time. The code below illustrates the following:

Case 0 illustrates the correct placement of the text. It goes right up to the left and the right as I think it should. Here things work as one would expect.

Case 1 uses a really long word. When used as in Case 1a,the text goes way past the end of specified textwidth. Case 1b does not appear to be all that different, and yet, things seems to work fine: the long word gets hyphenated. What is the difference that causes Case 1a to break, but Case 1b to work? More importantly, how do I get the behavior of Case 1B all the time.

Case 2 has text following two minipages which take up 0.65\linewidth and 0.30\linewidth. Some leading space is being inserted so perhaps that is the problem, and hence I have been limiting my minipage width sum to be 0.95\linewidth. I can put up with the leading space of the minipage, but I really don't like the line "Why does this not start on a new line?" starting past the edge where text should be placed.

Case 3 (seems different to me, but I suspect it might be the same root problem) has a wide minipage following some text. My understanding is that TeX will make a box of this minipage, realize that it won't fit at the current location in the line and then automatically go on to the next line, but it does not seem to be doing that.

\documentclass{article}
\usepackage{geometry}

\newcommand*{\BlahBlah}{
blah blah blah blah blah blah blah blah blah blah blah blah 
blah blah blah blah blah blah blah blah blah blah blah blah 
blah blah blah blah blah blah blah blah blah blah blah blah 
}

\newcommand*{\LongText}{
YadaYadaYadaBlahBlahBlahYadaYadaBlahBlahBlahYadaYadalahYadaYadaYadaBlahBlahBlahYadaYadaBlahBlahBlah
}

\geometry{top=0.5in}%
\geometry{bottom=0.5in}%
\geometry{paperheight=11.0in}%
\geometry{paperwidth=8.5in}%
\geometry{textwidth=6.0in}%
\geometry{left=1.125in}  %{(\paperwidth-\textwidth)/2)}
\geometry{right=1.125in}  %{(\paperwidth-\textwidth)/2)}
\geometry{showframe=true}%

\begin{document}
\textbf{Case 0}: This illustrates the correct placement of the text: \BlahBlah 

\textbf{Case 1a}: \LongText

\textbf{Case 1b}: The above did not stop at end of line, but this does: \LongText

Case 2: Text following minipages:

\begin{minipage}{0.65\linewidth}
\textbf{Minpage 1}: \BlahBlah
\end{minipage}
\begin{minipage}{0.30\linewidth}
\textbf{Minipage 2}: \BlahBlah
\end{minipage}
% Adding a blank line here will fix this problem
Why does this not start on a new line?

\textbf{Case 3}: \BlahBlah \BlahBlah
% Why does commenting this out result is a line much longer than 6.0in
\begin{minipage}{0.65\linewidth}
\textbf{Minipage 3}: \BlahBlah
\end{minipage}
\end{document}

I realize that Case 2 and 3 are easily fixed manually by inserting a blank line (currently comment lines) which tells TeX to start a new paragraph and things work fine. But I would like to understand why this is not happening automatically. I am having problems in much more complicated situations and the issues described here in this MWE are very similar so if I can understand this, I can tackle the more complicated issues.

So to summarize, how can I force text to always be placed only within the \textwidth dimensions I have set. Of course, if there is a minipage (or image) that is wider than the specified textwidth then all bets are off, but I am only interested in cases where things are less than the textwidth.

I would have liked to include an image of the results so that this question could be more easily understood, but I guess I don't have enough of a reputation to do that yet… Hopefully I will get there soon……

Best Answer

The short answer to your main question is that TeX can never be forced to keep text within the margins; it only tries very hard to do so. As you have found out, it is fairly easy to cause it to fail.

The slightly longer answer is that TeX works by minimizing the total badness of a paragraph, where badness is computed by a variety of factors, such as stretching and shrinking of spaces to make the text fit, hyphenations, and so on. Beyond a certain badness, though (10000 to be precise), the paragraph is considered to be infinitely bad, and so TeX sort of gives up.

In Case 1a, due to the very long word and relative lack of stretchable space, TeX couldn't find a hyphenation point that produced an acceptable outcome, so the long word remained unhyphenated and going into the margin. In case 2a, there is much more white space to stretch, and perhaps also you were fortunate enough to have a hyphenation point within a suitable range.

As to minipages, LaTeX treats a minipage as if it were one humongous letter. That is why you need the blank line after it in order to start a new line there.

Related Question