As some of you may know, you can use \newcommand and \DeclareMathOperator to create new TeX macros. For example, $\DeclareMathOperator{\cosine}{cosine}$$\DeclareMathOperator{\cosine}{cosine}$ allows you to write $\cosine$ and see $\cosine$. Unfortunately, using either of these commands affects the entire page they were used on, not just the question or answer they were used in. This would seem like terrible programming practice. However, there are obviously benefits to this. For example you don't need to write $\operatorname{Hom}$ ten times because you prefer a capital 'H'.

I've been avoiding using these commands myself because I think it is a better practice, but I am starting to wonder if I am being overly cautious.

Are there instances where it is ok to use these commands? Are there instances where it is crucial not to use these commands? (Other than purposeful destruction of course.)

Of course, one should not renew commands (e.g. $\renewcommand{\mathbb}{\mathbf}$)

Some behaviour tests:

$\DeclareMathOperator{\Hom}{Hom} \Hom$
$\DeclareMathOperator{\Hom}{hom} \Hom$
$\newcommand{\Hom}{\text{apple}} \Hom$
$\DeclareMathOperator{\Hom}{Hom} \Hom$

$\DeclareMathOperator{\Hom}{Hom} \Hom$

$\DeclareMathOperator{\Hom}{hom} \Hom$

$\newcommand{\Hom}{\text{apple}} \Hom$

$\DeclareMathOperator{\Hom}{Hom} \Hom$

It seems like using these commands overwrites previous definitions. If this is the case then it would seem like there is no danger of conflict.

  • 11
    $\begingroup$ As long as you don't redefine any existing macro and the name of macro you introduce is specific enough and has zero chance to collide with others, it is usually fine. If there is any chance the name you use will collide with other people's macros, you need to "bracket" you question/answer with Begingroup extension. Note - any macro you define is not limited to your question/answer. It will affect the whole page! $\endgroup$ Jun 16, 2017 at 11:30
  • 1
    $\begingroup$ What you would write 10 times is not \operatorname{\Hom}{Hom} but just \operatorname{Hom}. But still, if you're going to use this 20 times in a LaTeX document, creating a new command makes sense. $\endgroup$ Jun 17, 2017 at 19:39
  • 3
    $\begingroup$ @Michael Good catch. I had initially wrote "\operatorname{Hom} but then later I thought I had wrote \DeclareMathOperator so I "fixed" it. Also this isn't about LaTeX documents it's about writing questions on Math.SE. $\endgroup$ Jun 17, 2017 at 23:00
  • 1
    $\begingroup$ MathJax's \newcommand is arguably a bit too strong - it will happily let you do things like \newcommand{\sin}{\operatorname{\cos}}. That's obviously not OK on SE sites. $\endgroup$
    – E.P.
    Jun 19, 2017 at 21:06
  • $\begingroup$ Hm, I must admit that I sometimes used to begin my answer with \let\phi\varphi if the question uses \varphi and I feel lazy. Apparently I should better not do that in the future ... $\endgroup$ Jun 28, 2017 at 14:48

2 Answers 2


I don't think there exist general guidelines. However there are some characteristics of this site and of MathJax that together suggest, at least to me, a certain set of rules:

The relevant characteristics of this site:

  • The question is always on the top. Even if the answers are split over multiple pages, the question is on top of each page. Moreover, it is not possible to delete a question without also deleting its answers.

  • The answers may be in arbitrary order, depending on which order was selected, and even within one selection the order can change as answers are voted, accepted etc. Moreover, and answer can be deleted independent of any other answer.

  • The beginning of a question may appear on other pages (thanks to T. Gunn in the comments for pointing this out).

The relevant characteristics of MathJax:

  • Macros are only available after the definition. If the definition is further down than the use, the macro will be determined as undefined.

  • Macros can be localized using the begingroup extension of MathJax.

    Localizing is done by using $\require{begingroup}\begingroup$ before your first macro definition, and $\endgroup$ after your last.

  • Newer versions of MathJax may make more macros available.

Therefore I'd say the following rules are reasonable:

  • When properly localized with begingroup, arbitrary macros can be used. However for question, defining even localized macros in the first paragraph should be avoided (because the quoted excerpt may contain the \begingroup but not the corresponding \endgroup).

  • When writing a new question, it is in principle OK to define arbitrary new macros (of course, not redefining existing ones) without localizing them. Any such macro will be available in all answers. It may be a good idea to note somewhere (maybe in a comment to the question) which macros one has defined.

    However macro names that are likely to be defined in future MathJax versions should be avoided.

    Also consider whether your macro will help people who are answering your post. If it is unlikely to be useful for them, prefer localizing them.

  • When editing an existing question, non-local macros should no longer be changed, as this may break existing answers. This includes answers that are currently composed but not yet posted.

    One might relax those rules for macros with obvious definitions, since currently MathJax doesn't seem to enforce rules against redefinition. However there's the risk that future versions of MathJax are more strict.

  • When writing or editing answers, any non-local macro will cause inconsistent behaviour when the order of answers changes, therefore non-localized macros in answers should be unconditionally avoided.

    Note that this one holds even for obvious definitions, as users might use them when writing their answer without knowing that the macro was defined in another answer, and then their answer will break as soon as it moves above the answer defining the macro (or onto another page). Indeed, it may break for other people while working for the author, if both have different display orders selected.

Some behaviour test, as well:


$\newcommand{\Hom}{\operatorname{Hom}}\Hom$ $\newcommand{\Hom}{\mathcal{Hom}}\Hom$

OK, seems that MathJax at least currently ignores the usual LaTeX rules to prevent accidental redefinition.

  • 1
    $\begingroup$ This is a great answer. I would also like to point out that on the questions page there is some amount of the body of the question loaded. Any macros loaded at the top of a question will, I assume, affect the top of any question below them on the page. $\endgroup$ Jun 17, 2017 at 13:02
  • 1
    $\begingroup$ Also for (most) new operator names there shouldn't be a problem. For instance, \DeclareMathOperator{\Hom}{Hom} should be fine because it is hard to imagine someone using \Hom for something else. (This does not apply to operators whose command has to be renamed to avoid an already defined macro (e.g. \span).) $\endgroup$ Jun 17, 2017 at 13:02
  • $\begingroup$ @T.Gunn: Good point about the questions page. I guess if you avoid defining macros in the first paragraph, you should be safe, right? About the operator name: if someone else uses \newcommand{\Hom}{\operatorname{Hom}}, and both definitions get seen on the same scope, what will happen? $\endgroup$
    – celtschk
    Jun 17, 2017 at 13:22
  • $\begingroup$ @T.Gunn: OK, added some test myself, too, and it indeed seens to work. Of course there's always the risk of future versions of MathJax being more strict. $\endgroup$
    – celtschk
    Jun 17, 2017 at 13:38
  • $\begingroup$ @T.Gunn: I've updated the answer to deal with obvious macro definitions. $\endgroup$
    – celtschk
    Jun 17, 2017 at 13:50
  • $\begingroup$ It seems like MathJax uses a dictionary to store macros and calls to newcommand or DeclareMathOperator so using them again simply updates that dictionary. However the documentation does not address reusing these commands. $\endgroup$ Jun 17, 2017 at 13:53

While celtschk already mentioned this in their answer, I think this deserves highlighting:

There is a way to use MathJax macros safely in SE posts:

  1. Add $\require{begingroup}\begingroup$ at the beginning of your question / answer.

  2. Add $\endgroup$ at the end.

  3. Use macros as much as you want in between.

Any macros you define in your post after \begingroup will get cleared by the \endgroup. Thus, they won't have any effect on other posts.*

As a practical demonstration, the following Markdown text:

It is quite a remarkable fact that $$\frac{d}{dx} \cos x = \sin x.$$
Even more remarkable is that $$\frac{d}{dx} \cos x = -\sin x.$$

renders like this:

$\require{begingroup}\begingroup \renewcommand{\sin}{\operatorname{cos}} \renewcommand{\cos}{\operatorname{sin}}$ It is quite a remarkable fact that $$\frac{d}{dx} \cos x = \sin x.$$ $\endgroup$ Even more remarkable is that $$\frac{d}{dx} \cos x = -\sin x.$$

If you're still feeling suspicious, you can check for yourself (e.g. using the Markdown preview feature in the "post an answer" box below) that the sneaky redefinitions used in the first formula above indeed do not leak out of this answer, thanks to the \endgroup after them.

Also, I'd be remiss if I didn't mention that the Stack Overflow Unofficial Patch (SOUP) user script / browser extension, which I maintain, automatically limits the scope of MathJax macro definitions to individual posts. However, it only does that when you're viewing the page with SOUP installed; if you don't use \begingroup / \endgroup in your own posts, any macros you define in them can still leak out when other users view them. Hopefully, someday something like this restricted macro scope feature will be adopted into the SE codebase itself, making this \endgroup trick unnecessary.

*) An unfortunate exception is the snippet view on the search page, which isn't MathJax-aware and may end up including the macro definitions in the snippet but leaving out the \endgroup. SOUP also includes a fix for this issue.

  • 1
    $\begingroup$ Doing it this way should be required. Potentially modifying the content in other people's posts, without directly editing those posts, is just bad. $\endgroup$
    – Nick Alger
    Jun 21, 2017 at 2:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .