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 am writing a text which references a whole bunch of Brazilian norms. All of these norms are prefixed with "NBR". I therefore created the following command

\newcommand*{\NBR}[1]{NBR #1\cite{NBR#1}}

This way, I can, anywhere in the text, simply type \NBR{6118} and it comes out in a standard (instead of accidentally alternating between NBR6118, NBR 6118, NBR-6118, etc). This also puts a citation next to the name, which I tend to find useful, since the text is usually of the form "According to Table 8.1 of NBR 6118...". This way, this saves me from referencing things by hand.

There are instances, however, where I'd rather not have this citation. For example, I may have "Though the equations may differ, international norms and NBR 6118 share similar concepts...", where the citation adds nothing.

I therefore thought of creating an asterisked version of command (\NBR*) which places the name but not the citation. After trying the naive path

\newcommand*{\NBR*}[1]{NBR #1}

and getting an error, I found that one must use different methods. The xparse package seemed promising, so I defined the following:

\NewDocumentCommand \NBR {s m}
{
    \IfBooleanTF{#1}
        {NBR #2}
        {NBR #2\cite{NBR#2}}
}

which seems to do the trick. \NBR{6118} gives me the text with the citation, while \NBR* omits the cite. However, I noticed the end-result is different, with what seems to be additional spaces before and after the command in the \NewDocumentCommand version. The citation's automatic move after punctuation is also inhibited in the \NewDocumentCommand.

This is easily seen below:

With \newcommand

\newcommand

With \NewDocumentCommand

\NewDocumentCommand

Is there a way of correcting this?

share|improve this question

1 Answer 1

up vote 8 down vote accepted

You can't define the *-variant with \newcommand{\NBR*}, that's all.

The working version with \NewDocumentCommand has spurious spaces: you need to protect some end-of-lines.

\NewDocumentCommand \NBR {s m}
 {%
  \IfBooleanTF{#1}
   {NBR #2}
   {NBR #2\cite{NBR#2}}%
 }

No % is needed at the end of lines 3 and 4, because \IfBooleanTF has three arguments and TeX ignores spaces when looking for arguments.

You can abbreviate the definition by noting that NBR #2 is common to both cases:

\NewDocumentCommand \NBR {s m}
 {%
  NBR #2\IfBooleanF{#1}{\cite{NBR#2}}%
 }

You can define \NBR using \newcommand, but it requires a low level procedure:

\makeatletter
\DeclareRobustCommand{\NBR}{\@ifstar\NBR@s\NBR@}
\newcommand{\NBR@}[1]{NBR #1\cite{NBR#1}}
\newcommand{\NBR@s}[1]{NBR #1}
\makeatother
share|improve this answer
    
I'd already figured out I couldn't use \newcommand{\NBR*} from the link shown, but I just couldn't figure out how to get the formatting right. But those comments (%) solved it. Many thanks! –  Wasabi Aug 11 at 20:14

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.