# Command with or without parameter

How to define a command

\def\hello{Hello world!}


that can be called with no parameter:

\hello


or with a parameter:

\hello{3}


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
• 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

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

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