Default behaviour when missing argument in \newcommand

I'll try to be clear because this is not what I am looking for.

What I'm trying to do is to define a new command that takes one argument but has a different behaviour whether that argument is or is not specified.

Here's what I have so far :

\newcommand\Nk[1][k]{N_{#1}}


I want to be able to get :

• ${N}_k$ if I enter \Nk
• ${N}_i$ if I enter \Nk{i}

For now, with that second option I just get ${N}_ik$.

So here's my question : how can I get that behaviour I want ?

Thanks a lot.

• Use square brackets: $\Nk[i]$ in the invocation. Square brackets are used for optional arguments, which is the way you have defined \Nk. – Steven B. Segletes Feb 23 '16 at 14:08

1 Answer

The rule of \newcommand is that optional arguments are provided in square brackets [] and mandatory arguments are provided in curly braces {}.

It is a newbie mistake to forget it, but it can be frustrating to find when it happens.

\documentclass{article}
\newcommand\Nk[1][k]{N_{#1}}
\begin{document}
$N_k$ if I enter $\Nk$\par
$N_i$ if I enter $\Nk[i]$\par
but not $\Nk{i}$.
\end{document}