7

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
4
\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}%
  • 1
    @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

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.