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.

1
  • 2
    Use square brackets: $\Nk[i]$ in the invocation. Square brackets are used for optional arguments, which is the way you have defined \Nk. Commented Feb 23, 2016 at 14:08

1 Answer 1

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .