# How do I nest a command with an optional argument?

I'm having problems making a normalizer command that can nest. Here is my code so far:

\documentclass{article}
\newcommand{\nrml}[2][]{N_{#1}(#2)}

\begin{document}
$\nrml[\nrml{H}]{J}$
\end{document}


which works fine, but when I try $\nrml[\nrml[G]{H}]{J}$ I get a bunch of error codes, the first one being Argument of \nrml has an extra }.'' How can I fix this?

Edit: originally posted incorrect code, fixed.

Edit 2: It looks like $\nrml[{\nrml[G]{H}}]{J}$ does what I'm trying to do, but it seems pretty ridiculous that I would need to surround an argument that's already indicated by square brackets by braces, and that is something I would like to avoid.

If you define your command with xparse, the problem is taken care of automatically; note that you can also easily insert the subscript only if actually specified.

\documentclass{article}
\usepackage{xparse}

\NewDocumentCommand{\nrml}{om}{%
N\IfValueT{#1}{_{#1}}(#2)%
}

\begin{document}

$\nrml[\nrml{H}]{J}$

$\nrml[\nrml[K]{H}]{J}$

\end{document}


Enclose the outer optional argument in braces. The issue is in the way that optional arguments are digested by LaTeX (see Bug? LaTeX misparses nested optional arguments).

\documentclass{article}
\newcommand{\nrml}[2][]{N_{#1}(#2)}

\begin{document}
$\nrml[\nrml{H}]{J}$

$\nrml[{\nrml[G]{H}}]{J}$
\end{document}