# Why is text being placed beyond the specified line width?

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}{
}

\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......

-
If you don't have reputation to include images, you can upload an image to imgur.com or somewhere, and post the link here. –  ShreevatsaR May 14 '11 at 8:17
There is no need to include an image here. Given the complete working example provided, anyone at all interested can compile the example for themselves and see the result. Kudos to Peter for including it! (And a small anti-kudos for asking two questions in one. The minipage question should have been asked separately.) –  Harald Hanche-Olsen May 14 '11 at 8:21
@Herald: To me, they appeared to be the same issue, never occurred to me that they were different. –  Peter Grill May 14 '11 at 16:55
depending on what you are actually trying to do, this question might be interesting: typesetting large numbers –  kgr May 14 '11 at 19:56

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.

-
I don't understand Case1a: why did it not find a hyphenation point when it found one in Case1b? Wouldn't any hyphenation point be more acceptable than having the whole word on one line (and being truncated when it goes off the page)? –  ShreevatsaR May 14 '11 at 8:41
I think I found my own solution: Using \sloppy right after \begin{document} as suggested by this question about how to make text never go over the right margin seems to fix all the issues I mentioned here, and the output actually looks less sloppy with this. However, I am wondering if there are some nasty surprises that may come up somewhere else by using this. Any thoughts? Perhaps I should post a follow up question, but I am going to experiment with this.
Although using \sloppy solves the problem posted here, it seems to have absolutely no effect on my larger document. :-( –  Peter Grill May 14 '11 at 19:36
I have posted a followup problem about how to adjust a macro to respect the current line width. This exhibits a similar problem as the one posted here, but is not solved by using \sloppy. –  Peter Grill May 14 '11 at 21:16