8

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

4
  • If you don't have reputation to include images, you can upload an image to imgur.com or somewhere, and post the link here. May 14, 2011 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.) May 14, 2011 at 8:21
  • @Herald: To me, they appeared to be the same issue, never occurred to me that they were different. May 14, 2011 at 16:55
  • depending on what you are actually trying to do, this question might be interesting: typesetting large numbers
    – kgr
    May 14, 2011 at 19:56

2 Answers 2

8

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.

9
  • 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)? May 14, 2011 at 8:41
  • The compilation log indicates that the hyphenation it found was "Ya-daYa-daYad-aBlah-Blah-BlahYa-daYad-aBlah-Blah-BlahYa-daYadalahYa-daYadaYadaBlahBlahBlahYadaYadaBlahBlahBlah", which may answer my previous question above, but raises the new question of why it gives up hyphenating after a certain length. (I'm wondering about the design choice to have it this way, not the details of the implementation that make it so.) May 14, 2011 at 8:46
  • 3
    @ShreevatsaR: TeX abandons hyphenation at the 64th character in a word. This limitation is raised in LuaTeX.
    – egreg
    May 14, 2011 at 9:13
  • So the answer to "how do I get the behavior of Case 1B all the time" is "If you use any word more than 64 letters long, explicitly indicate its hyphenation". May 14, 2011 at 10:07
  • 1
    @Herald: So, if the minipages are treated just like big letters, why can't these "letters" automatically be moved to the next line if they don't fit on the current one, as is done with "normal" letters? May 14, 2011 at 17:06
3

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.

2

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .