# Area51 / SE 2.0 site-specific script support (such as LaTeX) in beta-phase

While equation markups are seldom needed on S[OFU], it is absolutely essential on many science-based SE 2.0 sites e.g. Mathematics, Statistical Analysis, Physics, etc. Unicode art is not enough for not-so-simple equations.

Being a professional site, they should have equation markup support (LaTeX if possible) since the beta phase, as not to repel professionals and hinder the site's popularity.

In MathOverflow it is handled by jsMath. But as all beta sites share the same template, it's possible only if site-specific script is allowed — is it / will it be allowed?

Moreover, I hope there is an official site-wise Opt-in LaTeX support, since this is and will be a common requirement for quite a number of beta sites. (This may take 6 to 8 weeks to implement though.)

(Alternatively site-specific formatting hooks could be installed to convert LaTeX into images at server side, ... but no one answered in 2 days!?)

-
+1 I agree that this is a critical need. –  user149013 Jul 22 '10 at 2:42
@Tobias: I prefer images generated by a real TeX engine. The Google API result is too ugly. Compare the one in your link and latex.codecogs.com/… –  kennytm Jul 22 '10 at 11:55
@KennyTM: looks definitely better, you're right. Please add this as an answer –  Tobias Kienzler Jul 22 '10 at 14:51
@Tobias: It would make yours a duplicate of meta.stackexchange.com/questions/12700/…. –  kennytm Jul 22 '10 at 15:48
@KennyTM: it is a subset anyway, I don't care about the how but about the if and my hopes to achieve something faster by proposing an actual solution didn't help anyway... –  Tobias Kienzler Jul 22 '10 at 15:50
I'd add theoryCS as another site where such support is needed critically. –  Suresh Jul 22 '10 at 20:40
This question has come up on meta.stats and meta.math, and it's going to come up on the LaTeX site soon too. It'd be really nice to have some info from someone working on SE2.0 about whether this is in the works or not. These sites are all going to be seriously handicapped until this feature is added. –  Noah Snyder Jul 23 '10 at 4:11
I have to say I'm a little concerned about the total radio silence on this issue. our Theoretical Computer Science site will be a nonstarter without it –  Suresh Jul 24 '10 at 17:26
We could use some help on this over at math.stackexchange ASAP. –  Tom Stephens Jul 25 '10 at 21:45
It's the weekend. Silence is not surprising. Could one of the folks who understands the details of what mathoverflow did write up an answer to this question to make the implementation process somewhat easier/faster for them? –  Larry Wang Jul 26 '10 at 0:27

This appears to be working on http://math.stackexchange.com. Thanks!

See the math.se meta.

-

Here's a rough outline of how we set things up at MathOverflow. As I understand it, only SO Inc. employees can perform administrative tasks (including loading javascript in the footer) on SE 2.0 sites, so this won't actually be useful to many people. But it should be pretty easy for SO to make into an option for SE sites. Some of it is crufty javascript workarounds that could be better implemented server side. Some of it is probably simply crufty because I'm not a javascript ninja; improvements are welcome.

We are currently using jsMath, but we'll be switching to jsMath's successor, MathJax, in the near future. The details will change slightly, but the basic idea will be the same.

The footer of every MO page loads the footer javascript, which I'll explain here.

loadScript=function(url,text){
var e = document.createElement('script');
e.type = "text/javascript";
e.src = url;
if (!text) text="";
e.text = text;
}


Now decide if math on the current page should be rendered. Since math gets rendered by javascript, it could be annoying for people to see things jumping around as math is being rendered on the home page. We don't render math on the home page or questions page by default, but we provide a way to set a cookie so that math is always rendered.

var path = window.location.pathname;
var isMath =
($.cookie("alwaysRenderMath")=="yes") || /^\/questions\/\d+/.test(path) || // Any of the question pages /^\/questions\/ask/.test(path) || // Ask page /^\/posts\//.test(path) || // Any edit page /^\/revisions\//.test(path) || // Any revisions page /^\/faq/.test(path) || // The FAQ /^\/tips/.test(path) || // The tips page /^\/users\/\d+/.test(path) || // The user pages /^\/tools\/.+/.test(path) || // Various tools pages false;  If the current page is supposed to be handled by jsMath, then load all the appropriate scripts and walk through the page to render any math. Note that our approach of loading a bunch of scripts by hand is a bad one, subject to weird timing issues. The right way to load jsMath is to use the easy loader and set a bunch of preferences in a config file that lives wherever the jsMath files live. This way is sensitive to the exact order in which the various files are loaded. For some reason I still don't understand, the easy loader didn't work for us. Hopefully we won't have any such problems with MathJax. Also note that we have latex.mathoverflow.net point to a server we control. Obviously, if this approach were implemented on SE sites, the same server would host the jsMath files. if (isMath) { jsMath = { Controls: { cookie: {scale: 110}, CheckVersion: function () {} } }; jsMath.Parser = {prototype: {macros: { mathfrak: ['Macro','{\\eufm #1}',1], operatorname: ['Macro','\\mathop{\\\\rm #1}',1], implies: ['Macro','\\Rightarrow'], Diamond: ['Macro','\\diamondsuit'], mod: ['Macro', '\\mathop{\\rm mod}'] }}}; document.write('<script type="text/javascript" src="http://latex.mathoverflow.net/jsMath/jsMath.js"><\/script>'); loadScript("http://latex.mathoverflow.net/jsMath/extensions/AMSsymbols.js"); loadScript("http://latex.mathoverflow.net/jsMath/extensions/AMSmath.js"); loadScript("http://latex.mathoverflow.net/jsMath/extensions/autobold.js"); loadScript("http://latex.mathoverflow.net/jsMath/extensions/boldsymbol.js"); loadScript("http://latex.mathoverflow.net/jsMath/extensions/moreArrows.js"); loadScript("http://latex.mathoverflow.net/jsMath/extensions/verb.js");$(document).ready(function(){
jsMath.Setup.Script("plugins/tex2math.js");
jsMath.ConvertTeX();
jsMath.Process(document);
});
}


If the current page has a WMD editor, overwrite it with a custom version that renders math in the live preview (let me know if you want to know more details). Also append a checkbox which sets a cookie in case the user doesn't want math rendered in the live preview (it gets slow for some people). If they have the live preview disabled, give them a one-shot preview button.

// if the editor loads, use custom wmd.js and include preview button
if (window.Attacklab) {
// Neuter the original WMD control
Attacklab = {wmdBase : function(){},
Util: {startEditor:function(){}}
};
if ($.cookie("mathPreview") == null) {$.cookie("mathPreview","yes",{expires:365, path:"/"}); }; // true by default
var mathPreview = ($.cookie("mathPreview")=="yes"); mathPreviewSpan = document.createElement("span"); mathPreviewSpan.innerHTML = '<input type="checkbox" id="math-preview-box"> Preview Math '; document.getElementById("post-editor").appendChild(mathPreviewSpan); mathPreviewBox = document.getElementById("math-preview-box"); mathPreviewBox.checked = mathPreview; mathPreviewBox.onchange = function(){ mathPreview = mathPreviewBox.checked;$.cookie("mathPreview",mathPreview?"yes":"no",{expires:365, path:"/"});
previewButton.style.display = mathPreview?"none":"inline";
Attacklab.previewManager();
}
previewButton = document.createElement("input");
previewButton.type = "button";
previewButton.id = "math-preview-button";
previewButton.onclick = function(){ jsMath.ConvertTeX('wmd-preview');jsMath.ProcessBeforeShowing('wmd-preview'); };
previewButton.value = "One-shot preview";
previewButton.style.display = mathPreview?"none":"inline";
mathPreviewSpan.appendChild(previewButton);
}


If you're on a page where extra comments might be loaded, overwrite the javascript comments function with one that renders math after fetching additional comments from the server (let me know if you want more details).

// load custom comments if needed
}


If you're on the preference page, add a checkbox to allow users to set a preference cookie to always render math.

// load preference box for math on the home page
if (/\/users\/\d+\?tab=preferences/.test(window.location.href)) {
var alwaysRenderMath = ($.cookie("alwaysRenderMath")=="yes");$("#optInEmail+label+br").after('<input type="checkbox" id="always-render-math-box"> Render math <i>all</i> pages, including the home page and questions pages. (Note: this simply sets a cookie, so this preference will not be remembered across browsers.)');
alwaysRenderMathBox = document.getElementById("always-render-math-box");
alwaysRenderMathBox.checked = alwaysRenderMath;
alwaysRenderMathBox.onchange = function(){
alwaysRenderMath = alwaysRenderMathBox.checked;
}
}

-
Thanks, Anton! Do you have any advice on JavaScript client-side solution vs something like CodeCogs? –  Larry Wang Jul 26 '10 at 7:57
+1, thanks for including that. –  George Stocker Jul 26 '10 at 13:09
@Larry: I don't like image-based solutions very much. One big advantage of MathJax is that it can render using HTML-CSS or MathML depending on browser capabilities and/or on a user preference cookie. You could also negotiate the content with the server, so it would definitely be nice to have some control server side. One way to speed things up with not much control over the server is to have it serve MathML and use MathJax to convert to HTML-CSS if necessary, which is much faster than converting from TeX to HTML-CSS, and if the user prefers MathML, you don't need to do anything. –  Anton Geraschenko Jul 28 '10 at 6:46

I second the request for site specific script support. I follow two proposals for Biblical Hermeneutics (Interested? Commit here) and Biblical Text Criticism. For both it would be great to have automatic linking of bible references, done for example with this script:

<script src="http://bible.logos.com/jsapi/referencetagging.js" type="text/javascript"></script>
<script type="text/javascript">
Logos.ReferenceTagging.lbsBibleVersion = "NASB";