How to define a command

\def\hello{Hello world!}

that can be called with no parameter:


or with a parameter:


but that won't be displayed anyway (3 shouldn't be displayed, it's an internal number I'll maybe use later, but for now, it should not be displayed)

  • It is called an optional argument, used with brackets: \newcommand\hello[1][]{Hello world!\gdef\savearg{#1}}, Then \hello or \hello[3] will work, and \savearg will preserve the optional argument. – Steven B. Segletes Oct 19 '18 at 14:16
  • see the xparse package it has some inrecommended options like the g type. – daleif Oct 19 '18 at 14:16
  • Thanks @StevenB.Segletes. Even \newcommand\hello[1][]{Hello World} works! – Basj Oct 19 '18 at 14:19
  • 1
    Yes it does, and it throws away the optional argument until you later decide to revise the command definition. – Steven B. Segletes Oct 19 '18 at 14:21
  • It's preferable to use a conforming syntax and the usual method for denoting optional arguments in LaTeX is with []. So \newcommand\hello[1][]{Hello world} is the best choice. With xparse there is a finer control for optional arguments. – egreg Oct 19 '18 at 14:22

As pointed out by Steven B. Segletes in a comment,

\newcommand\hello[1][]{Hello world!\gdef\savearg{#1}}

works and saves the optional argument. If the optional argument is not required, then

\newcommand\hello[1][]{Hello World}

is the solution.

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.