13

I have a document carefully written to fit in 4 pages; but when I added a \raggedright (or \RaggedRight with use of the ragged2e package) the length gets longer. Furthermore, suddenly the output isn't stable between moving from Web2c on my Linux machine, and a recent version of MiKTeX on my windows machine.

Here's a test case exhibiting the behaviour:

\documentclass[11pt,a4paper]{article}
\usepackage[a4paper,margin=20mm,nohead,foot=7mm]{geometry}
\usepackage{lipsum}
\usepackage{microtype}
\usepackage{txfonts}
\renewcommand{\familydefault}{\sfdefault}
\begin{document}
\lipsum[1-35]
\end{document}​​​​​​​​​​​

This produces something a touch over 4 pages and Web2c and MikTeX produce visually identical PDF files. But now consider:

\documentclass[11pt,a4paper]{article}
\usepackage[a4paper,margin=20mm,nohead,foot=7mm]{geometry}
\usepackage{lipsum}
\usepackage{ragged2e}
\usepackage{microtype}
\usepackage{txfonts}
\renewcommand{\familydefault}{\sfdefault}
\begin{document}
\newlength{\saveparindent}
\setlength{\saveparindent}{\parindent}
\RaggedRight
\setlength{\parindent}{\saveparindent}
\lipsum[1-35]
\end{document}​​​​​​​​​​​

On Web2c this adds two lines to the length of the document; but on MikTeX it adds 7 lines! What's going on?

Update: As some comments have very helpfully pointed out, package versions can make a difference, and unsurprisingly, this seems to be the case-- my Web2C installation (outside my control!) is very out of date. However, the original question stands-- why is there a different at all between using \RaggedRight and not?

11
  • 3
    It is highly unlikely to be a difference between the web2c and miktex implementations, more likely you have a different version of a package, add \listfiles to your document and check the versions of the packages. Mar 21, 2013 at 13:26
  • 1
  • 1
    From a quick check of the output when I run your MWE with and without microtype -- I have version 2.5 of that package, and I run MacTeX2012 -- the overall difference in document length seems to stem from a line break being inserted either right before or right after the word sclerisque, in the paragraph that starts with Curabitur tellus magna. Maybe unsurprisingly, LaTeX does not attempt to hyphenate this word, and this one variation has all these follow-on consequences. Have you tried finding out what happens with "real" text?
    – Mico
    Mar 21, 2013 at 16:00
  • 1
    It stands to reason that a document would increase in length if you switch from fully justified text to some form of ragged right: not as many words or characters will 'fit' on the same line if the text is not fully justified. As for the differences due to the different platforms, the culprit is surely differences in the versions of the packages in each distribution.
    – jon
    Mar 21, 2013 at 19:41
  • 1
    I've now actually read the ragged2e documentation (ahem) and there are various parameters you can play with to "force" more hyphenation; but oddly this still can't convince LaTeX to hyphen a word in the same way it's happy to in justified mode. Oh well, maybe this can't be done... Mar 21, 2013 at 20:31

2 Answers 2

12

\raggedright adds infinite glue (0pt plus 1fil) at the end of lines. As jon has commented, this will make hyphenation (next to) impossible, which will result in fewer words per line.

\RaggedRight, on the other hand, adds finite glue (the default is 0pt plus 2em), so hyphenation becomes possible again -- but this won't result in the same hyphenation/line-breaking patterns as with justified text. The reason is that lines that may feature acceptable (for TeX' line-breaking algorithm) interword spacing when using \RaggedRight would become too spaced out for justified text, so TeX will rather choose a solution with one more word squeezed into the respective line. This effect will occur more often if you use microtype and its font expansion feature.

The following example hopefully illustrates this:

  • With \raggedright (and microtype), the word "fermentum" won't be hypenated, but shifted as a whole to the fourth line of the paragraph.

  • \RaggedRight allows hyphenation ("fer-mentum").

  • Justified text plus microtype allows to fit the word "laoreet" in the first line, which in turn allows to fit "fermentum" as a whole in the third line, which will result in a paragraph one line shorter.

  • Justified text without microtype can't squeeze "laoreet" into the first line, which in turn means that hypenation ("fer-mentum") becomes the preferred solution in the third line. Contrary to \RaggedRight, the word "sed" will be part of the fourth line.


\documentclass{article}

\usepackage{ragged2e}
\usepackage{microtype}
\usepackage{lipsum}

\newlength{\saveparindent}
\setlength{\saveparindent}{\parindent}

\makeatletter
\g@addto@macro{\raggedright}{\setlength{\parindent}{\saveparindent}}
\g@addto@macro{\RaggedRight}{\setlength{\parindent}{\saveparindent}}
\makeatother

\begin{document}

\raggedright
\lipsum[6]

\RaggedRight
\lipsum[6]

\justifying
\lipsum[6]

\microtypesetup{activate=false}
\lipsum[6]

\end{document}​​​​​​​​​​​

enter image description here

1
  • 6
    Just to add to your answer. The original raggedright by Knuth had finite glue and as per my understanding the sole reason for the ragged2e package.
    – yannisl
    Mar 22, 2013 at 9:10
7

As pointed out in the comments, the difference between different installations is probably due to different hyphenation from different packages, and sometimes from different language settings.

The reason raggedright often gives more lines than fully justified text is to do with the intricacies of TeX's line-breaking algorithm. This is a fairly obvious statement, so I'll try to explain what is going as far as I understand it.

There is a "penalty" for each departure from perfection of a paragraph, and TeX tries to find line breaks which minimise the total penalty for a paragraph. Hyphenation carries a penalty as do lines that are overly stretched (loose) or compressed (tight), especially with adjacent loose and tight lines.

So with fully-justified text, the line breaking algorithm has to work harder to find a set of line breaks that are good enough, and will set tight lines if it is the best solution. This will squeeze lines as often as it will stretch them (to a first approximation).

With ragged right justification, \rightskip is nonzero, so extra stretchy space (glue) is added to the end of each line, and this is stretchy enough that when TeX is actually laying out the spacing of each line, it doesn't stretch the standard inter-word spacing at all. The consequence of this is that when searching for line breaks, solutions which just stretch this end-of-line spacing to its maximum (\rightskip=0pt plus 2em in plain.tex, \rightskip=0pt plus 1fil in latex.ltx) are relatively more attractive than a tight line that squeezes an extra word or part-word onto that line. So relatively few tight lines are set.

The solution (if you want one for a particular document) is to try to encourage TeX to favour tight lines, or at least find a set of breakpoints that isn't too bad that use fewer lines than the optimum found. The standard trick here is to put \looseness=-1 at the end (or beginning) of an offending paragraph. Otherwise you can start messing with the internal parameters of the line breaking algorithm: hyphen penalties, inter-word glue, tolerance, pretolerance, ...

... here there be dragons!

You must log in to answer this question.

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