# temporarily patch a command (xpatchcmd)

for context: I'm writing a cv with moderncv. I like the looks of it, but the package doesn't appear to allow easy layout cusomitzations.

What I want to achieve is one \section that formats a certain command differently than the others.

in particular I'm using

\xpatchcmd\cventry{,}{\newline}{}{}


to replace a comma by a \newline. Is it possible to 'scope' the patch, such that it is only applied within, say one section?

I suggest a different patch:

\xpatchcmd{\cventry}{,}{\cventrycomma}{}{}
\newcommand{\cventrycomma}{,}


Then you can do

\renewcommand{\cventrycomma}{\newline}


when you want to have a new line instead of a comma. The restoration can be obtained in two ways:

1. Enclose the part where you want the new line, including the \renewcommand, in a \begingroup...\endgroup pair

2. Issue \renewcommand{\cventrycomma}{,} when you want a comma again.

Method one can be hidden in an environment:

\newenvironment{specialsection}
{\renewcommand{\cventrycomma}{\newline}}
{}

• this looks like what i wanted - and it will use all latexs scopes that already exist automatically :) – IARI Aug 24 '15 at 14:04
• how would I use a parameter from the patched command (\cventry) inside the wrapped LaTeX command (\cventrycomma)? – IARI Aug 24 '15 at 14:37
• @IARI Sorry, I don't understand – egreg Aug 24 '15 at 14:59
• I'm sorry, I wasn't clear enough. cventry has 5 arguments. I want to be able to use these in xpatch, or even in the new proxy-newcommand - for instance i might want to replace #1 by \emph{#1} or something like that. If this isn't clear enouth I will write a separate question. – IARI Aug 25 '15 at 10:21
• @IARI That's definitely a new question – egreg Aug 25 '15 at 10:23

I would do this as follows:

\let\originalcventry\cventry%        save a copy of \cventry
\xpatchcmd\cventry{,}{\newline}{}{}% create the patched version

...use the patched version...

\let\cventry\originalcventry%       restore the original

• I'd suggest \LetLtxMacro instead of \let. In the sense that you restoration will not work. – egreg Aug 24 '15 at 13:52
• @egreg Where is \LetLtxMacro defined? I don't know it (and it sounds like I should). – Andrew Aug 24 '15 at 13:54
• While that would work I don't think its a very good solution. Assume I want to do that multiple times, with difference commands. What I really want, is an actual 'scoping' (i.e. automatically use latex environments) and not have to come up with a new variable name - it looks like boilerplate to me. – IARI Aug 24 '15 at 13:56
• @Andrew In the letltxmacro package; its documentation explains why it should be used. If you test your restoration, you'll realize it doesn't work, because \xpatchcmd does nothing to the macro \cventry, but it acts on the internal macro \\cventry. – egreg Aug 24 '15 at 14:02