7

I want to add changebars to toc-entries. It actually works fine but I had to disable the command in the \mark commands (so that changebar settles and stops the "rerun" messages in the log-file). The following works but I'm wondering why I have to patch two commands. Why isn't it enough to patch \markboth?

\documentclass{article}
\usepackage{etoolbox}
%must be before babel, as babel redefines markboth:
\patchcmd{\markboth}{\begingroup}{\begingroup\let\cbline\relax}{}{}
\pretocmd{\leftmark}{\let\cbline\relax}{}{}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[ngerman]{babel}
\usepackage{changebar}
\newcommand\cbline{}
\DeclareRobustCommand\cbline{\cbstart\cbend}

\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{}
\lhead{\leftmark}

\begin{document}
\tableofcontents

\section[toc\cbline]{Document}

abc\cbline
\end{document}

Edit

Using \def instead of \let when patching \markboth works:

\patchcmd{\markboth}{\begingroup}{\begingroup\def\cbline{}}{}{}

But I still don't understand why it is necessary as \markboth uses \let for label and index command.

Ulrike Fischer
  • 327,261

1 Answers1

1

If you do \let\cbline\relax, in a write operation or in an \edef, \cbline will be written as itself.

So with \let, TeX finds again \cbline when typesetting the mark and it uses the current definition, which is \protect\cbline  which becomes \cbstart\cbend.

With \def\cbline{}, the \edef will make \cbline disappear.

Relevant part of the log with \tracingmacros=1:

With \def\cbline{}

\@arabic #1->\number #1
#1<-\c@section

\cbline ->

\@themark ->{\protect \foreignlanguage {ngerman}{\protect \bbl@restore@actives
\protect \MakeUppercase  {1\hskip 1em\relax toc}}}{}

With \let\cbline\relax

\@arabic #1->\number #1
#1<-\c@section

\@themark ->{\protect \foreignlanguage {ngerman}{\protect \bbl@restore@actives
\protect \MakeUppercase  {1\hskip 1em\relax toc\cbline }}}{}

As you see, the latter still has \cbline.

egreg
  • 1,121,712
  • That's one part of the explanation. But what bothered me most was that \let\label\relax works as expected in \markboth. But I now found the other part: the output routine contains \let\label\@gobble. – Ulrike Fischer May 31 '13 at 09:39