[Tex/LaTex] How to make (La)TeX prefer to fill centered lines

formattinghorizontal alignmentline-breaking

My thesis formatting requirements ask for multiline chapter and section titles to be a maximum of 4.5 inches wide with any subsequent lines being wrapped such that they are sucessively shorter, forming an inverted pyramid. I found the \parshape TeX command that I am using to try and enforce this automatically. But, it doesn't always end up working because with centered text, the line breaks occur before the lines actually fill up in some cases.

I've read up a bit on TeX's line-breaking algorithm and have tried playing around with a few penalties, but I can't figure out how it works when it's in centering mode. Does anyone know how I can encourage TeX to fill up lines when in centering mode? Any clues would be greatly appreciated.

Here is a minimal working example of the difference in behavior in centered vs. non-centered modes:

\documentclass{book}

\newcommand{\headingparshape}%
{%
    \parshape=6 % account for a maximum of six lines - thereafter all will have the final width
        0.75in 4.5in
        1.00in 4.0in
        1.25in 3.5in
        1.50in 3.0in
        1.75in 2.5in
        2.00in 2.0in
}

\begin{document}

In a centered mode:
\begin{center}
    \huge\bfseries\headingparshape
    Here is a title that needs to wrap over several lines and has short words
\end{center}

In normal (justified) mode:

{\huge\bfseries\headingparshape{}Here is a title that needs to wrap over several lines and has short words\par}

\end{document}

In the centered mode, the line breaks end up here (messing up the inverted pyramid layout):

Here is a title that needs
to wrap over several lines
and has short words

In the normal mode, the line breaks end up here (inverted pyramid correct):

Here is a title that needs to
wrap over several lines and
has short words

So, the words "Here is a title that needs to" all fit on the first line, but I can't figure out how to make that happen in the centered version without having to do a bunch of distasteful manual formatting with ~ and \\ commands.

Best Answer

Based on egreg's answer, here's one that doesn't require identifying the last line. The trick comes from TeX by Topic.

\documentclass{article}
\newcommand\stupid[1]{%
        \vbox{%
                \hsize=4.5in
                \parindent=0pt
                \leftskip=0pt plus.5fil
                \rightskip=0pt plus-0.5fil
                \parfillskip=0pt plus1fil
                \emergencystretch=1in
                \parshape6
                0.00in 4.50in
                0.25in 4.00in
                0.50in 3.50in
                0.75in 3.00in
                1.00in 2.50in
                1.25in 2.00in
                \huge
                \bfseries
                \strut
                #1%
        }%
}
\begin{document}
\stupid{Here is a title that needs to wrap over several lines and has
short words}
\end{document}

enter image description here

Related Question