1

This question already has an answer here:

Suppose we have

\newcommand{\foo}[2][optional]{#1 #2}

defined somewhere (e.g. some package) and want to redefine it and re-use the original definition. This usually works with \let like described in https://en.wikibooks.org/wiki/TeX/let:

\let\originalfoo\foo
\renewcommand{\foo}[2][optional]{\originalfoo[#1]{modified #2}}

However, this leads to an infinite loop, as \let does not handle the optional argument.

How to handle the optional argument defined by \newcommand with \let?

marked as duplicate by egreg macros Mar 25 '18 at 12:32

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • FYI, I have a hack/workaround for it, but I would find it nicer, if copying by \let would workout somehow. – Daniel Krenn Mar 25 '18 at 11:35
  • Thank you for the link to tex.stackexchange.com/questions/88001/when-to-use-letltxmacro This other question is somehow the other way round and hard to find if you have the problem explained (the problem here is one case of an answer in the link; the answer here is the problem stated.) – Daniel Krenn Mar 26 '18 at 13:09
3

The usual \let does not work with macros having optional arguments. You need \LetLtxMacro from letltxmacro package in order to achieve this.

\documentclass{article}

\usepackage{letltxmacro}

\newcommand{\foo}[2][optional]{#1 #2}

\LetLtxMacro{\originalfoo}{\foo}

\renewcommand{\foo}[2][optional]{\originalfoo[#1]{modified #2}}


\begin{document}
\originalfoo[Hello]{World}

\foo[Hello]{World}
\end{document}

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