21

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?

n.r.
  • 4,942
  • 2
    I would like to upvote this question 10 times... related: http://tex.stackexchange.com/questions/93774/why-does-latex-make-declaremathsymbol-and-declaresymbolfont-preamble-only –  May 07 '14 at 06:16
  • @jfbu : Just read your the discussion from your question. Since \DeclareMathOperator doesn't necessarily involve loading new symbol, I wonder if we can find a satisfactory explanation for mere math operators! – n.r. May 07 '14 at 06:32
  • 1
    You can use \newcommand any any place, you can as well \catcode`\\=\active, but I will call you names for doing so. Yours, Journal Typesetter. – yo' May 07 '14 at 06:49
  • Can't users use \newcommand freely inside the document environment? – n.r. May 07 '14 at 07:01
  • 1
    @nicolai.rostov They can, but it's pretty unfortunate, since any processing of such document is pain-in-you-know-where. Cf. also http://tex.stackexchange.com/a/156754 – yo' May 07 '14 at 07:28
  • although the discussion I linked too dates back somewhat, I still had it somewhat on my mind, and failed to immediately appreciate on seeing your post that the situation is indeed much more user-friendly with regards to \DeclareMathOperator, as I explain in my just posted answer. –  May 07 '14 at 07:33
  • 1
    I could see reasons for allowing it in the document only for multiauthor collections, for instance the proceedings of a conference. Otherwise, changing the meaning of a command depending on the chapter is, in my opinion, quite disputable. – egreg May 07 '14 at 10:52
  • egreg and @tohecz : It may be. However, this is something a user needs to see by h(im|er)self, and not just refrain from doing because it's "frowned upon". Otherwise, it is like obeying the law only so as not to be arrested. No virtue in that. – n.r. May 07 '14 at 17:34

2 Answers2

10

I guess there are two components to an answer to your "why" question.

Most immediately, there's an \@onlypreamble qualifier for many setup-type macros of the amsmath package to make sure they won't work if invoked outside the preamble. The macro \DeclareMathOperator is defined in the file amsopn.sty, which is loaded by amsmath.sty; note the instruction \@onlypreamble\DeclareMathOperator at ca line 57 of amsopn.sty.

You might then ask, so why is the \@onlypreamble qualifier imposed? I wasn't involved in the design or creation of the amsmath package, but I'd say a reasonable guess is that the creators were adhering to the spirit of the LaTeX2e format, which strongly encourages separating matters of content from matters of visual display of the content. The latter matters are supposed to be taken care of in the preamble.

To address your related concern, i.e., having various chapters that may require different definitions of one and the same operator-type macro. For concreteness, let's say the operator is called \det. The creators of the amsmath package either didn't envisage a use case such as the one you've set forth here or, quite possibly, they may have believed that it's better to define all possible variants of \det -- say, \detA and \detB -- in the preamble and then execute a \let of \det to either \detA or \detB at the start of each chapter.

Mico
  • 506,678
  • 11
    it wasn't so much purity as necessity: if you used amsmath on emtex on a 512KB PC in 1993 you had hardly any bytes left for any user commands or cross references, \@onlypreamble any commands that we could get rid of, freed up enough memory that you could (just about) typeset a hello world document without running out of memory. – David Carlisle May 07 '14 at 08:22
  • @DavidCarlisle - Many thanks for clarifying what actually went on in the runup to the introduction of LaTeX2e and the amsmath package. :-) – Mico May 07 '14 at 08:31
4

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

  • 1
    \operatornamewithlimits is deprecated; \operatorname* does the same. – egreg May 07 '14 at 08:49
  • @egreg: I had looked at amsopn.sty, not amsopn.dtx which mentions indeed: For backwards compatibility we keep this old command name for the time being. –  May 07 '14 at 10:00