Welcome to the Question2Answer Q&A. There's also a demo if you just want to try it out.
+1 vote
1.5k views
in Q2A Core by

I have added the following Mathjax code to the <head> of all pages.

<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    extensions: ["tex2jax.js"],
    jax: ["input/TeX","output/HTML-CSS"],
    tex2jax: {inlineMath: [["$","$"],["\\(","\\)"]]},
    TeX: { extensions: ["AMSmath.js","AMSsymbols.js"]}
  });
</script>

So all mathematical formulas between $ $ could be translated.(for example see mathoverflow.com)

My problem is when I write a mathematical formula in comments and add comment browser doesn't translate it until I refresh the page.

I have the same problem when I click on previous comments.

But I don't have this problem when I edit a comment. because after editing a comment the page will refresh.

 

This is a big problem because my website is about mathematic. :(

Many thanks for your help.

1 Answer

+3 votes
by
edited by

You need to call Mathjax again after the ajax post.

But this is not easily done with q2a 1.6.3 - since everything is coded in pure Javascript.

Check qa-question.js there should be something like:

qa_ajax_post('answer', params,

there I have added

                var answTxt = lines.slice(3).join("\n");
                if(answTxt.indexOf('^')!=-1 && answTxt.indexOf('$$')==-1 && answTxt.indexOf('\\(')==-1) {
                    e.innerHTML = answTxt;
                }
                else {
                    e.innerHTML = answTxt;
                    if(answTxt.indexOf('$$')!=-1 || answTxt.indexOf('\(')!=-1 ) {
                        // insert mathjax-config for linebreak option
                        $('head').append('<script type="text/x-mathjax-config"> MathJax.Hub.Config({ "HTML-CSS": { scale:100, linebreaks: { automatic: true } }, SVG: { linebreaks: { automatic: true } }, displayAlign: "left" }); </script>');
                        
                        var mjaxURL = 'http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML&locale=de'; // ,Safe.js
                        // as soon mathjax is loaded
                        $.getScript(mjaxURL, function() {
                            // mathjax loaded, call it on the comment which is e
                            MathJax.Hub.Queue(['Typeset', MathJax.Hub, e]);
                        });
                    }
                }
 

Hope this helps.

 

PS: This approach is used in my German math forum: http://www.gute-mathe-fragen.de/

by
Kai: Thanks. But in qa-page.js I just have these:
    qa_ajax_post('vote', {postid:postid, vote:vote, code:code},
    qa_ajax_post('notice', {noticeid:ens[1], code:code},
    qa_ajax_post('favorite', {entitytype:ens[1], entityid:ens[2], favorite:parseInt(ens[3]), code:code},
function qa_ajax_post(operation, params, callback)

There isn't qa_ajax_post('answer', params,
could you please help me?
by
Kai: Any ideas, please?
by
by
Hi Kai. your code works fine. But how to stop scrolling to top after adding comment or answer? could you please help me?
by
@Kai You solution is the best now, but would like to see this in Core as MathJax is used in many sites.
...