7

I wonder what does the \nop mean, and how does it work? I Googled it but couldn't find a good answer, for example, I found something like:

\newcommand{\nop}[1]{}

in my .tex file, so what does it mean?

9

In your example, \nop is defined to take a single argument and do nothing with it. As such, \nop literally translates to "perform no operation", or "gobble your argument". In fact, there exists a similar core macro \@gobble (and friends) which does exactly the same (from latex.ltx:

\long\def \@gobble #1{}
\long\def \@gobbletwo #1#2{}
\long\def \@gobblefour #1#2#3#4{}

The first gobbles a single argument, making \@gobble{<anything>} expand to nothing. The second gobbles two arguments, making \@gobbletwo{<anything>}{<anything>} expand to nothing, and so on.

What's the use case here? Well, perhaps you define a macro that writes something in the margin, like a "to do" note:

\newcommand{\mymacro}[1]{\marginpar{Do #1}}

Now, later in your production, you decide that this operation is no longer valid/useful. So, you can make \mymacro act like \nop and do nothing, which allows you to leave your code as-is:

\let\mymacro\nop

or, at definition in your preamble, change it to a no-op:

\newcommand{\mymacro}[1]{}
6
  • another thing that confused me is that any paragraph that is enclosed in this \nop{} command using the macro in the example, is disappeared, so i wonder why.
    – daiyue
    Apr 16 '13 at 14:46
  • 1
    @daiyue -- making anything presented as an argument to \nop disappear is exactly its purpose. Apr 16 '13 at 14:52
  • Something like \nop is also frequently used in conjunction with BibTeX to fix the sorting order.
    – mafp
    Apr 16 '13 at 15:49
  • 1
    @ctrl-alt-delor: you can add \ignorespaces or \unskip or use a trailing %.
    – Werner
    Jul 8 '18 at 18:41
  • 2
    Thanks: \newcommand{\nop}[1]{\ignorespaces} seems to work. (comment added so others and I know where to add command). Jul 9 '18 at 6:57

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.