# Gobble optional arguments

You can use \@gobble, for instance, to restrict the usage of a macro like:

\documentclass{article}
\makeatletter
\newcommand*{\cmd}[1]{%
\textbf{#1}
\let\cmd\@gobble
}
\makeatother

\begin{document}
\cmd{Hello World!}
\cmd{Again...}%not desirable
\end{document}


But, if you have a command with an optional argument, e.g. \newcommand{\cmd}[2][]{#1 #2}, there is no kernel macro that helps you. Sure, you could define something like

\newcommand{\@gobble@opt}[2][]{}
%\newcommand{\RG@gobble@opt}[2][]{}%alt: to avoid possible name clashes


Is this the usual way? Is it, generally speaking, absurd to gobble like this?

• \let\cmd\@gobble is not really sufficient: you should add something more with \@bsphack and \@esphack. – egreg Nov 22 '13 at 15:08

## 1 Answer

\usepackage{letltxmacro}
\makeatletter
\newcommand*{\gobblemyopt}[2][]{\@bsphack\@esphack}
\makeatother
\newcommand*{\cmd}[2][]{%
#1 #2%
\GlobalLetLtxMacro\cmd\gobblemyopt
}


I have another solution, simpler and without usepackage:

\let\cmdnotyetrun=\iftrue
\newcommand*{\cmd}[2][]{\cmdnotyetrun%
#1#2%
\global\let\cmdnotyetrun=\iffalse\fi}%

• @cgnieder Not really important here since neither \\cmd will be needed nor \\gobblemyopt will be changed again. But it won’t hurt either. – Qrrbrbirlbel Nov 22 '13 at 21:37
• There is \GlobalLetLtxMacro. \global\LetLtxMacro won’t work. It might be helpful, if you also add that \usepackage{letltxmacro} is needed. – Qrrbrbirlbel Nov 22 '13 at 21:39