255

I know this has been discussed over and over again, and it may be very boring and annoying for people who have been around awhile, but I'm telling you: Unless this need is fulfilled, it will continue to come up again and again and again. I've read the other posts about this matter, and as far as I can see some of the best arguments have been put forward by Neysor.

The counter-arguments I have found on the other pages discussing this matter, along with my replies:

  • "If you need serious math, the question/answer does not belong on Stack Overflow anyway."

    Well, this may be true when we are talking about programming in C or Visual Basic. It is not true when we are talking about programming in Matlab, or R, or Octave, or Scilab, or … Questions about these languages very often involve the translation of mathematics into code, or programming problems are related to a misunderstanding of the math. Asking such questions and answering them is much facilitated by being able to use mathematical notation. Just a few examples I happened to be involved in here, here, here, and here. I'm sure there are many more.

    I guess one might argue that questions about Matlab & Co. do not belong on Stack Overflow because it is a different kind of programming. But the fact remains that the majority of Matlab questions on stackexchange are on Stack Overflow, and as long as there is no matlab.stackexchange.com, I'm pretty sure they will continue to pop up here.

  • "Enabling this means introducing an extremely heavy dependency."

    This does not correspond to my experience on other sites like math.StackExchange that do use MathJax. More objectively speaking, according to this detailed explanation it is simply wrong. The impact on pages that do not need math is minimal, and those that need it, well, need it.

    Talking about dependency in the sense of "it breaks if something goes away": Given its success, it is very unlikely that MathJax simply goes away, and it is much more likely that those image sites that people use now as a replacement will cease to exist. Moreover, if MathJax goes away there's still the LaTeX code itself which stays decipherable, while if an image site goes away, there's nothing left at all.

  • "The developers have better things to do."

    That may very well be true, but the fact is that enabling MathJax does not involve any new development whatsoever. Apart from the fact that this has been done for other Stack Exchange sites already, so it could just be copied: Enabling math simply amounts to including the following lines of code into the HTML delivered by the SO server:

<script type="text/x-mathjax-config">
    MathJax.Hub.Config({"HTML-CSS": { preferredFont: "TeX", availableFonts: ["STIX","TeX"], linebreaks: { automatic:true }, EqnChunk: (MathJax.Hub.Browser.isMobile ? 10 : 50) },
                        tex2jax: { inlineMath: [ ["$", "$"], ["\\\\(","\\\\)"] ], displayMath: [ ["$$","$$"], ["\\[", "\\]"] ], processEscapes: true, ignoreClass: "tex2jax_ignore|dno" },
                        TeX: {  noUndefined: { attributes: { mathcolor: "red", mathbackground: "#FFEEEE", mathsize: "90%" } }, Macros: { href: "{}" } },
                                messageStyle: "none"
});
</script>
<script type="text/javascript" src="http://beta.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"></script>

(Taken from math.stackexchange.com.)
  • "Enabling math puts an additional burden on the Stack Overflow server."

    Delivering eight additional lines of HTML code hardly does that, considering that even a short Q&A page on Stack Overflow may well have 800 lines of HTML code already. And as you can see from the code shown above, everything else is not loaded from Stack Overflow, but directly from mathjax.org. It's the burden on their servers that is increased, and even that is moderate, considering that the javascript files are not that large, and should be cached by the browser.

    The actual rendering is implemented completely client-side.

So, as far as I can see, there is no valid reason not to enable MathJax on Stack Overflow. Tell me if I've missed one.

46
  • 8
    Well, on the other hand, I don't see a compelling reason to add LaTeX support on Stack Overflow. 99% of questions which would require LaTeX would (in practice) be offtopic on Stack Overflow, even if you will (and I'm sure you can) come up with some valid questions right now. Commented Nov 2, 2013 at 21:13
  • 13
    Not a reason to avoid it if it was actually needed, but... A few of us just finished going through several hundred questions and retagging / editing / closing / deleting. Really glad I didn't have to wait for an extra rendering step while doing this. codinghorror.com/blog/2011/06/performance-is-a-feature.html
    – Shog9
    Commented Nov 2, 2013 at 21:13
  • 21
    @MadaraUchiha: I explicitly answered this counter-argument already in the post, in the first list-item. You work with PHP and javascript, I work with Matlab. I need this, and I see that many others have expressed this need, too. – I wonder whether anyone flagging this actually took the time to read.
    – A. Donda
    Commented Nov 2, 2013 at 21:16
  • 19
    It's still a huge dependency, it's additional files, additional scripts, an additional DNS lookup, and a slower page load time. This hasn't changed any. We won't enable this without an extremely good reason for doing so. We don't add 30 million file downloads a day to our users just because "there's no reason not to do it". We care about performance, this hurts performance.
    – Nick Craver Mod
    Commented Nov 2, 2013 at 21:17
  • 8
    Knowing the SO userbase like I do, this would get used even when it wasn't strictly needed, just to make certain formatting tasks slightly easier. Or as a replacement for bananas in comments. Which would then require it to be loaded and rendered on a bunch of pages where it contributed nothing useful. We enable this on sites where questions are very likely to make good use of it, not as a "nice to have" for the occasional post.
    – Shog9
    Commented Nov 2, 2013 at 21:19
  • 8
    Yes we have concrete reasons, the additional DNS lookup and script load even when there's nothing to do is a non-trivial delay to page load time, which we care deeply about.
    – Nick Craver Mod
    Commented Nov 2, 2013 at 21:20
  • 8
    I made comments here before, but they must have been purged by Big Brother. I guess Matlab is just sort of a misfit. Everyone seems to insist that if a problem requires math, then it doesn't belong on SO. But if a matlab question is not inherently mathematical, it doesn't belong on math.SE. Since syntactic questions do not belong on CS.SE, I guess that we users of Matlab are meant to be nomads, since there is no place for us.
    – nispio
    Commented Nov 2, 2013 at 21:20
  • 21
    Whatever we do to a page, multiply it by hundreds of millions of times a month. Any additional resources our clients have to load get loaded hundreds of millions of times. Given your own arguments, they're wasting their time and needlessly delaying page load time 99% of the time. That sounds like a terrible trade-off to me.
    – Nick Craver Mod
    Commented Nov 2, 2013 at 21:27
  • 6
    You know... There's no background image on Stack Overflow either... Or on MSO, for that matter. Page loads on beta sites are a drop in the bucket here.
    – Shog9
    Commented Nov 2, 2013 at 21:32
  • 3
    @NickCraver: Sorry to bother you, but this would actually be appreciated. And after that you can really say that the question has been answered and shouldn't be posed anymore.
    – A. Donda
    Commented Nov 2, 2013 at 21:49
  • 8
    I agree, and I'll write up a detailed answer with concrete numbers tonight.
    – Nick Craver Mod
    Commented Nov 2, 2013 at 21:50
  • 5
    @JanDvorak: Absolutely, since someone in the community is obviously going to have much better information than Nick has.
    – Robert Harvey Mod
    Commented Nov 2, 2013 at 21:57
  • 3
    Eh? I've answered my share of MATLAB questions and browsed/voted/edited several others and I'll say that there were very few questions where I've really needed MathJax. In most of those cases, I managed to do with just plain-text math (people can understand A_i in math means a subscript) and for the rest (perhaps 3-4 answers), I wrote up the equation in LaTeX and pasted a screenshot.
    – Lorem Ipsum
    Commented Nov 3, 2013 at 0:59
  • 6
    You think that people will only use MathJax for math on MATLAB/R/etc. question? Boy, are you wrong! Enabling MathJax also enables it on comments and you'll end up with millions of possibly creepy stalker crackheads who want to pimp up their comments (each of those pluses, hearts, and colors are due to MathJax).
    – Lorem Ipsum
    Commented Nov 3, 2013 at 1:02
  • 18
    (cc @NickCraver) Here's another fun fact: giving SO MathJax would be a breaking change. Commented Nov 3, 2013 at 1:49

5 Answers 5

164

This answer is to specifically address the "it's not a heavy dependency" argument. How might one do that? Well I just turned MathJax on for Stack Overflow for a few moments and ran some tests to confirm what we already know here: it makes the page slower. How much slower? See for yourself, here's are direct links to the tests. These are the numbers for the first visit (uncached) views of the homepage:

That's a significant performance penalty, even when "it's not doing anything". We're talking about a 21.3% increase in page load time for a user first hitting the site. Math.StackExchange is provided as a reference because it has the same HTML structure and content (besides MathJax), and actually has some work for MathJax to do...Stack Overflow has none. Keep in mind the exact same servers run all of these, they're literally the same application pool in IIS.

Also keep in mind the testing scenario actually helps MathJax percentage-wise. Many of the other domains being hit, namely: edge.quantserve.com, pixel.quantserve.com, www.google-analytics.com and ajax.googleapis.com are very likely to be in the DNS cache for a client already - they are used for analytics on many websites. You're seeing costs for those that, on average, is very unrealistic. beta.mathjax.org? that one's not going to be cached for most Stack Overflow users. Furthermore, jQuery, Google Analytics and QuantCast scripts are likely to be cached...many of those requests aren't just "not a DNS hit"...they're pure cache hits and have practically no time penalty.

This is the one certainty in programming: doing something is more expensive than doing nothing. When it comes to Stack Overflow we have to have a very, very compelling reason to make it any slower. I'll actually write a blog post soon as time allows on SSL and why it'll be optional (e.g. via HSTS) at first for the same reason.

To put things in an aggregate perspective: The homepage is loaded roughly a million times a day on stackoverflow.com, so on an average of 295ms slower x 1 mil hits = 81.9 hours a day of additional load time and 3 million extra requests. I won't go into how much harder this hits mobile users where HTTP requests are even more expensive.


Here's the time breakdown of page loads:

time breakdown key

Here's the breakdown of loads, this is Stack Overflow today:

Stack Overflow today (note that long stackauth.com SSL negotiation time...we're working on that)

This is Stack Overflow with MathJax enabled just a few moments ago:

Stack Overflow with MathJax

Where's all that time going? Well there's a blocking operation while it fetches the config you can see here:

MathJax blocking (ouch, that DNS lookup hurts, but the blocking for config hurts more)

Could we move that config to our CDN, to our site? Sure, but it still has to load for the page to continue. Either way, it significantly delays the time before the page is usable for the user.

32
  • 77
    The documentation suggests that since version 2.0 MathJax does not need to be loaded synchronously/before DOMContentLoaded, so it should be possible to load it asynchronously and not block rendering of the page. Has this possibility been considered? Commented Nov 3, 2013 at 1:16
  • 13
    @JeremyBanks it's a tradeoff, that introduces a huge delay in math rendering on the page, so the unformatted code is visible for a significant period of time. Since (so far) we only deploy this on sites where it's actually needed, it's a very perceivable problem. If Stack Overflow had any significant use of MathJax (if it didn't, we wouldn't turn it on...), then it would have the same issue.
    – Nick Craver Mod
    Commented Nov 3, 2013 at 1:19
  • 2
    @NickCraver, thank you very much for the detailed answer. Actually, I find the "repeat view" numbers even more convincing: 0.827 seconds vs 0.341 seconds. This together with Arjan's answer convinced me that it would be a bad idea to have MathJax on SO.
    – A. Donda
    Commented Nov 3, 2013 at 11:08
  • 1
    @ben many user avatars are on i.stack.imgur.com (most new users with an avatar), so we're eating that lookup either way.
    – Nick Craver Mod
    Commented Nov 3, 2013 at 16:49
  • 2
    @Cruncher to where? Imgur is geolocated to be near you, serving it from our stuff in New York is definitely a net loss.
    – Nick Craver Mod
    Commented Dec 12, 2013 at 21:05
  • 2
    @Cruncher we can't really have that, it means unformatted LaTeX everywhere for those with it disabled...we have it or don't at the site level for that reason.
    – Nick Craver Mod
    Commented Dec 12, 2013 at 21:17
  • 4
    The homepage of SO is loaded a million times ... for nothing. Anybody that really read the questions that have been posted the last second or so? Except for Jon of course. Personally I don't remember ever visiting the homepage on purpose. Anyway, it would be pretty easy to only load when required. Commented Jun 10, 2014 at 16:18
  • 3
    So why hasn't MathJax formatting been excluded from codereview.stackexchange.com - a site about code review? Or at least tell us why it makes sense to include MathJax on codereview (was it necessary)? btw, I'm not against its having MathJax, because it's certainly possible for a SO "how to implement..." question to actually require enough math that MathJax formatting is the only way to express the questions and answers clearly.
    – T. Webster
    Commented May 17, 2015 at 6:51
  • 8
    Anyone investigated katex? Comparison with mathjax here.
    – qed
    Commented Apr 27, 2016 at 10:48
  • 2
    I don't agree with this reasoning. We better make users pay little more time yet get more quality answers and discussion. Since many of the questions are math oriented this should be turned on.
    – Royi
    Commented Mar 28, 2017 at 18:32
  • 3
    Why can't it be enabled on a per-page basis (for pages which use/require it), instead of on a per-site basis (for every page on a site)?
    – ChrisW
    Commented Jun 4, 2017 at 13:26
  • 1
    @ChrisW Several reasons. For example, we'd have to preload all posts (and earlier in the page) and parse their titles and bodies in the question list pages (this also includes user profiles, etc.) which adds significant overhead, buffering, and allocations for Stack Overflow. And posts don't just appear in pages, they appear in APIs, mobile apps, emails, etc. All of these has to dynamically search and enable MathJax. It's still a ton of work for a very low percentage hit - we still don't see this changing in the foreseeable future.
    – Nick Craver Mod
    Commented Jun 6, 2017 at 10:28
  • 2
    Has these numbers changed after mathjax cdn move?
    – Braiam
    Commented Jun 17, 2017 at 23:18
  • 2
    I don't agree with the sentiment that a few millisecond is worth not having an essential feature. There are millions of questions that would benefit from being able to render math.
    – darksky
    Commented Mar 10, 2019 at 1:06
  • 2
    @NicolBolas I'd like to add my voice to the chorus that this is indeed an important feature and that it would be well worth a slightly slower page load from the user perspective. It comes up A LOT.
    – John
    Commented May 23, 2019 at 17:25
63

While loading MathJax for all pages is clearly not a good idea, there are other options.

There's already support for specifying a language for each code block: Syntax highlighting language hints .
We can use something similar for MathJax:

<!-- language: math -->
$$Beautiful Math$$

Or even:

<!-- language: math -->$Beautiful Inline Math$

The comment should only affect the immediate block following it.

Pros:

  • Only applicable to posts - does not affect titles, comments.
  • No breaking changes - you only get math where you explicitly ask for it.
  • Server knows whether MathJax is needed for a page or not, loading it only when necessary * does not slow down most pages (including homepage).

Cons:

  • Incompatible with Ajax (?) - Edited/new posts, pagination.
  • A little annoying.
8
  • 1
    I never missed math on Stack Overflow, but that's another discussion. If it is needed as many people claim, it looks possible.
    – Kobi
    Commented Nov 3, 2013 at 10:15
  • 1
    You ask to "merge" MathJax with Google Code Prettify? Don't think such thing is really possible... Commented Nov 3, 2013 at 10:21
  • @ShaWizDowArd - Nope. Just use the same/similar syntax. That isn't even a code block after the comment. Maybe <!-- MATH --> is better.
    – Kobi
    Commented Nov 3, 2013 at 10:23
  • 2
    Fair enough, let's see what dear Mr. Craver will have to say. :) Commented Nov 3, 2013 at 10:23
  • Interesting. But as far as I understand, it would still induce a break on a page where first one poster uses something like in Arjan's answer, and then another one triggers the use of MathJax by such a syntax hint.
    – A. Donda
    Commented Nov 3, 2013 at 11:22
  • 1
    @A.Donda - it should only effect the immediate block after it. I've updated the answer to clarify that. Is that better?
    – Kobi
    Commented Nov 3, 2013 at 11:28
  • So, you want the renderer parse the post, load MathJax if it finds the construct, and only then render it? Commented Jun 6, 2017 at 10:30
  • 1
    @ivan_pozdeev - Yes, that's the idea. Something simpler can also work, for example math$$...$$.
    – Kobi
    Commented Jun 6, 2017 at 10:34
48

Like Michael already commented, it might also be a breaking change? Like for:

[...] which then stores into a $_SESSION - the problem is most of the settings will be set by javascript, and as javascript is a client side script you can't access PHP $_SESSION variables as its server based. [...]

...suddenly rendering as:

Unexpected MathJax rendering

So, as for "The developers have better things to do", this might need some option to enable it per-post?

9
  • 3
    There is a workaround used on EE.SE you can read about at Inline LaTeX doesn't seem to work although because of the load times / lack of use don't think it's worthwhile for SO.
    – PeterJ
    Commented Nov 3, 2013 at 10:05
  • 3
    This is a strong counterargument. Of course one could use the standard LaTeX delimiters \[ \] and \( \) instead of the dollar signs.
    – A. Donda
    Commented Nov 3, 2013 at 11:19
  • 32
    Or we could just get people to use code formatting, which should have been used in this case anyway.
    – tckmn
    Commented Feb 18, 2014 at 19:38
  • 9
    That text should have used code formatting. I'm not inclined to care about optimising for it. Commented Jan 11, 2015 at 20:25
  • 1
    @LightnessRacesinOrbit There's tons of things that ought to have code formatting and don't. We can't break large parts of the site just because some people messed up. Code formatting or not, there's still valuable information in these places and we really don't need to screw up how it displays. Commented Jan 12, 2015 at 10:32
  • 5
    @ChrisHayes: Yes we can! Commented Jan 12, 2015 at 19:48
  • 19
    Breaking a PHP post? Seems like a feature, not a bug.
    – TylerH
    Commented Oct 22, 2015 at 14:28
  • 1
    Not an issue - it could be enabled only for questions edited after the official date of introduction.
    – einpoklum
    Commented Dec 12, 2018 at 15:59
  • The delimiter could be something different than the usual $ if needed. Commented Jun 30 at 19:03
20

It's not just the pageload that takes a hit. It's the preview that takes a hit, too. I've noticed that typing answers on Physics and Math is slower than on SO and MSO, probably because the preview reruns the mathjax renderer every keystroke.

Yes, this could be improved to behave like prettify where prettyprint is only run once every X seconds. But you usually want an immediate preview for MathJax because TeX is hard.

Also, on Stack Overflow is tiny compared to and and and and and and .... a lot more. All of these languages love the dollar symbol and have it denote variables or something similarly common. Of course, code formatting protects these from MathJax, but not everyone uses code formatting.

In contrast, for , you probably don't need TeX in every post. I agree that I'm no part of the tagging community so I may be horribly wrong here, but most of your questions don't seem to be the type that require math.

I guess it could be loaded on a per-tag basis as well. Or use something like the fiddle listed here that lets one decide to load MathJax dynamically.

3
  • 15
    +1. I've never wanted this feature, but if there are sub-communities who do, loading it only when the question has a mathsy tag seems to be the obviously best solution.
    – Mark Amery
    Commented Apr 26, 2014 at 16:19
  • 1
    Honestly, I don't think slower preview is that much of an issue. Preview takes time anyway.
    – einpoklum
    Commented Dec 18, 2016 at 10:22
  • 1
    SO should definitely ask people from other sites where Math is enable, either to reformat Q/A when they decide to migrate them here or not to migrate them or SO to enable Math on SO; otherwise they got totally messy and unreadable with the formatting after the migration took place. Commented Apr 23, 2020 at 6:01
-1

In a recent response I nearly broke my fingers, emulating math notations in hyper text.

We have emulators here, complete runtime engines for certain scripts, which are not labelled ´concurrent to performance´.

There is no qualified reason for not having an assistant, that generates math notations, without changing basic technical capabilities of the platform and without wasting users's time.

2
  • 1
    Re "nearly broke my fingers": That is what macro keyboards are for (or the equivalent). For instance, I have a macro key (without any modifier keys) to enclose the selected text in <kbd></kbd>. Commented Apr 6, 2022 at 9:05
  • @Peter Mortensen Yes, and it's a torture compared to Latex-Style notation. That's why would love to have an assistant for composing math expressions, e.g. as Word has a formula editor, where you don't need to care about the unreadable equivalent script. Commented Apr 6, 2022 at 9:09

You must log in to answer this question.

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