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 need to typset a book with very long section names. The long names should appear in the normal text and in the ToC but not in the header, so I can’t use the optional argument. \sectionmark is my fried but only for the page after the \section started because the first marking command on a page wins the place in the header.

Is it possible to set the header of the current page even after a \section set a header already?

\documentclass{book}

\usepackage{lipsum}

\begin{document}
    \chapter{Test}
    \lipsum
    \section{A Section}
    \lipsum[3-5]
    \section{A very long section title, which doesn't
             fit in the header because it's extremly long}
    \sectionmark{Short title}
    \lipsum[1-14]
\end{document}

The example is very minimalistic. The template I must use uses fancyhdr to change the layout.


I found this answer but it works only for chaperts because they always happen to be on a right page and so \chaptermark gives the desired result on the next left page.


Follow up question to my answer

Is ist possible to make a (Perl or whatever running on Mac OS X) script that converts

\section{long text}
\sectionmark{short text}

to

\section[long text]{long text\sectionmark{short text}}

or alternatively to

\section{long text}[short text]

I can’t ensure that there are no line breaks in the long text or if there is a break between \section and \sectionmark.

I asked the follow up question again here on StackOverflow.

share|improve this question
2  
Your second approach works for me and produces the desired result. –  lockstep Jan 31 '12 at 21:03
1  
Actually, you should turn your second approach into an answer. You'd have my upvote. ;-) –  lockstep Jan 31 '12 at 21:10
    
the Update: variant works for me too I think. –  tohecz Jan 31 '12 at 21:32
    
@lockstep: I forgot to delete the aux files O:-) But I wonder if there would be another method because the book is finished and it would be great if I don’t have to change the whole code manually … –  Tobi Jan 31 '12 at 22:10

3 Answers 3

up vote 5 down vote accepted

A possibility is to have a syntax such as

\section[toc entry]{title}[header entry]

that is similar to yours. Here's a way:

\documentclass{book}

\usepackage{xparse,lipsum}

\makeatletter
\let\tobi@kept@section\section
\RenewDocumentCommand{\section}{s}
  {\IfBooleanTF{#1}{\tobi@kept@ssection*}{\tobi@section}}
\NewDocumentCommand{\tobi@section}{o m o}
  {\IfNoValueTF{#3}% No short title for the header
     {\IfNoValueTF{#1}% No title for the toc
        {\tobi@kept@section{#2}}
        {\tobi@kept@section[#1]{#2}}%
     }
     {\IfNoValueTF{#1}% No title for the toc
        {\tobi@kept@section[#2]{#2\sectionmark{#3}}\sectionmark{#3}}
        {\tobi@kept@section[#1]{#2\sectionmark{#3}}\sectionmark{#3}}%
    }%
  }
\makeatother

\begin{document}
\tableofcontents
\chapter{Test}
\lipsum

\section{A Section}

\lipsum[3-5]

\section
  [A very long section title, which doesn't fit
   in the header because it's extremely long]
  {A very long section title, which doesn't fit\\
   in the header because it's extremly long}[Short title]

\lipsum[1-12]

\section
  {A very long section title, which doesn't fit
   in the header because it's extremly long}[Short title, but longer]

\lipsum[1-14]

\end{document}

The approach of memoir is

\section[toc entry][header entry]{title}

but I prefer to have later the header entry (it's also easier to realize it with xparse).

share|improve this answer
    
Very nice approach. I thought about something similar simpler but less robust … –  Tobi Feb 1 '12 at 0:21

I found an approach here (vie the linked page). Seems to work but I wonder if there is a way without changing my code?

\documentclass{book}

\usepackage{lipsum}

\begin{document}
    \tableofcontents
    \chapter{Test}
    \lipsum
    \section{A Section}
    \lipsum[3-5]
    \section[% necessary to keep \sectionmark away from ToC
        A very long section title, which doesn't fit in
        the header because it's extremly long
    ]{%
        A very long section title, which doesn't fit in
        the header because it's extremly long%
        \sectionmark{Short title}% changes the head for the current page
    }
    \sectionmark{Short title}% changes the head for following pages
    \lipsum[1-14]
\end{document}
share|improve this answer

If using titlesec is possible, just write:

\usepackage[toctitles]{titlesec}

With that, the optional argument in sectioning commands only is used for the header, while for the TOC entry the full title is used.

share|improve this answer
    
Didn’t know that. Thanks. But I can’t use titlesec :-( furthermor your approche needs also a change of my code … –  Tobi Feb 1 '12 at 13:34

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.