# Different toc and header titles (with titlesec and titleps)

I try to add a second optional argument to \section to specify different toc and header titles (like in the memoir class for example).

I use titlesec (and titleps via the pagestyles option). My code works fine except when the section is at the very beginning of a page (in my MWE, the fourth section shows the problem).

\documentclass[a4paper]{book}
\usepackage[pagestyles]{titlesec}
\usepackage{xparse}
% my new \section macro
\LetLtxMacro\origsection\section
\RenewDocumentCommand\section{o o m}{%
\IfValueTF{#1}{\def\titleintoc{#1}}{\def\titleintoc{#3}}%
\origsection[\titleintoc]{#3}%
}
% page style
\newpagestyle{section-section}[]{
\setfoot*{}{\thepage}{}
}
\pagestyle{section-section}
\usepackage{lipsum}
\begin{document}
\tableofcontents
\chapter{Test}
\lipsum
\section{section 1 everywhere}
\lipsum[10-19]
\section[section 2 in toc and in header]{section 2 in text}
\lipsum[10-20]
\section[section 3 in toc][section  3 in header]{section 3 in text}
\lipsum[10-20]
\section[section 4 in toc][section  4 in header]{section 4 in text}
\lipsum[10-19]
\end{document}


Top of page 8 with "section 3 in header" as expected:

Top of page of page 10 with "section 4 in toc" instead of "section 4 in header" (expected result):

How to patch titlesec (or titleps) to get the expected result?

• It seems the page shipout happens between \pretitlemark and \origsection. You need to ensure somehow that both happen on the same page. May 3 '20 at 7:03
• @cgnieder The page shipout occurs during \origsection (in the code of titlesec) but I don't know which command to patch to act at the right time. May 4 '20 at 4:38
• I usuallly use the needspace package when I need stuff to stay together. Indeed, \needspace{2\baselineskip} in the beginning of the new ´\section seems to work here May 4 '20 at 7:01
• @cgnieder Ok ... but the value 2\baselineskip is empirical and may fail in other cases. I'm looking for a solution that will work in any case. May 4 '20 at 7:13
• true. This is why I didn't mention it earlier… May 4 '20 at 8:17

You could execute \pretitlemark a second time at the begin of ship-out of the next page using \AtBeginShipoutNext of the atbegshi package.

\documentclass[a4paper]{book}
\usepackage[pagestyles]{titlesec}
\usepackage{atbegshi}
\usepackage{xparse}
% my new \section macro
\LetLtxMacro\origsection\section
\RenewDocumentCommand\section{o o m}{%
\IfValueTF{#1}{\def\titleintoc{#1}}{\def\titleintoc{#3}}%
\origsection[\titleintoc]{#3}%
}
% page style
\newpagestyle{section-section}[]{
\setfoot*{}{\thepage}{}
}
\pagestyle{section-section}
\usepackage{lipsum}
\begin{document}
\tableofcontents
\chapter{Test}
\lipsum
\section{section 1 everywhere}
\lipsum[10-19]
\section[section 2 in toc and in header]{section 2 in text}
\lipsum[10-20]
\section[section 3 in toc][section  3 in header]{section 3 in text}
\lipsum[10-20]
\section[section 4 in toc][section  4 in header]{section 4 in text}
\lipsum[10-19]
\end{document}