
If I use

\newcommand{\foo}[3][\empty]{#1#2#3}

and want to renew those options by


What do I have to note instead of "???", if I want to use #1 and #2 of \renewfoo and still wants to keep #1,#2,#3 of \foo?

• I think you just need to double up the #. So instead of #1 use ##1. Explanation: #1 is be the first paramater to \renewfoo so ##1 is the first paramater to \foo. – Peter Grill Mar 28 '17 at 21:53
• It's not really clear why not using \renewcommand{\foo} directly. What's the use of \empty for, by the way? – egreg Mar 29 '17 at 6:57
• Ah nice. I didn't thought this would be the right way. @egreg \empty seems to be an old fragment of my tex-life. I used it often if I wanted to check \ifthenelse{\equal{#1}{\empty}}{}{} – Shalec Mar 29 '17 at 7:27

(La)TeX allows for nested (re)definition of macros, i.e. (re)definitions inside of other macros. In order to let (La)TeX distinguish between the (possible) arguments of the outer macro and the inner arguments, the number of argument specifiers # etc. has to be doubled for the inner macro, i.e. #1, #2 becomes ##1, ##2 etc.

If there is another level of nesting, the argument specifiers # has to doubled again, i.e.

First level: #1

Second level: ##1

Third level: ####1

Fourth level ########1

etc. (Both more than three levels is weird), with other words: 2^{level-of-nesting-1} occurences of the # character must be used.

I've added a version for xparse as well to get around the 'weird' \empty optional argument parameter. However, I don't suggest the redefinition a) with another wrapper and b) the wrapper having an optional argument as well.

\documentclass{article}

\usepackage{xparse}

\newcommand{\foo}[3][\empty]{%
#1#2#3%
}


\NewDocumentCommand{\foobar}{o+m+m}{%
\IfValueT{#1}{#1}%
#2#3%
}

\NewDocumentCommand{\RenewFooBar}{o+m}{%
\IfValueTF{#1}{%
\RenewDocumentCommand{\foobar}{O{#1}+m+m}{%
And now for something completely different with xparse: #2 and ##2##3[##1]%
}%
}{%
\RenewDocumentCommand{\foobar}{+m+m}{%
And now for something completely different with xparse: #2 and ##1##2%
}%
}%
}

\begin{document}

\foo[A]{B}{C}

\renewfoo{\LaTeXe}

\foo[C]{A}{B}

\foobar{E}{F}

\foobar[D]{E}{F}

\RenewFooBar{Mr. Gumby}

\foobar{E}{F}

\RenewFooBar[Brain Surgeon]{Mr. Gumby}

\foobar{E}{F}

\end{document}