# Should titles be included in begingroup..endgroup (similarly as posts and comments)?

Not too long ago, a Stack Exchange software was changed in such way that macros defined in a post are valid only inside that post (similarly for comments). It was announced in an answer on this meta: The scope of \newcommand is the entire page.1

However, as titles are not enclosed in begingroup..endgroup, if a macro is defined in the title, it is still valid on the entire page (in the question, the answers and the comments).2 (You may also notice that the problem with the titles was mentioned by Joel Reyes Noche and quid in the comments under the linked answer.)

1. Would it be useful to include add begingroup..endgroup also for titles? (In the same way they are added around answers, questions, comments.)


One issue is the consistency. It seems unusual that in the posts, macros are limited only for the given post (and the same for comments), while macros defined in the title work on the entire page. There is a separate question whether macros should be used in the titles at all.

On one hand, using macros defined directly in the titles is probably not good practice, it is even explicitly mentioned in Guidelines for good use of $$\rm\LaTeX$$ in question titles. However, I think the main reason for that recommendation is probably the fear that the macro definition can "spill" into other posts (when the title is in the list of questions, frontpage, linked questions, related questions, etc.) and cause some problems.3

On the other hand, if adding begingroup .. endgroup around the title prevents the possibility that the macro defined in the title can cause problems elsewhere, perhaps there can be some legitimate uses. For example, by defining macro the source code can be made shorter and more readable. (One such instance would be using \operatorname only once in the macro definition, if the same operator is used several times in the title.) Readability matters for users editing the post - and also in the cases when the title of a question appears in some place where MathJax is not rendered.

Although macros in the title very likely bring all sorts of technical issues. Probably it's possible to make sure that the macro does not "leak" out of the title when the title appears within Stack Exchange networks - but it's reasonable to assume that the titles of posts from this site are also used elsewhere. Another issue is how the macros would be handled in the citations created by cite button. (And possibly many other problems which I have completely missed.)

1The same change is also mentioned on the networkwide meta in the post 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)"


Still, here are some examples showing that macros defined in titles are valid on the whole page: This question (Wayback Machine) has macro \Q defined in the title, it is used in the question without being defined in the body. This question (Wayback Machine) has macro \Re defined in the title - it is used in the question without being defined in the body. To include also one particularly bad example, in the title of this question (Wayback Machine) the macro \int is defined as \operatorname{int} $$\operatorname{int}$$ which overrides the usual meaning of \int $$\int$$.


3Here is an example of a problem which was caused by a macro defined in the title: A possible bug regarding MathJax, previews, and the arctangent/tangent functions?

• I was not entirely sure whether to post this here on the network-wide meta. Since the previous discussion and the announcement of the change were posted on this meta, posting follow-up here seemed reasonable to me. – Martin Sleziak Jun 17 '19 at 13:39
• Blacklist them all. – Asaf Karagila Jun 19 '19 at 5:53
• @AsafKaragila I wonder whether this was originally an answer and it was autoconverted to a comment. On a more serious note, perhaps with adding a few sentences about the reason why blacklisting is the way to go, your comment could be expanded to an answer - which would have the advantage that other users could leave some feedback on this. (If nothing else, at least by upvoting/downvoting.) – Martin Sleziak Jun 19 '19 at 5:57
• Yes, but that might take a few days, since I'm traveling right now. So I left the comment to record my view on this. – Asaf Karagila Jun 19 '19 at 5:58
• @AsafKaragila I have posted a CW answer which suggest to blacklist such stuff in titles. Feel free to comment there if something should be added. (From your previous comment I gather that you are now accessing the site mostly from mobile devices - which probably makes editing more difficult.) – Martin Sleziak Jun 19 '19 at 8:06
• You wrote "There is a separate question whether macros should be used in the titles at all." It seems illogical to propose such blacklisting before addressing this more fundamental matter, i.e. if someone makes a strong argument for needing such capability then that will certainly influence decisions on this matter. That some users cannot imagine a good use for something does not imply that it should be blacklisted (when instead it could easily be made safe). What's next, blacklisting of infinitesimals and surreals on main? – Bill Dubuque Jul 7 '19 at 13:59
• I have raised this on Meta Stack Exchange, perhaps in that way we are more likely to get some response from the Stack Exchange team: Shouldn't the titles be enclosed in the begingroup..endgroup, too? – Martin Sleziak Sep 13 '20 at 9:29


Here are some problems that might be caused by macros in the titles:

• A macro defined in the title can "spill" into other places. (And some examples showing that this can cause problems can be found on this meta.) Since the titles are displayed in many places on the site, it's probably not that easy to take care of all of them.
• It's quite plausible to assume that titles from this site can be displayed in many other places online, some of them do render MathJax. Even if the titles on this site are inside begingroup...endgroup and this prevents potential collision, we cannot be sure about the same thing on other sites.
• Cite button is intended to make easily accessible BibTeX entries for posts on Stack Exchange sites. If the title contains macro definitions, people using those citations in their documents would have to edit them manually.

1For the stuff that is currently blacklisted, see: Which are blacklisted tags and blacklisted phrases?


3In the case of \def we should be careful not to blacklist some legitimate MathJax/LaTeX commands which have \def as a prefix. (Although I cannot think of any from the top of my head.) As far as I know, when \def is used to define a macro, it has to be followed by a backslash.


I have posted this as an answer - this suggestion was made in comments, but answer is a better option for getting some feedback from the community. (Even if nobody posts a comment, at least from upvotes/downvotes we can see something about opinions of users around here on this.)

The answer is community wiki feel free to edit it further if needed. (Of course, without changing it to the opposite position.)

• But you might want to ask about those macros, and then use the macro names in the title for that purpose. I think blacklisting would prevent that as well. Unless it is possible to blacklist them only inside MathJax expressions, I'm against blacklisting. (BTW, is blacklisting site specific? Because blacklisting any LaTeX macro names in titles would be terrible for tex.SE) – celtschk Jun 20 '19 at 10:37
• @celtschk AFAICT, different sites has different blacklists for titles, tags, post bodies. Regarding blacklisting only inside MathJax, perhaps somebody with more experience in regular expressions can tell how difficult it would be. However, I have difficulties imagining on-topic question on Mathematics Stack Exchange where "\DeclareMathOperator" would be needed in the title - and even if there are such questions, I think that very likely changing "\DeclareMathOperator" to "DeclareMathOperator" would not decrease readability that much. – Martin Sleziak Jun 20 '19 at 10:41
• Still, pointing out that blacklisting only inside MathJax could be reasonable is a very good point. – Martin Sleziak Jun 20 '19 at 10:41
• Given that double dollar signs are already blacklisted, I guess it would be as simple as checking that there's an odd number of dollar signs preceding it (are there any ways to trigger MathJax without a dollar sign?) – celtschk Jun 20 '19 at 10:46
• @celtschk I believe that stuff such as $$x^2+y^2=z^2$$ or \begin{pmatrix} \lambda & 1 & 0 \\ 0 & \lambda & 1 \\ 0 & 0 & \lambda \\ \end{pmatrix} is a way to get MathJax without dollar signs. However, I think that most users include dollar. Just as an experiment, I have tried whether I'd be allowed to used them in a title (after the test I have immediately deleted the question). – Martin Sleziak Jun 20 '19 at 11:09
• In any case, I think that such environments are rather rarely used edge cases. (Which is probably the reason that they have not even been blacklisted in the titles. Blacklisting equation environment probably makes sense if $$..$$ is blacklisted - but as hardly anybody would use them, they were probably not worth blacklisting.) – Martin Sleziak Jun 20 '19 at 11:09
• @celtschk: Blacklisting is not only different for different websites, it is also different between a site and its meta. If someone wants to ask, on the main site, a question where \DeclareMathOperator is in the title, not in an "active capacity", then this question is in fact off topic. – Asaf Karagila Jun 27 '19 at 13:14
• @AsafKaragila: OK, so on the main site it can be blacklisted unconditionally, but the problem of course exists on the meta site as well. – celtschk Jun 27 '19 at 17:23
• @celtschk: Yes, but there's not as much math on meta for this to be a serious problem. It would be reasonable to allow these on titles on meta, and we should just make sure that the usage is not... trollish. – Asaf Karagila Jun 27 '19 at 17:24
• Also block \let? – Hagen von Eitzen Jun 30 '19 at 2:55
• To add to Hagen von Eitzen's comment, I did not find posts with \let in the title. There were two questions where \let was in the title, by it was later edited away: chat.stackexchange.com/transcript/88939/2019/6/30 – Martin Sleziak Jun 30 '19 at 10:45
• \DeclareMathOperator{rk}{rank} and \newcommand{Lim}{…} don't work in LaTeX (with default catcodes), but the other two (with or without braces around the command name) work just fine in LaTeX. – LSpice Jul 5 '19 at 2:51
• @LSpice Thanks for letting me know. I have edited the post - feel free to edit it further, if needed. (After all, it is community wiki.) – Martin Sleziak Jul 7 '19 at 7:05