[Tex/LaTex] Why must \DeclareMathOperator be in the preamble

amsmath

What is the rationale for requiring \DeclareMathOperator to be in the preamble?

Why are users not allowed to restrict the scope of an operator definition to a particular chapter or environment, like \newcommand?

Best Answer

In the case of \DeclareMathOperator, contrarily to the LaTeX issues raised in this question: Why does LaTeX make \DeclareMathSymbol and \DeclareSymbolFont preamble-only?, the user is provided with a simple possibility to circumvent the only-preamble restriction: it is to use the \operatorname or \operatornamewithlimits command, also provided by amsmath (or more precisely amsopn where \DeclareMathOperator is defined and made preamble only).

declare math operator in environment

\documentclass{article}
\usepackage{amsmath}
\begin{document}
\texttt{\textbackslash COS has meaning: \meaning\COS}

\texttt{\textbackslash cos has meaning: \meaning\cos}

\begin{large}
  \newcommand{\COS}{\operatorname{COS}}
  In this environment the math operator \verb|\COS| is defined: $\COS(x)=\cos(x)$.

\texttt{\textbackslash COS has meaning: \meaning\COS}
\end{large}

\texttt{\textbackslash COS now has meaning: \meaning\COS}

\end{document}

As an aside, there is an issue with \DeclareMathOperator and Unicode engines (if the minus sign is made a Unicode math character), which is fixed under lualatex by loading package lualatex-math