# Place theorem number *before* theorem label in page header?

In the accepted answer to Put current theorem-like item's name/number in header, it is shown how to put in the page's header the label of the (last-on-page) theorem, lemma, etc. For example, if the last such item is "Theorem 3.2", then that's what appears in the page's header.

I'm using the \swapnumbers with the amsthm package, so such an item appears in the body of the page as "3.2 Theorem". But the corresponding header still appears as "Theorem 3.2".

Is it possible to swap also in the number with the theorem-like label ("Theorem", etc.)?

You must pay attention to the fact that \swapnumbers could be issued several times between theorem definitions, being effectively part of the theorem style (indeed, it acts as a “style modifier”). Therefore, the correct way of coping with the problem is to use \thm@swap (in its “inner” meaning!) to select between two predefined “marking styles”.

Here is a new version of the code that uses this strategy to provide full compatibility with \swapnumbers (edited to correct—I hope—the bug reported in Include in header theorem-like item name with math letter in optional argument):

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

\usepackage{amsthm} % comment or uncomment as you prefer
\usepackage{amsfonts} % for "\mathbb"

\usepackage{fancyhdr}
\usepackage{lipsum}

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

\newtheorem{theorem}{Theorem}[section] % or "[chapter]", or whatever
\newtheorem{lemma}  {Lemma}  [section]

% A little trick to make this example work both with amsthm and without it;
% once you've made your choice, you do not need this code.
\makeatletter
\@ifdefinable\IfAmsThm{}
\let \IfAmsThm = \@firstoftwo
}{
\let \IfAmsThm = \@secondoftwo
}
\makeatother

\IfAmsThm{
\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:
}{% replace with:
\thm@swap\my@swapped@mark\my@direct@mark{#1}{#2}{#3}%
}{% execute if succeeded:
\typeout{>>> Made patch specific for amsthm.}
}{% execute if failed:
\typeout{>>> Patch specific for amsthm FAILED!}
}
\else
\apptocmd{\@begintheorem}     {\myMark{#1\ #2}\ignorespaces}{}{}
\apptocmd{\@opargbegintheorem}{\myMark{%
#1\ #2%
\ (#3)% comment this line to leave Mickey Mouse out
\ignorespaces
}}{}{}
\fi
\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}$]
The third theorem of the first section.
\end{theorem}

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

% \begingroup
%   \showboxdepth = 10
%   \tracingonline = 1
%   \showlists
% \endgroup

\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}$]
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}$]
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}

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

% \lipsum[85-96]

\end{document}


Once again, there’s no point in showing here the output: you must try the code by yourself and check that it behaves as expected.

• No, that doesn't work: with or without \swapnumbers, in the header the theorem-like item appears in form "name m.n", with number after the item's name. I asked about reversing that. – murray Jul 24 '16 at 15:36
• @murray: I have tried several combinations, and in all cases the output I get seems absolutely OK to me. When the theorem is titled “Theorem/Lemma/Definition m.n” in the text, the same happens in the running heads atop pages; when the title has the form ”m.n Theorem/Lemma/Definition” (i.e., with the number before the name), again the same format is used in the running heads too. In other words, I cannot see anything wrong… :-/ – GuM Jul 27 '16 at 23:44
• The fix for the issue reported in tex.stackexchange.com/questions/324834/… works perfectly. – murray Aug 16 '16 at 0:32

In the lines

\apptocmd{\@begintheorem}     {\myMark{#1\ #2}\ignorespaces}{}{}
\apptocmd{\@opargbegintheorem}{\myMark{%
#1\ #2%
\ (#3)% comment this line to leave "Mickey Mouse" out
\ignorespaces
}}{}{}


swap #1 with #2.

• I'm using amsthm with \swapnumbers, and that modification does not do what I asked: on recto page headers, put the theorem-like item's number before its name (Theorem, Lemma, etc.) – murray Jul 24 '16 at 15:35
• However, what does seem to work is swapping #1 and #2 in  \myMark{#1\@ifnotempty{#2}{\ #2}\@ifnotempty{#3}{\ (#3)}}%. – murray Jul 24 '16 at 15:40