# \DeclareMathOperator internals

I ask two questions below, which are unified in a third meta-question. Thanks to @DavidCarlisle's comment, I realise that my questions are such that the answer may depend on whether they are interpreted in the context of MathJax or of LaTeX+amsmath. I am interested in answers in both contexts.

I am constantly on the hunt to impose (my admittedly self defined) best practices on the sometimes baroque TeX that crops up at MO. A lot of times this involves changing, say, $Hom(A, B)$ to $\operatorname{Hom}(A, B)$, and I often leave a note for a new user letting them know about \DeclareMathOperator and siblings.

I noticed on one post that a user had picked up this advice with a twist, and started using \DeclareMathOperator\Hom{\operatorname{Hom}}.

Q: Is \DeclareMathOperator\Hom{\operatorname{Hom}} synonymous with \DeclareMathOperator\Hom{Hom}?

I have recently noticed another user who used a construction like \DeclareMathOperator\Set{\mathit{Set}}.


Obviously there's a meta-question lurking here, which I would be able to answer if I were more comfortable diving into—I guess it is—the amsmath source:

Q: On the level of code, not just the big picture "this is what you want for an operator name", what do \DeclareMathOperator and \operatorname do?

• Note that if you are asking about MathOverflow, the details of the tex internals of the latex definitions of these commands are not relevant as that is using Javascript not TeX, can you clarify whether "on the level of code" you are asking about tex or mathjax? Nov 24, 2020 at 0:06
• the amsmath source is quite readable and is a typeset document, try texdoc amsmath.pdf Nov 24, 2020 at 0:08
• mathjax definitions are usually unrelated to tex definitions of commands of the same name, it is in no sense a translation. the main difference is not just that it is written in javascript, but it is written in terms of (essentially) mathml javascript objects and the css layout model, whereas latex definitions are written involving catcodes and tex's box and glue layout model, so apart from the top level names and documented behaviour, there is usually nothing in common, and behaviour of undocumented edge cases as in some of the examples you show is quite likely different. Nov 24, 2020 at 7:52
• @LSpice - If you hanker to understand the deep details of \operatorname, do check out this posting. :-)
– Mico
Nov 24, 2020 at 8:25
• @LSpice - On your first comment: I believe the main practical difference is that \DeclareMathOperator{\xyz}{xyz} defines a "robust" macro called \xyz, whereas \newcommand\xyz{\operatorname{xyz}} creates a potentially non-robust macro. On the second comment: I hadn't thought of "idempotency" in the context of (recursive) macro definition, but I think it's a remarkably apt term. (Well, idempotency might fail if the tokens contain some devious low-level font-size changing commands.)
– Mico
Nov 25, 2020 at 6:56

I'll not touch MathJax, because its model is completely different from LaTeX.

### First question

Something like \DeclareMathOperator{\Hom}{\operatorname{Hom}} of course works, but \Hom will do twice the same things. In amsopn.sty, we find

\DeclareRobustCommand{\operatorname}{%
\@ifstar{\qopname\newmcodes@ m}%
{\qopname\newmcodes@ o}}%
\DeclareRobustCommand{\qopname}[3]{%
\mathop{#1\kern\z@\operator@font#3}%
\csname n#2limits@\endcsname}
\newcommand{\DeclareMathOperator}{%
\@ifstar{\@declmathop m}{\@declmathop o}}
\long\def\@declmathop#1#2#3{%
\@ifdefinable{#2}{%
\DeclareRobustCommand{#2}{\qopname\newmcodes@#1{#3}}}}


Assuming that \foo is undefined, \DeclareMathOperator{\foo}{???} does

\DeclareRobustCommand{\foo}{\qopname\newmcodes@ o{???}}


which is a shortened version of

\DeclareRobustCommand{\foo}{\operatorname{???}}


On the other hand, \operatorname{+++} will do

\qopname\newmcodes@ o{+++}


Hence your \Hom would do

\qopname\newmcodes@ o{\qopname\newmcodes@ o{Hom}}


which seems quite complicated for nothing: it's essentially the same as doing

\operatorname{\operatorname{Hom}}


each time \Hom is used.

### Second question

The definitions

\DeclareMathOperator\Set{\mathit{Set}}
\newcommand\Set{\mathit{Set}}
\newcommand\Set{\mathop{\mathit{Set}}}


will do different things. We can see the differences with a simple example

\documentclass[twocolumn]{article}
\usepackage{amsmath}

\begin{document}
\begin{gather}
x \operatorname{\mathit{Set}}_2 y\\
x \mathit{Set}_2 y \\
x \mathop{\mathit{Set}}_2 y
\end{gather}
\end{document}


The answer to your first question shows that \foo after \DeclareMathOperator{\foo}{foo} is the same as using \operatorname{foo}, so line (1) is the same as you'd get from \DeclareMathOperator{\Set}{\mathit{Set}}.

Are they the same? No.

### Third question

Use \operatorname in the document for operators that appear one or two times. For operators that are used several times, add the appropriate \DeclareMathOperator line, in order to ease typing and reduce clutter.

I leave to you as an exercise to find the differences between

\DeclareMathOperator*{\tlim}{t-lim}


and

\newcommand{\tlim}{\mathop{t-lim}}

• Note that (as you of course know) one can get \mathop spacing without the log-like subscript in (3) with \mathop{\mathit{Set}}\nolimits. Nov 24, 2020 at 9:35
• @Davislor Yes, I know, but I answered the OP's question. ;-) On the other hand \mathop{a-b} and \operatorname{a-b} will output very different things. Nov 24, 2020 at 9:46
• Ah, good old hyphen-minus, doing double duty. Nov 24, 2020 at 9:50
• You definitely answered my second question, but I'm not entirely sure you answered the first or the third. The first question was: Is \DeclareMathOperator\Hom{\operatorname{Hom}} synonymous with \DeclareMathOpator\Hom{Hom}? If I understand correctly, you instead answered: Is \DeclareMathOperator\Hom{\operatorname{Hom}} synonymous with \newcommand\Hom{\operatorname{\operatorname{Hom}}? For the third, you seem to have explained when to use \DeclareMathOperator and when to use \operatorname, which is general macro-style advice; but I was interested in getting into the weeds … Nov 24, 2020 at 14:38
• @LSpice Yes, they're almost the same; they'll be the same with \DeclareRobustCommand instead of \newcommand (but with \DeclareMathOperator you get a slightly more efficient macro). Nov 24, 2020 at 15:58