46

I was just wondering if there was a LaTeX tool for making paragraphs of text into perfect rectangles. The effect is shown below in a schematic

enter image description here

50

The parameter is \parfillskip; it's better to increase \emergencystretch not to confine all badness in one line. Paragraphs should be sufficiently long.

\documentclass{article}
\usepackage{microtype}
\usepackage{kantlipsum}

\setlength{\parindent}{0pt}
\setlength{\parskip}{.5\baselineskip}
\setlength{\parfillskip}{0pt} % don't fill the last line
\setlength{\emergencystretch}{.5\textwidth} % not to get preposterously bad lines

\begin{document}
\kant[1-3]
\end{document}

enter image description here

You can get the same behavior on a single paragraph if you end it with the combination

... here ends the paragraph.%
{\parfillskip=0pt \emergencystretch=.5\textwidth \par}

Notice the braces: the setting will be undone just after the paragraph has ended.

At the end of a paragraph, when \par is sensed (it's usually inserted automatically by TeX seeing an empty line), TeX removes horizontal glue (space) that might be present, then issues

\penalty10000 % no break here or the next glue would be removed
\hskip\parfillskip
\penalty-10000 % force a break

The usual value of \parfillskip is 0pt plus 1fill, so normally this will push the last line to be ragged right. If we set it to 0pt, the last line will be filled up. Of course, TeX must be able to find a suitable sequence of line breaks, or it will set a paragraph with underfull lines. In general it will prefer to concentrate all badness in one line; by setting \emergencystretch to a positive value, TeX will possibly reconsider the line breaks and distribute badness over all lines. See p. 100 of the TeXbook and exercise 14.15 (about modernistic novelist).

  • 1
    and here is the much better solution :) – Scott H. May 28 '13 at 20:30
  • 1
    It might make sense to use the option stretch=30 or stretch=40 for microtype in addition to the \emergencystretch setting (20 is the default). – Stephan Lehmke Nov 24 '13 at 12:41
  • @StephanLehmke I fully agree. – egreg Nov 24 '13 at 13:17
  • 1
    @egreg Likewise, should all of the microtype settings be slightly liberalized if we are pursuing the rectangular appearance? – Simon Kuang Feb 5 '16 at 23:03
  • 1
    See tex.stackexchange.com/questions/364715/… for further discussion on rectangular paragraphs, including a shortcoming of this solution. – Ruben Verborgh Apr 14 '17 at 11:38
15

There are likely much better solutions, but using \linebreak at the end of a paragraph as a quickie seems to work:

enter image description here

\documentclass{article}
\begin{document}

\noindent
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed eget ante
pretium  leo  aliquet ultricies.  Aenean  ultrices  facilisis sem  nec
tempus. Aenean  pretium odio  eget diam  congue feugiat.  Sed faucibus
erat vel  risus lobortis sit  amet tincidunt justo mollis.   Mauris id
volutpat sem.   Praesent bibendum, dui  vel tempus dictum,  nisi risus
hendrerit  eros, sed  faucibus orci  felis in  neque. Vivamus  ut nibh
lectus.   Sed  sit amet  dolor  eget  diam  tempus tempus  vitae  eget
leo. Cras imperdiet risus id  ante molestie tincidunt. Mauris nec nunc
orci, vitae  auctor velit.  Proin  gravida quam sit amet  nulla tempor
condimentum auctor tellus facilisis.\par\bigskip

\noindent
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed eget ante
pretium  leo  aliquet ultricies.  Aenean  ultrices  facilisis sem  nec
tempus. Aenean  pretium odio  eget diam  congue feugiat.  Sed faucibus
erat vel  risus lobortis  sit amet tincidunt  justo mollis.  Mauris id
volutpat sem.  Praesent bibendum,  dui vel  tempus dictum,  nisi risus
hendrerit  eros, sed  faucibus orci  felis in  neque. Vivamus  ut nibh
lectus.  Sed  sit  amet  dolor  eget diam  tempus  tempus  vitae  eget
leo. Cras imperdiet risus id  ante molestie tincidunt. Mauris nec nunc
orci, vitae  auctor velit.  Proin gravida quam  sit amet  nulla tempor
condimentum auctor tellus facilisis.\linebreak

\end{document}
  • Gah, copy pasted lipsum won't break... – Scott H. May 28 '13 at 20:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.