GeneralWhen can \newcommand not replace \def ?

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
deniss
Posts: 11
Joined: Tue Aug 04, 2009 9:06 pm

When can \newcommand not replace \def ?

Post by deniss »

I am wondering about the general equivalence of \newcommand and the TeX primitive \def. The authors of main LaTeX books advise against using \def and for using \newcommand instead. But I distinctly remember reading that in certain instances, \def must be used instead of \newcommand... Could anyone please give some examples of such instances and some general guidelines?

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

When can \newcommand not replace \def ?

Post by sommerfee »

deniss wrote:Could anyone please give some examples of such instances and some general guidelines?
\newcommand and \renewcommand are limited in parsing arguments. For example things like

Code: Select all

\def\@tempb#1=#2\@nil{#1}
or

Code: Select all

\long\def\@caption#1[#2]#3{%
  ...}
are not possible using \newcommand.

As a generic guideline I would say use \newcommand preferred to \def, if possible, so one would not accidently overwrite an already-defined command.
Post Reply