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.

I have some long section names that span more than one line, so I break them into two lines with \\ (I am using fancyhdr for that to work). The result is not very aesthetic.

What I would like to have is that for multiline sections, the first line of the section will be aligned to the left as usual, but the second line be aligned to the right (similar to a multline equation). I would like the same behavior to carry also to the header and to the table of content.

share|improve this question
add comment

2 Answers

fancyhdr breaks long section names for you already - remove your manual lines breaks and it should look good.

Otherwise, please provide an example of the problem.

share|improve this answer
    
The OP's request is to switch from left-aligned to right-aligned after the first line. –  lockstep Oct 3 '11 at 22:27
add comment

The trick is to set both \leftskip and \rightskip to have infinite stretchable space that cancel each other out. For the last line, the \parfillskip will also be used, so you can cancel the effect of \rightskip. Basically, you just have to write

\leftskip=0pt plus 1fil %
\rightskip=0pt plus -1fil %
\parfillskip=0pt plus 1fil % 

where you want to use this formatting. You can usually omit the \parfillskip definition, as the above value is usually the default. Here’s a complete example, showing the effect on section headings.

\documentclass[11pt]{article}

\usepackage{lipsum}

\usepackage{sectsty}
\sectionfont{\leftskip=0pt plus 1fil \rightskip=0pt plus -1fil}

\begin{document}

\section*{This is a short heading}

\lipsum[4]

\section*{This a long (but not an extremely long) heading, spanning span two lines}

\lipsum[10]

\section*{This an extremely long heading, even longer than the one above, and spanning not one, not two, but three lines}

\lipsum[2]

\end{document}

Image showing the result of the above code.

Note that all lines except the last are justified, as in the normal LaTeX classes, which might look ugly, with large interword spaces. To make them ragged-right instead, use

\leftskip=0pt plus 1fil %
\rightskip=0pt plus 1fill %
\parfillskip=0pt plus -1fill %

However, if the last line is very short, you do risk that the first word in the last line starts to the right of the last word(s) in the preceding line(s), making the last line appear not to be connected to the rest of section heading.

In other words, either having the first part of the section heading justified or having it ragged-right may cause ugly headings, so you might want to reconsider this formatting choice.

For the header, just use the same commands. Example, using the fancyhdr package (and a numbered and slightly longer section title):

\fancyhead{}
\fancyhead[L]{\leftskip=0pt plus 1fil \rightskip=0pt plus -1fil \leftmark}

With the default settings of fancyhdr, but using numbered sections, it looks like this:

Image showing the result of the above heading code

For the ToC, the code to use depends on which classes (book/Koma-Script/memoir/…) and/or packages (tocloft/titletoc/…) you use, but I would strongly recommend just using a shorter version of the title instead. Example:

\section[Extremely long heading]{This an extremely long heading, even longer than the one above, and it's spanning not one, not two, but three lines}

The short version will by default also be used in the header.

share|improve this answer
    
So, with the main aim to be a modification of the header and ToC, why don't you include this in your MWE rather than just saying "The same trick should work for the header too"? –  Werner Oct 30 '11 at 19:03
    
@Werner I’m not sure I understood the original poster. Isn’t he also/mainly talking about formatting of the actual section title, as shown in my example? I can of course try to provide code for the header, but I’d like to see the fancyhdr code the poster uses (and which doesn’t work ) first, to see how it should be modified. For the TOC, the code depends on which class (article/Koma-Script/memoir) one uses. –  Karl Ove Hufthammer Oct 30 '11 at 19:14
    
I also understand that multiline sections is a requirement, as you've shown. Would you be able to provide fancyhdr code that works in a similar way even if the OP doesn't supply his/hers? –  Werner Oct 30 '11 at 19:19
    
@Werner OK, I’ve now added a very simple (and quite ugly) example for the header. –  Karl Ove Hufthammer Oct 30 '11 at 20:06
    
I’ve also added some information on how to make the first part of the section heading ragged-right instead of justified, which may or may not look better. –  Karl Ove Hufthammer Oct 31 '11 at 15:51
add comment

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.