TransWikia.com

Make section unaffected by parfillskip

TeX - LaTeX Asked by Ruben Verborgh on December 25, 2020

To avoid widow words at the last line of a paragraph, I adjust parfillskip:

parfillskip 0pt plus 0.75textwidth

While this gives nicer paragraphs, it also stretches section titles a little, as you can see below.
The first title is with the default parfillskip, the second with 0pt plus 0.75textwidth:

How can I keep my parfillskip setting for paragraphs, but not for titles?

This code showcases the effect for text and titles (preview):

documentclass{article}
usepackage{lipsum}
begin{document}
section{This is a section title}
lipsum[1]
section{This is a section title}

% Fill the last line of paragraphs for minimum 25%
parfillskip 0pt plus 0.75textwidth
section{This is a section title}
lipsum[1]
end{document}

3 Answers

The section title is typeset by @sect, which happily encloses the relevant part in a group. At the end it adds @@par, which is the primitive par.

Thus we can safely patch @sect (for numbered sections) and @ssect (for unnumbered sections):

documentclass{article}

usepackage{etoolbox}
makeatletter
patchcmd{@sect}{begingroup}{begingroupparfillskip=0pt plus 1filrelax}{}{}
patchcmd{@ssect}{begingroup}{begingroupparfillskip=0pt plus 1filrelax}{}{}
makeatother

usepackage{lipsum}
begin{document}
section{This is a section title}
lipsum[1]

% Fill the last line of paragraphs for minimum 25%
parfillskip 0pt plus 0.75textwidth
section{This is a section title}
lipsum[1]
end{document}

enter image description here

Correct answer by egreg on December 25, 2020

For the memoir class, it suffices to add to the preamble:

setsecheadstyle{Largeparfillskip=0pt plus 1fil}

Answered by Ruben Verborgh on December 25, 2020

Since section titles should not be formatted with flush-right margins in any case -- despite the default document classes doing exactly that -- you should declare raggedright in the definition of the sectioning; and once you do that, the parfillskip is irrelevant.

Define the sectioning however you want:

  • with memoir or koma or other nice document class;
  • using a package like titlesec or some such;
  • with sectioning of the standard document classes using @startsection
  • or, the most hackish, patch commands like @sect

For example, the basic "article" class uses

newcommandsection{@startsection {section}{1}{z@}%
                               {-3.5ex @plus -1ex @minus -.2ex}%
                               {2.3ex @plus.2ex}%
                               {normalfontLargebfseries}}

so a document setup could include a renewcommand of that with raggedright inserted in the font-style argument

makeatletter
renewcommandsection{@startsection {section}{1}{z@}%
                               {-3.5ex @plus -1ex @minus -.2ex}%
                               {2.3ex @plus.2ex}%
                               {normalfontraggedrightLargebfseries}}

Answered by Donald Arseneau on December 25, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP