[Tex/LaTex] \RaggedRight makes document longer

horizontal alignmenthyphenationline-breakingragged2e

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?

Best Answer

\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