Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question
    
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. –  vh4x0r Feb 12 '13 at 17:21
    
Isn't \NC\ some text sufficient? –  Claudio Fiandrino Feb 12 '13 at 17:32

1 Answer 1

up vote 6 down vote accepted

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.

share|improve this answer
    
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

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.