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\sectioncommand 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
\sectioncommand 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
\tl_new:N \g__db_header_section_title_tl
\tl_new:N \g__db_even_header_tl
\tl_new:N \g__db_odd_header_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
\tl_gclear:N \g__db_header_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)
\tl_gset:Nn \g__db_even_header_tl {
\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
\tl_gset:Nn \g__db_odd_header_tl {
\ifthesection{%
\thesection.~%
}{%
}
\tl_if_empty:NTF \g__db_header_section_title_tl {%
\sectiontitle
}{
\g__db_header_section_title_tl
}
}%
%
% Create a custom pagestyle with the previous headers
\newpagestyle{_db_pagestyle}[]{%
\sethead[\g__db_even_header_tl][][]{\g__db_odd_header_tl}{}{}%
\setfoot{}{\thepage}{}%
\headrule%
}
\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}




\markcommand intitleps. – Denis Bitouzé Nov 24 '17 at 11:03\markbothcommand – Saravanan Nov 24 '17 at 11:22titlepsis used, it is discouraged to use\markboth(see § 3 "On\markbothand\markleft" oftitleps's documentation). – Denis Bitouzé Nov 24 '17 at 11:31\sectionmarkdoes the trick. Do you plan to elaborate in an answer (otherwise I could do it)? – Denis Bitouzé Nov 24 '17 at 15:06