1

I could remove text decorations like highlighting or todos from the TOC but not from the header line. I did this without putting the plain text into the optional parameter of \chapter[]{} because pandoc is used to convert the text to latex and currently does not support optional parameters.

Here is a simple MWE:

\documentclass[oneside]{book}

\usepackage{xcolor}
\usepackage{soul}
\usepackage{todonotes}
\usepackage{lipsum}
\usepackage{ulem}
\DeclareRobustCommand{\nohl}[1]{#1}
\addtocontents{toc}{\begingroup%
  \protect\renewcommand{\protect\todo}[1]{}
  \let\hl\nohl
}
\AtEndDocument{%
  \addtocontents{toc}{\endgroup}
}

\begin{document}

\tableofcontents

{\let\clearpage\relax \chapter{Header with \hl{highlights} and \sout{deletions}}}

\section[A second header with a note]{A second header with a note\todo{keep it}}

\lipsum[1-2]

\end{document}

header decorations

How can I remove the highlighting in the header line (default and with fancyhdr) without using the optional parameter of \chapter? Or is it even possible to remove any latex command/macro and just keeping the inner text?

BTW: the \DeclareRobustCommand and \let\hl\nohl is a workaround because \protect\renewcommand{\hl}[1]{#1} produces the error ! Illegal parameter number in definition of \reserved@a.

Hood Chatham
  • 5,467

1 Answers1

1

You can check whether the command is going into the table of contents or headers with \ifx\protect\@unexpandable@protect <code for moving text> \else <normal code> \fi.

Note that in order for this to work, it's important that the macro so defined not be protected / "robust".

Try this:

\documentclass[oneside]{book}

\usepackage{xcolor}
\usepackage{soul}
\usepackage{todonotes}
\usepackage{lipsum}
\usepackage{ulem}

\makeatletter
\newcommand\ifmoving{%
    \ifx\protect\@unexpandable@protect
        \expandafter\@firstoftwo
    \else
        \expandafter\@secondoftwo
    \fi
}

\let\oldhl\hl
% If you used \DeclareRobustCommand or \protected\def it would not work.
\renewcommand\hl{\ifmoving{}{\oldhl}}
\makeatother


\begin{document}

\tableofcontents

{\let\clearpage\relax \chapter{Header with \hl{highlights} and \sout{deletions}}}

\section[A second header with a note]{A second header with a note\todo{keep it}}

\lipsum[1-2]

\end{document}
Hood Chatham
  • 5,467