\let on macro defined by \newcommand with optional argument [duplicate]

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 StackExchange.ready(function() { if (StackExchange.options.isMobile) return; $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() { var$hover = $(this).addClass('hover-bound'),$msg = $hover.siblings('.dupe-hammer-message');$hover.hover( function() { $hover.showInfoMessage('', { messageElement:$msg.clone().show(), transient: false, position: { my: 'bottom left', at: 'top center', offsetTop: -7 }, dismissable: false, relativeToBody: true }); }, function() { StackExchange.helpers.removeMessages(); } ); }); }); Mar 25 '18 at 12:32

• 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

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}