# \section's extra optional argument for a header title possibly different from the text and TOC ones: displayed too early

## Context

With the e.g. book class, the odd pages headers contain by default either the ToC or the text current section title, depending whether the optional argument of \section is used or not.

This is sometimes not efficient enough: suppose the text section title is quite long but would be quite less meaningful if abbreviated. In such a case, an abbreviated title in the ToC should be avoided and only the text title should be use. But therefore, in the odd pages headers, this text title would be probably too long and hence truncated.

To avoid this deficiency, I'm trying to redefine the \section command with 2 optional arguments, the second one being for a header title possibly different from the ToC and the text ones, according to the following outline:

                                    |----------------------------|
| Text   | ToC    |  Header  |
|-----------------------------------+--------+--------+----------|
| \section{⟨text⟩}                  |          ⟨text⟩            |
|----------------------------------------------------------------|
| \section[⟨ToC⟩]{⟨text⟩}           | ⟨text⟩ |      ⟨ToC⟩        |
|----------------------------------------------------------------|
| \section[][⟨header⟩]{⟨text⟩}      |      ⟨text⟩     | ⟨header⟩ |
|----------------------------------------------------------------|
| \section[⟨ToC⟩][⟨header⟩]{⟨text⟩} | ⟨text⟩ | ⟨ToC⟩  | ⟨header⟩ |
|----------------------------------------------------------------|


## Attempt of a solution

In my attempt below, the :

• ⟨text⟩ and ⟨ToC⟩ titles (displayed thanks to the titleps's \sectiontitle) are displayed at the right time (not before the page where the corresponding \section command is inserted), as it can be seen pages 5 and 11 of the PDF generated file: see the first two images below,
• ⟨header⟩ title is displayed too early (on the page preceeding the one where corresponding \section command is inserted), as it can be seen pages 17 and 23 of the PDF generated file: see the last two images below.

## Question

Do you see what is wrong is my attempt and how to make the ⟨header⟩ title displayed at the right time?

(Okay, I could use the memoir class which provides this feature, but anyway...)

\documentclass{book}
\usepackage{xparse}
\usepackage[pagestyles]{titlesec}
\usepackage[papersize=10cm]{geometry}
\usepackage{lipsum}
%
\ExplSyntaxOn
%
% Creation of the token lists
\tl_new:N \g__db_section_title_tl
\tl_new:N \g__db_toc_section_title_tl
%
% Copy the original section command
\cs_set_eq:NN \__db_original_section \section
%
% Redefinition of the section command (without star in order to keep it simple):
% - 1st optional argument: section title in the ToC
% - 2nd optional argument: section title in the header
% -    mandatory argument: section title in the text
\RenewDocumentCommand \section { o o m } {%
%
% Clear the ToC and header titles possibly previously defined
\tl_gclear:N \g__db_toc_section_title_tl
%
% If a ToC section title is specified, store it in \g__db_toc_section_title_tl
\IfNoValueF{#1}{
\tl_gset:Nn \g__db_toc_section_title_tl {#1}
}
%
% The text section title is store in \g__db_header_section_title_tl
\tl_gset:Nn \g__db_section_title_tl {#3}
%
% If no ToC section title is specified, make it the same as the text one
\tl_if_empty:NT \g__db_toc_section_title_tl {%
\tl_gset_eq:NN \g__db_toc_section_title_tl \g__db_section_title_tl
}
%
% Use the original section command with the previous text and ToC titles
\__db_original_section[\g__db_toc_section_title_tl]{\g__db_section_title_tl}
%
% If a header section title is specified, store it in \g__db_header_section_title_tl
% that will be used in the header of the page
\IfNoValueF {#2}{ \tl_gset:Nn \g__db_header_section_title_tl {#2} } }
%
% The header of a even pages contains the current chapter' title (with its
% number if any)
\ifthechapter{%
\thechapter.~%
}{%
}
\scshape\chaptertitle
}
%
% The header of a odd pages contains the current section' title (with its
% number if any) which is:
% - the \sectiontitle macro if the header section title isn't specified
% - the header section title if it is specified
\ifthesection{%
\thesection.~%
}{%
}
\sectiontitle
}{
}
}%
%
% Create a custom pagestyle with the previous headers
\newpagestyle{_db_pagestyle}[]{%
\setfoot{}{\thepage}{}%
}
\ExplSyntaxOff
%
% Use the custom previous pagestyle
\pagestyle{_db_pagestyle}
%
% Create a new command for the test
\newcommand{\test}[1]{%
\chapter{Test}
\lipsum[1-2]
#1
\lipsum[1]
}
%
\begin{document}
\tableofcontents
\test{\section                             {Section Title 1}}
\test{\section[Toc Title 2]                {Section Title 2}}
\test{\section[]           [Header Title 3]{Section Title 3}}
\test{\section[Toc Title 4][Header Title 4]{Section Title 4}}
\end{document}


## Too early: ⟨header⟩ section title

• The KOMA classes provide this feature too. Beside this your code is wrong. You are setting the header when the section command is seen which is often not the page where it is typeset. You must set the header through \mark command if it should be correct. – Ulrike Fischer Nov 24 '17 at 10:50
• @UlrikeFischer Could you elaborate? I don't see any \mark command in titleps. – Denis Bitouzé Nov 24 '17 at 11:03
• @Denis Bitouzé use \markboth command – Saravanan Nov 24 '17 at 11:22
• @Saravanan If titleps is used, it is discouraged to use \markboth (see § 3 "On \markboth and \markleft" of titleps's documentation). – Denis Bitouzé Nov 24 '17 at 11:31
• The marks are set with \sectionmark, which has a special definition in titlesec. Without titlesec it is more straightforward: tex.stackexchange.com/a/6866/2388 – Ulrike Fischer Nov 24 '17 at 12:16