I want to patch some command in a numbered list based on the current level (enumi, enumii, ...). Basically, I want to replace, in a command the name of which is stored in a macro, a macro the name of which is stored in another macro. This summarizes my problem:
\documentclass{article}
\usepackage{xpatch}
\begin{document}
\def\tobereplaced{foo}
\def\tobepatched{\tobereplaced}
\def\patchedname{tobepatched}
\def\replacedname{tobereplaced}
\xpatchcmd{\csname\patchedname\endcsname}{\csname\replacedname\endcsname}{\csname\replacedname\endcsname!}{yes}{no}
\tobepatched
\end{document}
The patch is not effective. Interestingly, the document output is "yesno" (so the patch worked and did not, at the same time?), and "foo" (I would like "foo!"). I have tried dozens of combinations of \expandafter, but have not found a solution.
At @egreg's request, here is an extended, more applied example:
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{enumitem}
\usepackage{xpatch}
\makeatletter
\begin{document}
\begin{enumerate}
\item
\edef\label@listctr{label\@listctr}
\meaning\label@listctr
\item
\expandafter\meaning\csname\label@listctr\endcsname
\item
\csname\label@listctr\endcsname
\item
this works now:
\expandafter\xpatchcmd\csname\label@listctr\endcsname{\c@enumi}{\c@enumi'}{}{n}
\csname\label@listctr\endcsname
\item
\edef\c@@listctr{c@\@listctr}
\meaning\c@@listctr
\item
this still doesn't:
\expandafter\xpatchcmd\csname\label@listctr\endcsname{\csname\c@@listctr\endcsname}{\csname\c@@listctr\endcsname!}{y}{n}
\end{enumerate}
\end{document}
The problem is that the command name depends on the enumeration level, so what I did in the "it works now" is not valid on a second enumeration level.


\expandafter\xpatchcmd\csname\patchedname\endcsname{search}{replace}{true}{false}But you can't use macros for thesearchandreplacepart: they're not expanded at all. – egreg Nov 23 '16 at 16:42{...}around\csname. With fixedsearchandreplace, this works. How do I apply this to my MWE, withsearchandreplaceother variable macros? – bers Nov 23 '16 at 16:47\expandafter. I see no reason for doing something like that in thesearchandreplaceparts. And also for the first argument, actually. – egreg Nov 23 '16 at 16:51\expandafter" - the one that you suggested was among them, yet with an extra pair of{...}. Anyway - maybe you could enlighten me why you see no reason to do what I am doing? Although I might also be interested in a solution to the direct problem, understanding why you think it can be done differently may help, too! – bers Nov 23 '16 at 16:54