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
-
4See, maybe, this answer. Very cool-looking book. (But I don't think it is easy to do!)– jonMay 28, 2013 at 20:12
-
More discussion at tex.stackexchange.com/questions/364715/…. Please also read on at tex.stackexchange.com/questions/151680/… if headings should be left untouched.– kopporDec 7, 2017 at 10:06
2 Answers
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}
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
-
1It might make sense to use the option
stretch=30
orstretch=40
formicrotype
in addition to the\emergencystretch
setting (20 is the default). Nov 24, 2013 at 12:41 -
1@egreg Likewise, should all of the
microtype
settings be slightly liberalized if we are pursuing the rectangular appearance? Feb 5, 2016 at 23:03 -
1If one doesn't want to have this effect in headings, follow the instructions given at tex.stackexchange.com/a/151692/9075– kopporJan 25, 2017 at 7:35
-
1See tex.stackexchange.com/questions/364715/… for further discussion on rectangular paragraphs, including a shortcoming of this solution. Apr 14, 2017 at 11:38
There are likely much better solutions, but using \linebreak
at the end of a paragraph as a quickie seems to work:
\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}