2

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 :

  • nk if I enter \Nk
  • ni if I enter \Nk{i}

For now, with that second option I just get nik.

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

Thanks a lot.

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

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}

enter image description here

| improve this answer | |

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.