Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question
6  
I think you should consider doing this manually. Without commenting on the wisdom of this rule ... if it applies you should try to make the breaks occur at places that make sense semantically, and automating shape creation isn't going to guarantee that, to say the least. In this case, I think manual formatting (however distasteful) is correct. –  Paul Stanley Jun 3 '11 at 21:53
    
I certainly agree that semantically sensible line breaking would be preferable, but the thesis office could care less what the words say. They are just looking for the shape they specify. That's why I wanted to try coming up with something more automated and would still appreciated any insights on how to control the line breaking in the centered version. –  E J Jun 3 '11 at 22:11
    
@EJ: Welcome to tex.sx! Usually, we don't put a greeting or a "thank you" in our posts. While it might seem strange at first, it is not a sign of a lack of politeness, but rather part of our trying to keep everything very concise. Upvoting is the preferred way here to say "thank you" to users who helped you. A tip: you can use backticks ` to mark your inline code as I did in my edit. –  doncherry Jun 3 '11 at 22:48
    
@doncherry: Thanks, err, upvote for the tips. ~:-) –  E J Jun 4 '11 at 0:32
1  
@EJ: Please don't add a solution to the question. Add it as an answer. This makes the structure of the page much cleaner. –  Caramdir Jun 4 '11 at 4:39

4 Answers 4

\usepackage[newcommands]{ragged2e}
share|improve this answer
1  
I had heard of this package, but never really read up on it. After taking a read through the first part of its documentation, I see that it would effectively provide a solution very similar in nature to the one TH. provided below. Thanks for pointing it out. I'll definitely keep it in mind for future use and I learned a lot just from reading the docs. –  E J Jun 4 '11 at 2:35
1  
You should add a description (even if it's short) of what your answer is and why is it good. Now it's not an answer — a hint at best. –  Andrey Vihrov Jun 4 '11 at 6:47
    
@Andrey: At the risk of losing more reputation: My answer solves EJs problem. And it points him to the package. I don't see why this is not useful. –  Martin Schröder Jun 4 '11 at 22:40
2  
Be "Instructive – whenever possible, answers should teach users the rationale behind a solution: why does it work? This is most valuable when the question suggests that the user misunderstands some of the basics of TeX/LaTeX." (meta.tex.stackexchange.com/questions/227/…) –  doncherry Sep 5 '11 at 21:57
\newcommand{\invpyr}[1]{\vbox{\hsize=4.5in \parindent=0pt \emergencystretch=1in
  \parshape 6
    0.00in 4.50in
    0.25in 4.00in
    0.50in 3.50in
    0.75in 3.00in
    1.00in 2.50in
    1.25in 2.00in
    \leftskip=0pt plus 1fil \rightskip=0pt plus -1fil
    \parfillskip=0pt plus 2fil
    #1\par}}
\invpyr{\huge\bfseries
  Here is a title that needs to wrap over several lines and
  has short words}

You can then center the \vbox or do what you want.

Note. I've seen the trick with \leftskip, \rightskip and \parfillskip somewhere, but I don't remember where.

Do committees for thesis regulations race one against the other for who finds the more absurd rules?

share|improve this answer
    
I think I see what you're getting at with this, but having to manually identify the last line would be just about as much work as manually specifying all the line breaks. This is definitely a case of an absurd rule, but not the only one in our formatting handbook. Here's what they say about trying to meet the specifications: "No prepackaged computer program completely adheres to the [university to remain anonymous] Graduate School thesis guidelines." I.e. no package that uses well-designed typographical rules would ever be caught dead doing things like this... Hence the difficulty with LaTeX. –  E J Jun 3 '11 at 22:33
2  
@EJ: Welcome to thesis writing. =) –  TH. Jun 4 '11 at 0:40
    
@E J Try with the new definition. –  egreg Jun 4 '11 at 7:56

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

share|improve this answer
2  
Phenomenal. Thank you so much! That works like a charm. Well, as charmingly as could be expected given that the name you picked for the command is spot on. Hooray for ridiculous thesis office formatting requirements. –  E J Jun 4 '11 at 2:25
    
I edited my answer without realizing you did it already. –  egreg Jun 4 '11 at 21:11

Final Solution

Building on the suggestions from TH. and what I learned from the ragged2e package documentation given Martin's suggestion, here is the way I implemented this command:

\newlength{\@headingTextWidth}
\newcommand{\centeredHeadingPar}[1]%
{%
    \leavevmode\vbox{%
        \hsize=4.5in
        \parindent=0pt
        \parfillskip=0pt
        \emergencystretch=2em
        %
        \pretolerance=500
        \tolerance=1000
        \hyphenpenalty=100
        \linepenalty=0
        %
        \settowidth{\@headingTextWidth}{#1}%
        \ifdim\@headingTextWidth<4.5in%
            % True case - the heading will be only one line.
            % Add fils to the [left/right]skips to avoid distracting underful hbox complaints.
            \leftskip=0pt plus 0.5fil
            \rightskip=0pt plus 0.5fil
        \else%
            % False case - multiline heading.
            % Match the rubber in the [left/right]skips to the pyramid steps to ensure that each
            % successive line is shorter than the previous.
            \leftskip=0pt plus 0.25in
            \rightskip=0pt plus 0.25in
        \fi%
        %
        #1%
        %
        \parshape=6 % account for a maximum of six lines - thereafter all will have the final width
            0.00in 4.5in
            0.25in 4.0in
            0.50in 3.5in
            0.75in 3.0in
            1.00in 2.5in
            1.25in 2.0in
    }% \vbox
}% \centeredHeadingPar

I left out the formatting commands so that the ones in effect will apply. That way I can use it for the different levels of headings that have different font sizes. By matching the \leftskip and \rightskip rubber to the step with each level of the pyramid it makes sure that each line will be shorter than the next without requiring potentially huge interword spaces. Since the width of the \parbox now matches the length of the top line, the command is now intended to be used in a centering environment.

Now there are just two circumstances where trouble arises: (1) places where TeX absolutely can't find an appropriate place to break the line and leaves one hanging out into the margin, and (2) places where a single word ends up on the last line and the left/right skips don't provide enough stretch to fill the line, causing underful hboxes.

If the whole premise of the exercise weren't so stupid (making it hard to justify the time investment), I'm sure it would be possible to fix the second issue mentioned above and also get rid of all the hard-coded lengths so the pyramid step size and other values would be parameters. I've wasted enough time accomplishing this bit of ugly formatting already, though, so I'll refrain.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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