Trouble with patchcmd and double #
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).
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}