# Include in header theorem-like item name with math letter in optional argument

In Put current theorem-like item's name/number in header, Gustavo Mezzetti posted an answer that allows including in the page header the current theorem-like item's name and number.

The problem

However, that answer fails when the \theorem, etc. takes an optional argument that includes certain math alphabetical characters, including \mathbb (but is OK for normal math letters).

I suspect that the additional fix needed is to \protect something, but I'm unsure what. Any suggestions?

Test example

This example shortens Mezzetti's code so as to eliminate irrelevancies (e.g., test as to whether amsthm is loaded).

\documentclass{book}

\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{amsfonts}

\newtheorem{theorem}{Theorem}
\newtheorem{corollary}[theorem]{Corollary}

\usepackage{etoolbox} % needed for headings wizardry, below

\usepackage{fancyhdr}

\fancyhf{}
\fancyhead[LE,RO]{\thepage}
\fancyhead[RE,LO]{\myBotmark} % "RE" just for testing purposes
\pagestyle{fancy}

%%%%%%%% BEGIN WIZARDRY %%%%%%%%

\makeatletter

\@ifdefinable\@my@claim@mark{\newmarks\@my@claim@mark}
\newcommand*\myMark[1]{\marks\@my@claim@mark{#1}}
\newcommand*\myBotmark{\botmarks\@my@claim@mark}

\patchcmd{\@begintheorem}{% search for:
\thm@swap\swappedhead\thmhead % more specific than before
}{% replace with:
\myMark{#1\@ifnotempty{#2}{\ #2}\@ifnotempty{#3}{\ (#3)}}%
\thm@swap\swappedhead\thmhead
}

\makeatother

%%%%%%%%  END WIZARDRY  %%%%%%%%

\begin{document}

Denote the reals by $\mathbb{R}$.

\begin{theorem}[about \protect $\mathbb{R}$] % gives error
%\begin{theorem}[about $R$] % so would this be OK
%\begin{theorem}[about reals] % this would be OK

Big result!

\end{theorem}

\newpage

%\begin{corollary}[special case for $\mathbb{Q}$]
\begin{corollary}[special case for $Q$]
%\begin{corollary}[special case for rationals]

Another assertion.

\end{corollary}

\end{document}


The error from running pdflatex on that is:

...42: Undefined control sequence.
\GenericError   ...
#4   \errhelp \@err@   ...
l.42   \begin{theorem}[about \protect $\mathbb{R}$]
% gives error


A clue to ultimate error cause?

Comment out all reference to fancyhdr and comment out all the patch code related to that, but now insert

\usepackage{thmtools}


in the preamble and

\listoftheorems


in the document body. Then having $\mathbb{R}$ in the theorem's optional argument causes no error in typesetting the List of Theorems — even without any \protect in that optional argument.

All this suggests the error stems from something in the code's "wizardry" that interacts with amsthm or fancyhdr.

• try entering this as $\protect\mathbb$. – barbara beeton Aug 14 '16 at 23:57
• Nope, putting \protect into the optional \begin{theorem} argument does not eliminate the error. That's one of the things I did already try, without success; should have said that in OP. – murray Aug 15 '16 at 0:44

## 1 Answer

Before referring you to my previous, and now updated answer, allow me to draw your attention to some facts:

1. The code \protect $\mathbb{R}$ is wrong for (at least) two reasons, the first being that \protect applies only to the following token, in this case $; but $ is robust by itself, since it is a primitive command of TeX and, as such, an unexpandable token.

2. Even $\protect\mathbb{R}$ would be wrong, anyway, or rather, useless, because \mathbb is already made robust by the LaTeX kernel.

3. \patchcmd takes five arguments:

\patchcmd{<cmd>}{<search>}{<replace>}{<success>}{<failure>}


where <success> and <failure> are bits of code you want to execute if \patchcmd succeeds or fails, respectively. If you don’t want to execute any code, leave them empty, but you cannot simply omit them. In your example, you have been very lucky that a \par and the \makeatother were gobbled in their place…

For your convenience, I’ll also leave here the code streamlined for use with the amsmath package. I invite you to try it out: I have added \protect management in the \myMark command. Omitting to do so in previous versions was certainly a mistake: see if it now works.

\documentclass[a4paper,twoside]{article}
\usepackage[T1]{fontenc} % Not necessary, but recommended.
\usepackage{etoolbox}

\usepackage{amsthm} % we have decided to go for this one
\usepackage{amsfonts}

\usepackage{fancyhdr}
\usepackage{lipsum}

\fancyhf{}
\fancyhead[LE,RO]{\thepage}
\fancyhead[RE,LO]{\myBotmark} % "RE" just for testing purposes
\pagestyle{fancy}

\newtheorem{theorem}{Theorem}[section] % or "[chapter]", or whatever
\newtheorem{lemma}  {Lemma}  [section]
\newtheorem*{spclaim}{Special Claim}
\swapnumbers
\theoremstyle{definition}
\newtheorem{defin}{Definition}[section]

%%%%%%%% BEGIN WIZARDRY %%%%%%%%

\makeatletter

\@ifdefinable\@my@claim@mark{\newmarks\@my@claim@mark}
\newcommand*\myMark[1]{% <<< MODIFIED
\begingroup
\let\label\relax \let\index\relax \let\glossary\relax
\let\protect\@unexpandable@protect
\marks\@my@claim@mark{#1}%
\endgroup
}
\newcommand*\myBotmark{\botmarks\@my@claim@mark}

\typeout{****************************************}
\newcommand*\my@direct@mark[3]{%
\myMark{%
\thmname{#1}%
\thmnumber{\thmname{\ }#2}%
\thmnote{\ (#3)}%
}%
}
\newcommand*\my@swapped@mark[3]{%
\myMark{%
\thmnumber{#2}%
\thmname{\thmnumber{\ }#1}%
\thmnote{\ (#3)}%
}%
}
\patchcmd{\@begintheorem}{% search for:
\thm@swap\swappedhead\thmhead % more specific than before
}{% replace with:
\thm@swap\my@swapped@mark\my@direct@mark{#1}{#2}{#3}%
\thm@swap\swappedhead\thmhead
}{% execute if succeeded:
\typeout{>>> Made patch specific for amsthm.}
}{% execute if failed:
\typeout{>>> Patch specific for amsthm FAILED!}
}
\typeout{****************************************}

\makeatother

%%%%%%%%  END WIZARDRY  %%%%%%%%

\begin{document}

\section{First section}

\lipsum[1]

\begin{theorem}
This is the first theorem of the first section.
\end{theorem}

\lipsum[2]

\begin{theorem}
This is the second theorem of the first section.
\end{theorem}

\begin{theorem}[Donald Duck and~$\mathbb{Q}$]
This is the third theorem of the first section.
\end{theorem}

\begin{spclaim}
This is a special claim.
\end{spclaim}

\lipsum[3-16]

\begin{theorem}
This is the fourth theorem of the first section.
\end{theorem}

\begin{defin}[Something new]
This is the first definition of the first section.
\end{defin}

\begin{defin}
This is the second definition of the first section.
\end{defin}

\lipsum[17]

\begin{lemma}
This is the first lemma of the first section.
\end{lemma}

\lipsum[18]

\begin{theorem}
This is the fifth theorem of the first section.
\end{theorem}

\lipsum[19-20]

\begin{lemma}[Uncle Scrooge and~$\mathbb{R}$]
This is the second lemma of the first section.
\end{lemma}

\lipsum[21-32]

\begin{theorem}
This is the sixth theorem of the first section.
\end{theorem}

\lipsum[33-36]

\begin{theorem}
This is the seventh theorem of the first section.
\end{theorem}

\begin{lemma}
This is the third lemma of the first section.
\end{lemma}

\begin{theorem}[Mickey Mouse and~$\mathbb{C}$]
This is the eighth theorem of the first section.
\end{theorem}

\lipsum[37-40]

\begin{theorem}
This is the ninth theorem of the first section.
\end{theorem}

\lipsum[41-48]

\begin{defin}
This is the third definition of the first section.
\end{defin}

\lipsum[49-64]

\begin{theorem}[The last one]
This is the tenth theorem of the first section.
\end{theorem}

\lipsum[65-80]

\begin{lemma}
This is the fourth lemma of the first section.
\end{lemma}

\begin{lemma}
This is the fifth lemma of the first section.
\end{lemma}

\begin{lemma}
This is the sixth lemma of the first section.
\end{lemma}

\begin{lemma}
This is the seventh lemma of the first section.
\end{lemma}

\begin{lemma}
This is the eighth lemma of the first section.
\end{lemma}

\begin{lemma}
This is the ninth lemma of the first section.
\end{lemma}

\begin{lemma}[Deep breath]
This is the tenth lemma of the first section.
\end{lemma}

\lipsum[81-84]

\begin{lemma}
This is the eleventh lemma of the first section.
\end{lemma}

\begin{lemma}
This is the twelfth lemma of the first section.
\end{lemma}

\begin{lemma}
This is the thirteenth lemma of the first section.
\end{lemma}

\begin{lemma}
This is the fourteenth lemma of the first section.
\end{lemma}

\begin{lemma}
This is the fifteenth lemma of the first section.
\end{lemma}

\begin{lemma}[Hexadecimal deep breath]
This is the sixteenth lemma of the first section.
\end{lemma}

% \lipsum[85-96]

\end{document}


Please note that I intend to remove this answer as soon as you notify me that you have read it: it is actually a duplicate of my other answer to which it refers.

• @murray: It looks like this answer, streamlined for amsthm, meets with more success than the other one… :-) Which one should I remove? – GuM Aug 19 '16 at 2:46
• I much prefer the "streamlined" version specifically for amsthm, since that's the package I always use for theorem-like environments. I do note that with my actual book-length multi-file document (using memoir as documentclass), the marks that include the theorem number & name will not appear at all in a chapter when the 1st instance of a theorem-like environment is not until the 2nd or 3rd pages of the chapter -- unless that theorem-like environment is actually "theorem" rather than "definition". I have tried to reproduce this in a simple, single-file MWE but have not yet succeeded. – murray Aug 20 '16 at 1:33