# TeX formatting in other comments in the same post is lost when the comment list is updated

If there's any TeX formatting in comments of a post, after you add a comment to that post, or click "add / show n more comments", all formulae are reverted to $...$.

Steps to reproduce:

1. Go to a post where there are formulae in the comments, e.g. Dot product in coordinates
3. See that formula ($v \cdot w = |v| |w| \cos \theta$) is reverted to code i.e. ＄v \cdot w = |v| |w| \cos \theta＄.
• Reproduced on Safari 5 and Opera 10.6. – Larry Wang Aug 1 '10 at 17:16
• (Problem still exists even with MathJax.) – kennytm Aug 3 '10 at 18:10
• test 1: $\LaTeX$ – Larry Wang Aug 14 '10 at 17:40
• test 2: plaintext only here – Larry Wang Aug 14 '10 at 17:41
• test 3: $\LaTeX$ again – Larry Wang Aug 14 '10 at 17:43
• new comment without TeX – Larry Wang Aug 14 '10 at 17:44
• another $\LaTeX$ – T.. Aug 14 '10 at 18:49
• on my browser this replicates the TeX-sensitivity of the bug in the form I had reported it: prior to adding [another LaTeX], with only K.H.'s [new comment without TeX], the Tex formatting was lost; after adding it, TeX formatting is retained or reprocessed, so no problem when clicking on "show more comments". – T.. Aug 14 '10 at 18:53
• @T..: I can't reproduce it even with another $\LaTeX$ (Firefox 3.6.8, OS X). – kennytm Aug 14 '10 at 18:55
• @Kenny: you get no bug at all, or no TeX-sensitive bug (for the test in these comments)? – T.. Aug 14 '10 at 18:57
• @T..: I mean even with "another $\LaTeX$" I can only see the raw TeX code. – kennytm Aug 14 '10 at 18:59
• I also get only the raw TeX code now, despite the LaTeX in the additional comments, and no matter what further comments are added. So there is some instability to this bug, at least on my system (Firefox 3.6.8, Mac OS X). – T.. Aug 14 '10 at 19:55
• @T..: Can't reproduce with a fresh install of Firefox 3.6.8 on OSX 10.5.8. That is, comments always get de-TeXed. Are you still using John Gietzen's plugin? – Larry Wang Aug 14 '10 at 20:22
• Test: $\mathbf{R}_{+}^{\times }$ for $\{x\in\mathbf{R}:x>0\}$ This is not properly displayed here, but it is OK in my profile's page. – Américo Tavares Aug 23 '10 at 21:58
• Is anyone working on fixing this? – anon Sep 20 '10 at 15:04

A workaround is to use the following userscript. (can some GM gurus see if it's possible to remove all those unsafeWindows?)

// ==UserScript==
// @name           Fix TeX related problems on math.SE
// @description    Fix TeX related problems on math.SE
// @include        https://math.meta.stackexchange.com/*
// @include        http://math.stackexchange.com/*
// ==/UserScript==

rl();
unsafeWindow.styleCode();
}


There are 2 options to fix this problem.

Option 1:

1. Edit "question.js".
2. Find the "var n = function (r, q)" function, inside the comments object.
3. Modify this function to:

var p = h(r, " > table > tbody");
if (p.children().length > 0) {
p.children().remove()
}
p.append(q);
d();
o();
if (typeof MathJax != "undefined")                // <--
MathJax.Hub.Queue(["Typeset", MathJax.Hub]);  // <--


This should fully solve the problem (I haven't tested), but it requires changing a file that is shared by every site.

Option 2:

1. Edit any Javascript file, e.g. "math-tex.js", after "master.js" is loaded.
2. Add these lines of code:

var rl = removeLoader;
rl();
MathJax.Hub.Queue(["Typeset", MathJax.Hub]);
}


This relies on the loader is being removed when a comment is successfully added, fetched or edited. It is pretty hackish.

• While it improves the experience of using the site, hiding problems using scripts like that just reduces the impetus to solve it properly, for everyone. – anon Sep 20 '10 at 15:05
• @muad: Looking at the history of new features/bugfixes, I get a vague sense that the opposite is true - it seems to be easier to get something changed if you can provide the code to make it work, and the stackexchange folks need only implement it, rather than expecting them to come up with a solution from scratch. – Larry Wang Sep 21 '10 at 18:03
• Awesome. Thanks for sharing the workaround! Although I am not a “GM guru”, I found a way to eliminate the use of unsafeWindow and posted it as a separate answer. – Tsuyoshi Ito Nov 11 '10 at 3:46

Here is a way to eliminate the need for unsafeWindow in KennyTM’s awesome workaround. I also added cstheory.stackexchange.com and the corresponding Meta.

// ==UserScript==
// @name           Fix TeX related problems on Stack Exchange sites
// @namespace      https://math.meta.stackexchange.com/questions/1114
// @description    Fix TeX related problems on Stack Exchange sites
// @include        https://math.stackexchange.com/*
// @include        https://math.meta.stackexchange.com/*
// @include        https://cstheory.stackexchange.com/*
// @include        http://meta.cstheory.stackexchange.com/*
// ==/UserScript==

var HTML_NS = 'http://www.w3.org/1999/xhtml';
var script = document.createElementNS(HTML_NS, 'script');
script.type = 'text/javascript';
script.appendChild(document.createTextNode(' \
void function(){ \
rl(); \
styleCode(); \
} \
}(); \
'));
document.body.appendChild(script);


(Edit: I changed the value of @namespace because now I realized that if I use other people’s URL as @namespace, it defeats the purpose of namespaces.)

Tested on Firefox 3.6.12 with Greasemonkey 0.8.20100408.6. I used the following posts to test the functionality.

In principle, the same user script should work on other Stack Exchange websites using MathJax if you add the corresponding @include lines.

(reposting analysis of the bug from related but not exact duplicate question that was closed.)

Example here: Question Relating Gamma Function to Riemann Zeta function evaluated at integers (see comments under Robin Chapman's answer).

When the page is loaded, TeX is processed for an initial set of visible comments. When clicking on "show more comments" below the answer, the Tex processor is not invoked unless the additional comments contain TeX. Comments in the original visible set that contain TeX and that are shuffled when the full set of comments appear, will revert to non-graphical plain text (dollar signs, TeX code).

I experimented on this in the example by posting another comment with TeX code. In the presence of "more comments" that do contain TeX, the display of TeX in all comments was re-processed after the click and worked correctly. So there is apparently no run of the TeX processing (for the entire displayed page) when additional comments are displayed, unless TeX code is detected in those comments. But the TeX display can be disturbed if lower-ranked comments chronologically precede top-ranked comments containing TeX.

• Are you saying you do not get this bug when (hidden comments contain only plaintext or when hidden comments are the most recent), or that you have not come across such situations? – Larry Wang Aug 13 '10 at 21:10
• That's hard for me to answer, because on repeating the experiment I can't replicate the earlier results. Earlier, posting TeX in the comments (repeatably) prevented reversion to plaintext, but now it reverts to plaintext no matter whether the added comments are plain or TeXed. In the latter case the added TeX displays as plaintext. This is in Firefox running on Mac OS X, though the problem seems browser independent. – T.. Aug 14 '10 at 5:21
• In that case, do you agree that it appears to be the same bug that KennyTM describes here? The only difference I could see based on your description would be if the situations I mentioned previously did not cause reversion to plaintext. – Larry Wang Aug 14 '10 at 16:53
• I'm not sure why my agreement is relevant. I posted an analysis and some experimental data on what trigger(ed) the bug. As such it is new information and not subsumed in the original posting by KennyTM. That it was repeatable before and not now may indicate any number of things, such as SE making changes to the software. It does not indicate that there was no sensitivity to the presence of TeX in new comments, since as I mentioned, the earlier experiments showed that to be a factor, at least when viewed on my platform. – T.. Aug 14 '10 at 17:10