3

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[colorlinks]{hyperref}
\usepackage{xparse}
% my new \section macro
\LetLtxMacro\origsection\section
\RenewDocumentCommand\section{o o m}{%
  \IfValueTF{#1}{\def\titleintoc{#1}}{\def\titleintoc{#3}}%
  \IfValueTF{#2}{\def\titleinheader{#2}}{\def\titleinheader{\titleintoc}}%
  \pretitlemark{section}{\titleinheader}%
  \origsection[\titleintoc]{#3}%
  \sectionmark{\titleinheader}%
}
% page style
\newpagestyle{section-section}[]{
  \sethead[\firsttitlemarks\sectiontitle][][]{}{}{\firsttitlemarks\sectiontitle}
  \setheadrule{.4pt}
  \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: enter image description here

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

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

5
  • 1
    It seems the page shipout happens between \pretitlemark and \origsection. You need to ensure somehow that both happen on the same page.
    – cgnieder
    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
    – cgnieder
    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…
    – cgnieder
    May 4 '20 at 8:17
1
+200

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[colorlinks]{hyperref}
\usepackage{atbegshi}
\usepackage{xparse}
% my new \section macro
\LetLtxMacro\origsection\section
\RenewDocumentCommand\section{o o m}{%
  \IfValueTF{#1}{\def\titleintoc{#1}}{\def\titleintoc{#3}}%
  \IfValueTF{#2}{\def\titleinheader{#2}}{\def\titleinheader{\titleintoc}}%
  \AtBeginShipoutNext{\pretitlemark{section}{\titleinheader}}%
  \pretitlemark{section}{\titleinheader}%
  \origsection[\titleintoc]{#3}%
  \sectionmark{\titleinheader}%
}
% page style
\newpagestyle{section-section}[]{
  \sethead[\firsttitlemarks\sectiontitle][][]{}{}{\firsttitlemarks\sectiontitle}
  \setheadrule{.4pt}
  \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}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.