I want to define a command \NewSmartOp
such that:
\NewSmartOp \MYOP \myop
would produce the following code (or anything equivalent):
\makeatletter
\def\MYOP{\@ifstar\MYOP@star\MYOP@nostar}
\def\MYOP@star#1{\myop\!\left( #1 \right)}
\def\MYOP@nostar#1{\myop #1}
\makeatother
I guess I need \expandafter
and perhaps \csname … \endcsname
but otherwise I have no clue how to generate these definitions, especially regarding how to form a new command name \MYOP@star
from \MYOP
, and also how to deal with these #1
that must be kept as-is in the produced definitions (by contrast with being expanded by the definition producing them).
Background (reading optional)
For a bit of context: when typing math I want to abstract from concrete syntax as much as possible. For instance, for cardinality of set S, perhaps I’d like it to be rendered as card S (with parentheses only when needed, e.g. card(S₁ ∪ S₂)), but perhaps at some point in the future I’d like to switch syntax to |S|. So I would define:
\DeclareMathOperator \card {card}
\NewSmartOp \CARD \card % option 1
%\newcommand* \CARD[1] {\left\lvert # \right\rvert} % option 2
after which
- I might use the operator directly (low-level, my TeX typing reflects the concrete syntax):
\card S
or\card(S)
; - but I’d rather use a command with an argument to which I defer the concrete syntax (high-level, my TeX typing reflects the abstract syntax tree):
\CARD {S}
.
This ideal seems hard to achieve fully because I cannot(?) find out automatically when parentheses are needed, so the compromise I adopted is to have stared versions of these “smart operators” add parentheses: \CARD* {S_1 \cup S_2}
.