12

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.

4
  • 2
    Your second approach works for me and produces the desired result.
    – lockstep
    Commented Jan 31, 2012 at 21:03
  • 1
    Actually, you should turn your second approach into an answer. You'd have my upvote. ;-)
    – lockstep
    Commented Jan 31, 2012 at 21:10
  • the Update: variant works for me too I think.
    – yo'
    Commented Jan 31, 2012 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
    Commented Jan 31, 2012 at 22:10

4 Answers 4

8

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).

3
  • Very nice approach. I thought about something similar simpler but less robust …
    – Tobi
    Commented Feb 1, 2012 at 0:21
  • Why does section[X]{X\sectionmark{Y}} \sectionmark{Y} work but section{X\sectionmark{Y}} \sectionmark{Y} doesn't?
    – gghuffer
    Commented Feb 16, 2017 at 23:07
  • @gghuffer Because the mark has to be issued twice in order to comply with the request.
    – egreg
    Commented Feb 16, 2017 at 23:09
8

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}
7

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.

1
  • Didn’t know that. Thanks. But I can’t use titlesec :-( furthermor your approche needs also a change of my code …
    – Tobi
    Commented Feb 1, 2012 at 13:34
0

In KOMA-Script it is now possible to use a keyval list as optional argument for \section etc. to set the ToC entry and page head differently. First one must activate this by using headings = optiontoheadandtoc|optiontohead|optiontotoc as class option:

  • optiontoheadandtoc: same as default behavior, i.e. the optional argument is used for ToC and head mark, but the optional keyval syntax is enabled.
  • optiontohead: optional argument is only used for the head mark (and the optional keyval syntax is enabled)
  • optiontotoc: optional argument is only used for ToC (and the optional keyval syntax is enabled)

With the optional keyval syntax one can use head={head mark} and/or tocentry={toc}:

\documentclass[%
   headings = optiontoheadandtoc,% or 'optiontohead' or 'optiontotoc'
]{scrbook}

\usepackage{lipsum}

\begin{document}
\tableofcontents
\chapter{Headline}
\lipsum[1-10]
\chapter[ToC and Page Head]{Headline}
\lipsum[1-10]
\chapter[tocentry={ToC}]{Headline}
\lipsum[1-10]
\chapter[head={Page Head}]{Headline}
\lipsum[1-10]
\chapter[tocentry={ToC},head={Page Head}]{Headline}
\lipsum[1-10]
\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .