# How do I create the following macros?

I would like to define the following macros.

\newcommand{\NC}{\ensuremath{\mathbf{NC}}}
\newcommand{\NC}[1]{\ensuremath{\mathbf{NC^{#1}}}}


But LaTeX complains that \NC is already defined. So, I only define \NC with optional arguments as follows.

\newcommand{\NC}[1][\phantom{0}]{\ensuremath{\mathbf{NC^{#1}}}}


And now \NC works, but \NC1 doesn't work as expected.

-
You can only have one macro \NC: either with or without argument(s). With the second one, you need to write \NC[1]. –  Qrrbrbirlbel Feb 12 '13 at 17:05
I'd define just \newcommand{\NC}[1][]{\ensuremath{\mathbf{NC^{#1}}}}, then you could use either \NC and \NC[x]. –  Claudio Fiandrino Feb 12 '13 at 17:16
Ok, \NC and \NC[x] works for me, but I still need the \phantom{0} or it eats up the following space if I write \NC circuits for example. –  vh4x0r Feb 12 '13 at 17:21
Isn't \NC\ some text sufficient? –  Claudio Fiandrino Feb 12 '13 at 17:32

There are two ways to solve your problem: If you are willing to type \NC[1] etc., then the following works

\newcommand{\NC}[1][]{{\def\NCexponent{#1}%
\ifx\NCexponent\empty{\ensuremath{\mathbf{NC}}}\else
\ensuremath{\mathbf{NC^{#1}}}\fi}}


Here, the \ifx\exponent\empty tests whether the optional argument was present and then uses \mathbf{NC^{#1}} if the argument was there and \mathbf{NC} otherwise. This makes both \NC and \NC[1] work as expected. (The extra layer of braces {{...}} is there to prevent the auxiliary macro \NCexponent from leaking outside this macro.) As for all TeX macros, spaces after a macro are swallowed by TeX, so you need to use constructions like \NC\ and\NC[1] to get a space after \NC.

If you insist to be able to type \NC as well as \NC1, this can also be done, but the solution is more complicated. Here is a complete example:

(updated to use \noexpand#2 as suggested by @egreg)

\documentclass{article}

\def\NC{\NCa{}}
\def\NCa#1#2{\def\next{\NCa{#1#2}}%
% test whether #2 is a digit:
\if0\noexpand#2\else\if1\noexpand#2\else\if2\noexpand#2\else
\if3\noexpand#2\else\if4\noexpand#2\else\if5\noexpand#2\else
\if6\noexpand#2\else\if7\noexpand#2\else\if8\noexpand#2\else
\if9\noexpand#2\else
\def\next{\NCb{#1}#2}%
\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\next}
\def\NCb#1{{\def\exponent{#1}\ifx\exponent\empty
\ensuremath{\mathbf{NC}}\else\ensuremath{\mathbf{NC^{#1}}}\fi}}

\begin{document}
There is \NC, \NC1, \NC 2\ and \NC345, as well as \NC a and again
\NC.
\end{document}


This version of the macro \NC checks all the following tokens: as long as they are digits, they are incorporated into the exponents, the first non-digit stops the argument. To achieve this, two auxiliary macros are used. \NCa constructs the exponent; it takes two arguments, the exponent constructed so far (#1) and the next token in the input stream (#2). If the next token is equal to 0, 1, ..., 9, we call \NCa again, with #2 added to the exponent. Otherwise, we call \NCb for typesetting (testing for empty exponents as above) and put back #2 into the input stream behind the expansion of \NCb (by writing \NCb{#1}#2).

One quirk of the second solution is, that all spaces encountered by \NCa are swallowed by the TeX parser. Thus, \NC 1 2 3 is the same as \NC123.

-
Using \if0#2 is quite dangerous if #2 turns out to be a macro rather than a character, because \if does complete expansion; \if0\noexpand#2 would be safer, but a syntax \NC{<exponent>} seems preferable and clearer. –  egreg Feb 19 '13 at 13:00
@egreg, I agree that \NC[1] or \NC{1} are probably the way to go. But I'm interested in how TeX macros work, so I'd like to learn how to get the second variant right, too. You are right, if my \NC is followed by a macro this will be expanded at an inconvenient time, so \noexpand should be added. Can you see anything still going wrong after this change? –  jochen Feb 19 '13 at 19:17
You can shorten the detection of a digit with the single \ifnum9<1\noexpand#2: if #2 is a digit the test is true, otherwise it's false and #2 disappears as part of the false branch. Be sure not to put a space after 1. –  egreg Feb 19 '13 at 20:20