# Sectionmark escaping and numbering problem

I have a document, where I wanted to use \sectionmark to make the heading shorter, but keep the long section title in TOC. Unfortunately, doing that is wrong (the long title is output for header) if the page starts ... somewhere (I don't quite get this, sorry for the poor description).

So as per problems with sectionmark • Page Layout • LaTeX Community, I tried "doubling" the \sectionmark, but then that breaks the document ("TeX capacity exceeded", or "! Argument of \@sect has an extra }.").

Finally I found a construct with \texorpdfstring and \protect which passes the compilation:

\documentclass[a4paper]{book}

\usepackage{lipsum}
\usepackage{hyperref}
\usepackage{fancyhdr}

\begin{document}

\section{A first section is ok}

\lipsum[1-12]

\makeatletter
\@starttoc{toc}
\makeatother

% %this construct breaks in my doc ("TeX capacity exceeded"), in this MWE breaks with "! Argument of \@sect has an extra }.":
% \section{A new, very very very long, impossibly long, title, with no intention of being shorter\sectionmark{A new, very very very long, impossibly long, title}}
% \sectionmark{A new, very very very long, impossibly long, title}

\section{A new, very very very long, impossibly long, title, with no intention of being shorter%
\texorpdfstring{\protect\sectionmark{A new, very very very long, impossibly long, title}}{}}%
\label{sect:ahaa}
\sectionmark{A new, very very very long, impossibly long, title}

\lipsum[4]

\end{document}


... but that now gives the wrong section number in the header:

As you can see, the heading says "0.1. A new, very very...", while it should say "0.2. A new, very very...".

How can I get the correct section number to show, also on this page where the section appears first?

• I am a bit confused, why aren't you using the optional argument? \section[for toc and head]{the very long and verbose title that is written within the doc} – Johannes_B Jun 15 '15 at 15:12
• @Johannes_B: He wants to have the long title in the ToC too – user31729 Jun 15 '15 at 15:14
• @ChristianHupfer Ah, somehow overred this. @<>sdaau How about using a KOMA-class? What you want is achieved there very very easily. – Johannes_B Jun 15 '15 at 15:16
• The problem is, that \addcontentsline from within \@sect tries to use #7 then and that will write an \sectionmark into the toc. – user31729 Jun 15 '15 at 15:27

Try this with an additional optional argument for the \section macro, holding the \sectionmark.

\documentclass[a4paper]{book}

\usepackage{blindtext}
\usepackage{fancyhdr}

\usepackage{xpatch}%

\usepackage{xparse}

\usepackage{hyperref}

\let\LaTeXStandardSection\section

\newif\ifinternalsectionmark
\internalsectionmarkfalse

\xpatchcmd{\@sect}{%
\csname #1mark\endcsname{#7}%
}{%
\ifinternalsectionmark
\csname #1mark\endcsname{#7}%
\fi
\internalsectionmarktrue
}{}{}

\RenewDocumentCommand{\section}{s+o+m+o}{%
\IfBooleanTF{#1}{%
\LaTeXStandardSection*{#3}
}{%
\IfValueTF{#2}{%
\LaTeXStandardSection{#2}%
}{%
\IfValueTF{#4}{%
\internalsectionmarkfalse
\LaTeXStandardSection{#3}%
\sectionmark{#4}%
}{%
\LaTeXStandardSection{#3}%
}%
}%
}%
}

\begin{document}

\section{A first section is ok}

\blindtext[12]

\makeatletter
\@starttoc{toc}
\makeatother

% %this construct breaks in my doc ("TeX capacity exceeded"), in this MWE breaks with "! Argument of \@sect has an extra }.":
\section{A new, very very very long, impossibly long, title, with no intention of being shorter}[A shorter sectionmark]

\blindtext[10]

\clearpage

\section{A new, very very very long, impossibly long, title, with no intention of being shorter%
\texorpdfstring{\protect\sectionmark{A new, very very very long, impossibly long, title}}{}}%
\label{sect:ahaa}

\blindtext[10]

\end{document}


EDIT: caveat emptor: this answer seems to be wrong - i.e. the only reason it works, is because the tableofcontents is dumped right before that section; if you move that part earlier, then it will not work..

Eh, apparently we can use the old \let\oldcmd\cmd ... \let\cmd\oldcmd hack, but here with counters (so if anyone has a better solution, I'd love to hear it).

That is, we can temporarily increase the section counter just for the "inner" \sectionmark, then decrease it - and since that ends up written in the .aux .toc file, we have to sprinkle generously with \protects too:

\documentclass[a4paper]{book}

\usepackage{lipsum}
\usepackage{hyperref}
\usepackage{fancyhdr}

\begin{document}

\section{A first section is ok}

\lipsum[1-12]

\makeatletter
\@starttoc{toc}
\makeatother

% %this construct breaks in my doc ("TeX capacity exceeded"), in this MWE breaks with "! Argument of \@sect has an extra }.":
% \section{A new, very very very long, impossibly long, title, with no intention of being shorter\sectionmark{A new, very very very long, impossibly long, title}}
% \sectionmark{A new, very very very long, impossibly long, title}

\section{A new, very very very long, impossibly long, title, with no intention of being shorter%