7

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.

7

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}

enter image description here

| improve this answer | |
5

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}

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.