0

I want to patch \addcontentsline command for changing \thepage to \thechapter--\thepage. However I found that things went wrong when I used expl block:

\documentclass{book}
\usepackage{etoolbox}
% \usepackage{hyperref}
\ExplSyntaxOn
\patchcmd{\addcontentsline}{\thepage}{\thechapter--\thepage}{\typeout{toc~succeed}}{\typeout{toc~fail}}
\ExplSyntaxOff
% \patchcmd{\addcontentsline}{\thepage}{\thechapter--\thepage}{\typeout{toc succeed}}{\typeout{toc fail}}
\begin{document}
  \tableofcontents
  \section{one}
  \chapter{text}
  \section{two}
\end{document}

If I didn't load hyperref package, the patch can be done both inside and outside expl block: terminal types tocsucceed;

enter image description here

If I load hyperref package, the patch can't be done inside expl block: terminal types tocfail, but can be done outside expl block.

Why would this happen?

Syvshc
  • 1,328
  • When patching all involved tokens must have the same catcode as at definition time. The definition of hyperref contains spaces and a colon, which have a different catcode in expl3. – Ulrike Fischer May 13 '22 at 18:30
  • @UlrikeFischer I see, there is a bookmark\temp@online:\MessageBreak in hyperref-defined \addcontentsline, so it breaks \patchcmd? Also, if I want to patch this in a explpackage, I have to use \ExplSyntaxOff to exit expl environment and finish it? – Syvshc May 14 '22 at 03:00
  • Ah you said space would also break \patchcmd in expl3, even a simple linebreak? So can I say that if I want to patch a command from latex2e, I should work under 2e environment? – Syvshc May 14 '22 at 03:57

0 Answers0