# MathJax \newcommand in question titles is incorrectly scoped to the entire page, unlike in posts and comments

There are many sites in the network where MathJax is enabled. In MathJax we can also define macros - which can be used basically as shortcuts.

However, some problems might be caused by such macros, since macros with the same name defined in different answers might clash.1 For this reason, since January 2019 each post and each comment is automatically enclosed in begingroup..endgroup by the Stack Exchange software - this prevents macros defined in the post/comment from "leaking" elsewhere.2 The intention of this change was to prevent the problems which might be caused by clashing macros or even redefining the standard macros.

However, the titles are not enclosed in begingroup..endgroup. Thus if somebody defines a macro in the title, it can lead to the same problems. (So this seems inconsistent with the change mentioned above - it prevented "leaking" of macros from posts and comments, but it is still allowed for the titles.)

Proposal: Include also the titles into begingroup..endgroup. Make sure that this happens in all places where the titles are displayed. (For example, similar questions displayed when composing a post, list of related/linked questions, etc.3)

Experiments. You can see in this question that a macro defined in a title can be also used in comments and answers: This is the test of a question where a macro is defined in the title. I have also posted another question to see whether macros can "leak" from linked questions: Test - please ignore (related to macros in the titles). It seems that they are rendered only sometimes - usually in the preview,4 typically they stop being rendered after refreshing the page. (I will eventually deleted both linked posts on Mathematics - after that they will still be visible to 10k+-users and diamond users. But at the moment, feel free to use them for further experimentation, if needed.)

I have previously posted about this also on Mathematics Meta: Should titles be included in begingroup..endgroup (similarly as posts and comments)? Since some time passed, I have tried also here - this issue influences also some other sites. (In that discussion I have asked also about possibility of blacklisting definition of macros in the titles completely - which gained some support, but I cannot claim that the number of votes is sufficient to consider it a consensus.)

1For a more detailed explanation, see the post on this issue on Mathematics Meta: The scope of \newcommand is the entire page.

2 This was announced here: The scope of \newcommand is the entire page. It is also mentioned in Recent feature changes to Stack Exchange:

2019-01-14: On sites with MathJax enabled, the effect of \newcommand and similar directives is now scoped to individual posts and comments rather than the entire page. (Bug report on Math.SE's meta)

3 As far as I can tell, it should not be a problem for HNQs - since questions containing MathJax are currently excluded: Are the questions with TeX markup in titles excluded from Hot Network Questions?

4The fact that linked questions can influence behavior of macros in the preview is consistent with some previous posts that noticed this: A possible bug regarding MathJax, previews, and the arctangent/tangent functions? and MathJax rendering in preview but not in actual post?

Maybe it is worth mentioning that this change also broke some older posts and comments which relied on the fact that they can use macros defined anywhere on the page, but that is probably a topic for a separate discussion. This was raised for example on Physics Stack Exchange, where the problematic posts were edited manually: The scope of \newcommand changed. As I have mentioned in another answer, some examples of broken posts can be seen on Cross Validated and also on MathOverflow. (Some examples from MO are in this post and also in these two chat conversations: MO1, MO2.) There are probably more instances on Mathemaics, which is a much bigger site. Some of the posts have been edited manually. You can also find some examples of such comments - of course, in the Stack Exchange philosophy, comments are less important. I have previously mentioned this as a side note in another discussion on this meta.

• I believe definitions should be disallowed in titles. Sep 13, 2020 at 9:58
• @EmilJeřábek That would solve the problem too. (In such case, the existing posts with macros in the title would need manual editing.) Sep 13, 2020 at 10:02
• Based on this comment, I believe SE is aware of this problem and has decided not to act. Moderators are responsible for dealing with this problem (at least for the time being). Sep 13, 2020 at 20:41
• @Kevin As far as I can tell, that specific comment refers to putting \endgroup..\begingroup into a post (or a comment). It does not mention titles. Sep 13, 2020 at 20:51
• @Martin: Oh, you're right! I just subconsciously assumed that Adam would respond to the upvoted comment, and not the one he was actually responding to. Weird... Sep 13, 2020 at 20:59

While I’m not too familiar with MathJax, I think the best solution is to block \newcommand (and similar) in titles. I searched and it looks like there are only 22 posts or so that would be affected (assuming I’m searching for the right commands). You can search any site with that query though the data is only updated once a week.