2

LaTeX has so many similar yet different ways of creating new macros/commands/functions and it is quite confusing. How do \DeclareMathOperator and \operatorname differ from other common definition commands?

2 Answers2

4

\operatornameis not a definition at all, \operatorname{log} is essentially \mathop{\mathrm{log}} (with some details omitted)

\DeclareMathOperator\mylog{log} is essentially \def\mylog{\operatorname{log}}

Providing a LaTeX name provides a more consistent syntax, ensures the details are not omitted (eg \operatorname{S} sits on the baseline, but \mathop{\mathrm{S}} would be like \sum and vertically centred) It can also make use of LaTeX syntax for related definitions such as star form \DeclareMathOperator* (to control \limits in this case)

David Carlisle
  • 757,742
1

There are many other methods for assigning a meaning to a control sequence, but \operatorname isn't among them.

  • \def \gdef \edef \xdef and \let (but use them with care)
  • \newcommand \renewcommand \providecommand
  • \DeclareRobustCommand
  • \NewCommandCopy \RenewCommandCopy
  • \newlength \newsavebox (and other \new...)
  • \DeclareMathSymbol and friends (look into fontmath.ltx)
  • \DeclareTextCommand and friends (look into fonttext.ltx)

Also

  • \newenvironment \newtheorem
  • \newcounter

that indirectly define control sequences. All of these are either primitives or are defined in the LaTeX kernel.

Packages may introduce new ways to define control sequences. One case is amsmath (actually amsopn) that provides \DeclareMathOperator.

Why this wealth? Because one wants to avoid unnecessary code duplication. If you look into t1enc.def you will find several instances of \DeclareTextAccent, \DeclareTextCommand and \DeclareTextCompositeCommand. Of course they could be substituted with the lower level code at each call, but…

% latex.ltx, line 7345:
\def\DeclareTextCompositeCommand#1#2#3#4{%
  \expandafter\let\expandafter\reserved@a\csname#2\string#1\endcsname
  \ifx\reserved@a\relax
   \DeclareTextCommand#1{#2}{%
     \@latex@error{\string#1 undeclared in encoding #2}\@eha}%
   \@latex@info{Composite with undeclared \string#1 in encoding #2}%
   \expandafter\let\expandafter\reserved@a\csname#2\string#1\endcsname
  \fi
  \expandafter\expandafter\expandafter\ifx
  \expandafter\@car\reserved@a\relax\relax\@nil \@text@composite \else
      \edef\reserved@b##1{%
         \def\expandafter\noexpand
            \csname#2\string#1\endcsname####1{%
            \noexpand\@text@composite
               \expandafter\noexpand\csname#2\string#1\endcsname
               ####1\noexpand\@empty\noexpand\@text@composite
               {##1}}}%
      \expandafter\reserved@b\expandafter{\reserved@a{##1}}%
   \fi
   \expandafter\def\csname\expandafter\string\csname
      #2\endcsname\string#1-\string#3\@empty\endcsname{#4}%
  }

There are twelve instances of \DeclareTextCompositeCommand just in t1enc.def and so many others in the .def files for output encodings. Would you replace each of them with that preposterous piece of code? And I could have mentioned \DeclareMathSymbol that has an even longer definition and is used hundreds of times for assigning a meaning to the math symbol commands.

Of course not! And \DeclareMathOperator is in the same vein: in order to correctly define a command to stand for an operator there are several things to do and so amsmath provides an abstraction that frees the user from knowing the inner details that are irrelevant to them. You don't study how a microwave oven works in order to get one: you go to the shop and buy it.

Similarly, \pgfmathsetmacro is an abstraction that allows the user to employ the command to define a control sequence that will have the documented properties. The details of the implementation are known to the maintainers of PGF, just like the design of a microwave oven is known to the manufacturer.

egreg
  • 1,121,712