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.
General ⇒ undefining (not redefining) a "\newcommand"
-
- Posts: 9
- Joined: Wed Apr 04, 2012 6:33 pm
NEW: TikZ book now 40% off at Amazon.com for a short time.

undefining (not redefining) a "\newcommand"
Code: Select all
\let\NN\undefined
\undefined
is undefined).-
- Posts: 9
- Joined: Wed Apr 04, 2012 6:33 pm
undefining (not redefining) a "\newcommand"
Thank you, sommerfee.
The following
fails, as desired, with the message
Changing the "\let" statement to
also fails, with a slightly different message:
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.
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}
Code: Select all
! Undefined control sequence.
<argument> \NN
{x}
l.8 $$ A = \frac{\sin\alpha\cos a}{\NN{x}}
$$
Code: Select all
\renewcommand{\NN}{\undefined}
Code: Select all
! Undefined control sequence.
\NN ->\undefined
l.9 $$ A = \frac{\sin\alpha\cos a}{\NN{x}}
$$
Will mark this thread as solved.
- localghost
- Site Moderator
- Posts: 9202
- Joined: Fri Feb 02, 2007 12:06 pm
undefining (not redefining) a "\newcommand"
Topics are not marked as solved by tagging them. They are marked correspondingly by accepting the answer that led to the solution.Curious George wrote:[…] Will mark this thread as solved.
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
How to make a "Minimal Example"
Board Rules
Avoidable Mistakes
¹ System: TeX Live 2025 (vanilla), TeXworks 0.6.10
Board Rules
Avoidable Mistakes
¹ System: TeX Live 2025 (vanilla), TeXworks 0.6.10