[Tex/LaTex] How to use short subsection title in header but not in table of contents

fancyhdrheader-footer

I have a (onesided) document with fancyhdr and pagestyle fancy so it displays the section-title on the right header and the subsection-title on the left header. If section and subsection titles are lengthy they start to overlap each other.

Example:

\documentclass[a4paper,10pt]{scrartcl}
\usepackage[utf8x]{inputenc}
\usepackage{lipsum}
\usepackage{fancyhdr}
\pagestyle{fancy}

\begin{document}

\section{some section with quite a lengthy title}
\lipsum
\subsection{very very very long title of subsection}
\lipsum

\end{document}

I know of two solutions:

  1. give short section/subsection headings like in

    \documentclass[a4paper,10pt]{scrartcl}
    \usepackage[utf8x]{inputenc}
    \usepackage{lipsum}
    \usepackage{fancyhdr}
    \pagestyle{fancy}
    
    \begin{document}
    
    \section[short section]{some section with quite a lengthy title}
    \lipsum
    \subsection[short subsection]{very very very long title of subsection}
    \lipsum
    
    \end{document}
    

    I don't like this because I don't want to have the short version in my toc.

  2. use \sectionmark like in

    \documentclass[a4paper,10pt]{scrartcl}
    \usepackage[utf8x]{inputenc}
    \usepackage{lipsum}
    \usepackage{fancyhdr}
    \pagestyle{fancy}
    
    \begin{document}
    
    \section{some section with quite a lengthy title}
    \sectionmark{short section}
    \lipsum
    \subsection{very very very long title of subsection}
    \subsectionmark{short subsection}
    \lipsum
    
    \end{document}
    

    But this just doesn't work right – the short subsection mark is not obeyed on page 2, but is used correctly on page 3. That feels just really wrong.

So, how can I make version 2 work 'right'?

Best Answer

This is answered in the TeX FAQ. On the left-hand pages the text used in the title is that of the first mark on the page, so your \subsectionmark is coming too late. This can be solved by adding this command again in the title itself:

\documentclass[a4paper,10pt]{scrartcl}
\usepackage[utf8x]{inputenc}
\usepackage{lipsum}
\usepackage{fancyhdr}
\pagestyle{fancy}

\begin{document}
\tableofcontents

\section[some section with quite a lengthy title]{some section with quite 
a lengthy title%
\sectionmark{short section}}
\sectionmark{short section}
\lipsum

\subsection[very very very long title of subsection]{very very very long
title of subsection%
\subsectionmark{short subsection}}
\subsectionmark{short subsection}

\lipsum

\end{document}

Because of the way the writing to the table of contents works you will also have to add the full title to the optional argument of the \(sub)section command, as above. As this is rather clumsy, it is reasonable to introduced commands such as \markedsection to achieve this:

\documentclass[a4paper,10pt]{scrartcl}
\usepackage[utf8x]{inputenc}
\usepackage{lipsum}
\usepackage{fancyhdr}
\pagestyle{fancy}

\newcommand{\markedsection}[2]{\section[#2]{#2%
\sectionmark{#1}}
\sectionmark{#1}}

\newcommand{\markedsubsection}[2]{\subsection[#2]{#2%
\subsectionmark{#1}}
\subsectionmark{#1}}

\begin{document}
\tableofcontents

\markedsection{short section}{Some section with quite a lengthy title}

\lipsum

\markedsubsection{short subsection}{Very very very long title of subsection}

\lipsum

\end{document}

For the standard cases, you can still use the ordinary commands.

If your are prepared to switch to the memoir class, then you will have sectioning commands that help this proceedure as they have two optional arguments: one for the head and one for the table of contents.

enter image description here

Related Question