change or patch a command using parameters

Assume I want to use a command \foo from some package takes a number of arguments, let's say #1 to #5. I want to change the behavior of \foo in ways that might involve these arguments.

For example assume \foo formats the 5 arguments in a certain fashion. I might want to:

1. remove the first occurrence of a comma produced by \foo
2. output argument #5 in front of everything
3. swap arguments #1 and #2
4. enclose the usage of argument #3 with \emph{}

So far I've been able to accomplish 1. with xpatch like this:

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


but clearly, i cannot literally use arguments here like this

\xpatchcmd\cventry{#3}{\emph{#3}}{}{}


How can I achieve points 2. to 4. in a nice, readable generic reusable fashion?

Note that solutions for the other points should allow something like the following approach:

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


where \cventrycomma is used to allow scope control, so in a certain \begingroup\endgroup environment \cventrycomma can be redefined to behave differently (if this isn't clear see also temporarily patch a command (xpatchcmd)).

• It's possible (probably) but there comes a point where it's just so much easier to simply copy the original definition and edit it to your requirements. I suspect that this is well past that point. – David Carlisle Aug 25 '15 at 11:02
• In my opinion, solutions must be very specific to the command. I think it may happen it amounts to entirely rewriting the command. If you say what is this command, it would be easier o have a useful answer. – Bernard Aug 25 '15 at 11:03
• This might be a practical solution that just works, but I strongly believe that copy-pasting is always the wrongest thing to do and I would like to avoid it whenever it is possible. – IARI Aug 25 '15 at 11:06
• The link to the other question is more specific. However, I'm looking for a generic solution. I believe my problem is stated in a way that it can be applied to any command, and i want a solution that can be applied to any command, if thats possible. – IARI Aug 25 '15 at 11:08
• Remember you are working in a macro expansion language not a compiled language, there is no hiding of implementation in anycase the patch command is simply copying the implementation and has complete dependency on the tokens in that implementation inorder to make source-level changes, copying the code is often simpler and safer in that you are more resilient to changes if the original is updated. – David Carlisle Aug 25 '15 at 11:16