Generalundefining (not redefining) a "\newcommand"

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
Curious George
Posts: 9
Joined: Wed Apr 04, 2012 6:33 pm

undefining (not redefining) a "\newcommand"

Post by Curious George »

When writing messy equations, I frequently use \newcommand to reduce typing, reduce errors, and improve consistency. Here is an example from a current paper:

\newcommand{\NN}[1]{\sqrt{\sin^2 {#1}-CC}}

I would like to "undefine" the macro at the end of the section where it is used. Then, if it is accidentally typed in a later section where it doesn't belong, LaTeX would halt and give an error.

Of course, it would be possible to use something like

\renewcommand{\NN}{\mbox{ERROR}}

but, besides being somewhat inelegant, this would require being able to spot the error message in the finished paper. My preference would be the LaTex processor itself to detect the error.

So, my question: is there a way to undefine a \newcommand, so that any future attempt to use it results in an "Undefined control sequence" error?

Thanks for any assistance.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

sommerfee
Posts: 503
Joined: Mon Apr 09, 2007 4:20 pm

undefining (not redefining) a "\newcommand"

Post by sommerfee »

Code: Select all

\let\NN\undefined
should do the trick (at least as long as \undefined is undefined).
Curious George
Posts: 9
Joined: Wed Apr 04, 2012 6:33 pm

undefining (not redefining) a "\newcommand"

Post by Curious George »

Thank you, sommerfee.

The following

Code: Select all

\documentclass{article}
\begin{document}
\newcommand{\NN}[1]{\sqrt{\sin^2 {#1}-CC}}
$$ A = \frac{\sin\alpha\cos a}{\NN{x}} $$
\let\NN\undefined
$$ A = \frac{\sin\alpha\cos a}{\NN{x}} $$
\end{document}
fails, as desired, with the message

Code: Select all

! Undefined control sequence.
<argument> \NN
               {x}
l.8 $$ A = \frac{\sin\alpha\cos a}{\NN{x}}
                                           $$
Changing the "\let" statement to

Code: Select all

\renewcommand{\NN}{\undefined}
also fails, with a slightly different message:

Code: Select all

! Undefined control sequence.
\NN ->\undefined
                 
l.9 $$ A = \frac{\sin\alpha\cos a}{\NN{x}}
                                           $$
Have a slight preference for the first form, using"\let". On the other hand, "\renewcommand" would help insure that it was really "\NN" that was undefined.

Will mark this thread as solved.
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

undefining (not redefining) a "\newcommand"

Post by localghost »

Curious George wrote:[…] Will mark this thread as solved.
Topics are not marked as solved by tagging them. They are marked correspondingly by accepting the answer that led to the solution.

A remark not related to the problem. Do not use the TeX syntax $$ … $$ for unnumbered displayed equations. It causes unwanted side effects (alignment, spacing). Use the LaTeX syntax \[ … \] instead. Perhaps you should read about "deadly sins" in l2tabu.


Thorsten
Post Reply