# Trouble with patchcmd and double #

I need to patch the following macro from gloss-greek.ldf (polyglossia):

\def\greek@numbers{%
\let\latin@alph\@alph%
\let\latin@Alph\@Alph%
\if@greek@numerals
\def\greek@alph##1{\protect\greeknumber{##1}}%
\def\greek@Alph##1{\protect\Greeknumber{##1}}%
\let\@alph\greek@alph%
\let\@Alph\greek@Alph%
\fi}


to the following

\def\greek@numbers{%
\let\latin@alph\@alph%
\let\latin@Alph\@Alph%
\if@greek@numerals
\def\greek@alph##1{\protect\greeknumber{\the##1}}%
\def\greek@Alph##1{\protect\Greeknumber{\the##1}}%
\let\@alph\greek@alph%
\let\@Alph\greek@Alph%
\fi}


(to add the two \thes).

However I get problems related to different catcodes of #.

I've tried approaches like the ones mentioned in Patching arguments inside a macro but I have failed so far.

Working example:

\documentclass{memoir}

\usepackage{amsmath}
\usepackage{fontspec}
\usepackage{xpatch}

\usepackage{polyglossia}
\setdefaultlanguage{greek}
\setotherlanguage{english}
\newfontfamily\greekfont{Times New Roman}

\usepackage{xpatch}
\tracingpatches
\makeatletter
\newcommand{\patchme}{%
\xpatchcmd{\greek@numbers}{\greeknumber{##1}}{\greeknumber{\the##1}}{}{}
\xpatchcmd{\greek@numbers}{\Greeknumber{##1}}{\Greeknumber{\the##1}}{}{}
}
\makeatother

\begin{document}
\patchme
\begin{subequations}
\begin{align}
a &= b \label{eq:eq1}\\
c &= d \label{eq:eq2}
\end{align}
\end{subequations}

This is \eqref{eq:eq2}? Is it really?

\end{document}


If the patch were applied the reference would appear correct. Also the log reads:

[debug] tracing \patchcmd on input line 28
[debug] analyzing '\greek@numbers'
[debug] ++ control sequence is defined
[debug] ++ control sequence is a macro
[debug] -- nested patching command and parameters in patch
[debug] -> the patching command seems to be nested in the
[debug] argument to some other command
[debug] -> the patch text seems to contain # characters
[debug] -> either avoid nesting or use # characters with
[debug] category code 12 in the patch text
[debug] -> simply doubling the # characters will not work

• maybe something like \makeatletter \xpatchcmd{\greek@numbers}{\greeknumber{##1}}{\greeknumber{\the##1}}{}{} \xpatchcmd{\greek@numbers}{\Greeknumber{##1}}{\Greeknumber{\the##1}}{}{} \makeatother (note I did not try it)) – Steven B. Segletes Apr 14 '16 at 10:53
• @StevenB.Segletes it won't work – niels Apr 14 '16 at 10:56
• I don't believe in the "different catcodes of #". Show a complete document that demonstrates your problem. – Ulrike Fischer Apr 14 '16 at 10:59
• You can fix it directly, by avoiding the use of \xpatchcmd, and respecifying \def\greek@numbers as desired in the preamble. Is that solution not acceptable? I suppose your question may be about xpatch itself, rather than about the MWE, which can be fixed through other means. – Steven B. Segletes Apr 14 '16 at 11:44
• OK I take it back with the catcodes. If you are trying to patch they will matter. By why are you trying to redefine \@greeknumbers? I see no difference in the output if I do it. – Ulrike Fischer Apr 14 '16 at 12:47

Patching commands with ## with etoolbox (which xpatch uses internally) is not possible, as far as I can see.

I'm not sure why you need \the in that place, because the output is the same with or without it. Anyway, I'd prefer \number.

It is possible to do the patch with regexpatch. The patches can be done all at once, noting we want to replace every occurrence of {##1} with {\number##1}, so we can use \xpatchcmd*.

\documentclass{memoir}

\usepackage{amsmath}
\usepackage{fontspec}
\usepackage{regexpatch}

\usepackage{polyglossia}
\setdefaultlanguage{greek}
\setotherlanguage{english}
\newfontfamily\greekfont{Times New Roman}

\makeatletter
\xpatchcmd*{\greek@numbers}
{{##1}}
{{\number##1}}
{\typeout{Hurray!}}{\typeout{Rats!}}
\makeatother

\begin{document}

\begin{subequations}
\begin{align}
a &= b \label{eq:eq1}\\
c &= d \label{eq:eq2}
\end{align}
\end{subequations}

This is \eqref{eq:eq2}? Is it really?

\end{document}


Here's the relevant part of the terminal output:

(/usr/local/texlive/2015/texmf-dist/tex/latex/polyglossia/gloss-english.ldf)
Hurray!
(./doublehash.aux) (/usr/local/texlive/2015/texmf-dist/tex/latex/tipa/t3cmr.fd)
[1] (./doublehash.aux) )
Output written on doublehash.pdf (1 page).

• Your MWE gives the improper result, referencing eq 1-alpha', rather than 1-beta'... at least when I compile it. – Steven B. Segletes Apr 14 '16 at 14:13
• @StevenB.Segletes Timing problems; I removed \patchme – egreg Apr 14 '16 at 14:30
• \the was about to workaround a bug with polyglossia github.com/reutenauer/polyglossia/issues/110 The submitter proposes using \the. Would you suggest to go with \number? – niels Apr 14 '16 at 14:53
• Regarding the output at least in my set up (texlive 2015 updated) a get the wrong reference (see also Steven's final example) – niels Apr 14 '16 at 14:59
• @niels You might use \greek@numbers with an explicit number; in this case \the would give an error, whereas \number wouldn't. – egreg Apr 14 '16 at 16:34

If the problem is about how to fix the MWE, a fix can be had by avoiding the use of xpatch altogether, and merely respecifying, in total, the desired definition of \greek@numbers in the preamble.

\documentclass{memoir}

\usepackage{amsmath}
\usepackage{fontspec}

\usepackage{polyglossia}
\setdefaultlanguage{greek}
\setotherlanguage{english}
\newfontfamily\greekfont{Times New Roman}

\makeatletter
\def\greek@numbers{%
\let\latin@alph\@alph%
\let\latin@Alph\@Alph%
\if@greek@numerals
\def\greek@alph##1{\protect\greeknumber{\the##1}}%
\def\greek@Alph##1{\protect\Greeknumber{\the##1}}%
\let\@alph\greek@alph%
\let\@Alph\greek@Alph%
\fi}
\makeatother

\begin{document}
\begin{subequations}
\begin{align}
a &= b \label{eq:eq1}\\
c &= d \label{eq:eq2}
\end{align}
\end{subequations}

This is \eqref{eq:eq2}? Is it really?

\end{document}


If however, the real purpose of the OP's question is to understand the nature of why \xpatchcmd doesn't like ##1 in its arguments, then this answer sheds no light.

A simpler MWE to demonstrate the problem of ##1 in an \xpatchcmd macro is as follows. If the patch worked, the result would be Q(C), not B(C).

\documentclass{article}
\usepackage{xpatch}
\def\A{\def\B##1{B({##1})}}
\xpatchcmd{\A}{B({##1})}{Q({##1})}{\xdef\result{successful}}{\xdef\result{unsuccessful}}
\A
\begin{document}
\B{C}.  Patch was \result.
\end{document}


• You know you just made me feel retard, don't you? ;-) – niels Apr 14 '16 at 12:16
• Hardly. The xpatch issue is a fascinating question, too. – Steven B. Segletes Apr 14 '16 at 12:17