# Make \section unaffected by \parfillskip

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

\parfillskip 0pt plus 0.75\textwidth


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.75\textwidth:

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.75\textwidth
\section{This is a section title}
\lipsum[1]
\end{document}

• My impression is that you create more paragraphing problems than you solve, with that setting of \parfillskip. Dec 29 '13 at 17:32
• @egreg I'm quite happy with what it does to the paragraphs actually; just not do the titles: i.imgur.com/YAVeaTu.png But other suggestions to achieve the same effect with paragraphs are welcome. Dec 29 '13 at 18:30

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}{\begingroup\parfillskip=0pt plus 1fil\relax}{}{}
\patchcmd{\@ssect}{\begingroup}{\begingroup\parfillskip=0pt plus 1fil\relax}{}{}
\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.75\textwidth
\section{This is a section title}
\lipsum[1]
\end{document}


• Great! Note that this solution also works for \subsection (despite what \@sect might make you think). Dec 29 '13 at 20:24
• how can I apply this for section*? Nov 3 '20 at 22:28
• @EmitTaste Apply the same patch to \@ssect. I added the code, thanks for noticing. Nov 3 '20 at 22:29

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

\setsecheadstyle{\Large\parfillskip=0pt plus 1fil}

• And for KOMA-Script classes you do not need to do anything, because they already support \parfillskip > 0. Mar 10 '17 at 11:37

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

\newcommand\section{\@startsection {section}{1}{\z@}%
{-3.5ex \@plus -1ex \@minus -.2ex}%
{2.3ex \@plus.2ex}%
{\normalfont\Large\bfseries}}


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

\makeatletter
\renewcommand\section{\@startsection {section}{1}{\z@}%
{-3.5ex \@plus -1ex \@minus -.2ex}%
{2.3ex \@plus.2ex}%
{\normalfont\raggedright\Large\bfseries}}