General ⇒ undefining (not redefining) a "\newcommand"
-
- Posts: 9
- Joined: Wed Apr 04, 2012 6:33 pm
undefining (not redefining) a "\newcommand"
\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.
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
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"
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
Board Rules
Avoidable Mistakes
¹ System: TeX Live 2025 (vanilla), TeXworks 0.6.10