3

If I use

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

and want to renew those options by

\newcommand{\renewfoo}[2][\empty]{\renewcommand{\foo}[3][\empty]{???}}

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?

  • 1
    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
  • \newcommand{\renewfoo}[2][\empty]{\renewcommand{\foo}[3][#1]{This is from #2 and the internal ##1, ##2,##3}} perhaps? But the use - case is not really clear – user31729 Mar 28 '17 at 22:01
  • \newcommand{\renewfoo}[2][\empty]{\renewcommand{\foo}[3][\empty]{##1, ##2 and ##3 from foo and #1 and #2 from renewfoo}} – cfr Mar 28 '17 at 23:21
  • 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
2

(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%
}

\newcommand{\renewfoo}[2][]{\renewcommand{\foo}[3][#1]{And now for something completely different: #2 and ##2##3[##1]}}

\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}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.