Nested \newcommand's in Latex
Abstract also the formatting command
\newcommand{\newdescriptor}[3]{\newcommand{#1}{#2{#3}}}
Then
\newdescriptor{\sP}{\mathbf}{P}
will do
\newcommand{\sP}{\mathbf{P}}
Nowhere I see any improvement over the direct definition unless you do a further abstraction step:
\newcommand{\whatever}[1]{\mathbf{#1}}
\newcommand{\constant}[1]{\mathsf{#1}}
\newcommand{\newdescriptor}[3]{\newcommand{#1}{#2{#3}}}
\newdescriptor{\sP}{\whatever}{P}
\newdescriptor{\cs}{\constant}{S}
Add \ensuremath
and \xspace
if you really want. I will never be convinced that typing
The constant \cS is nice
is better than typing
The constant \(\cS\) is nice
To be honest, I find the second way the only good one (on par with $\cS$
).
In the replacement text of a definition #2
is replaced by the second argument, #1
is replaced by the first argument, and ##
is replaced by #
so
\documentclass{article}
\usepackage{xspace}
\newcommand{\makeconstructor}[2]{%
\newcommand{#1}[2]{\newcommand{##1}{\ensuremath{#2{##2}}\xspace}}}
\makeconstructor\makespace\mathcal
\makeconstructor\makewidget\mathsf
\makespace\SP{P}
\makespace\SQ{Q}
\makewidget\WP{P}
\makewidget\WQ{Q}
\begin{document}
$\SP<\SQ<\WP<\WQ$
\end{document}