I would like to define the following macros.


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


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
  • If \newcommand{\NC} didn't work (due to an already-defined \NC), then any other \newcommand{\NC} would also not work. You could redefine it using \renewcommand, or plain ol' \def. Perhaps provide more context so we can help you better. – Werner Feb 12 '13 at 17:13
  • 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. – vikraman 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


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)


  % test whether #2 is a digit:

There is \NC, \NC1, \NC 2\ and \NC345, as well as \NC a and again

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.

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

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.