0

I am working on a chemistry project, and i'm trying to make a site that will show the needed equations, i have installed MathJax and mhchem, but the \ce command is not being processed and the output looks like this:

enter image description here

Where the input is: \ce{H2O + HCl <=> H3O+ + Cl-}

The MathJax config and connection to the site is the following:

<script type="text/x-mathjax-config">
MathJax.Hub.Config({
  TeX: {
    extensions: ["[Contrib]/mhchem/mhchem.js"]
}
});
</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.4/MathJax.js?config=TeX-MML-AM_CHTML"></script>
Penny Liu
  • 15,447
  • 5
  • 79
  • 98
Cornul11
  • 191
  • 1
  • 12
  • Without a code sample there's not much anyone can say. Random guess: something is stripping out the backslash. – Peter Krautzberger Mar 24 '17 at 16:44
  • Note: cdn.mathjax.org is nearing its end-of-life, cf. https://www.mathjax.org/cdn-shutting-down/. – Peter Krautzberger Apr 11 '17 at 15:19
  • @Peter, the problem itself is not in the MathJax, but in the malfunctioning MhChem. – Cornul11 Apr 12 '17 at 16:09
  • As I wrote, the code you posted works fine in general. I suspect something else is stripping the backslash in `\ce` (e.g., an editing tool, php, a markdown parser). Check the HTML source of the page if the backslash is really there or post a live sample that exhibits the problem. – Peter Krautzberger Apr 12 '17 at 16:53
  • Here's a link to a working copy of your code https://codepen.io/pkra/pen/MmWmWb – Peter Krautzberger Apr 12 '17 at 16:55
  • @PeterKrautzberger, looks that the MhChem was working even before posting this question. The problem is that the input is parsed by the default MathJax, not the MhChem, when changing the output dynamically (without reloading the page). Here's the example (sorry for the shitcode) - http://codepen.io/pkra/pen/MmWmWb. – Cornul11 Apr 13 '17 at 20:04
  • That link leads to the pen I shared. You'll have to fork it to save new examples. – Peter Krautzberger Apr 13 '17 at 20:19
  • @PeterKrautzberger, yes, my mistake, first time using that platform. Here is the correct link (http://codepen.io/Cornul11/pen/XRJdZz). – Cornul11 Apr 13 '17 at 20:33
  • 1
    As I guessed originally, you are not escaping backslashes in JS strings, e.g., `UpdateMath('\ce{H2O + HCl <=> H3O+ + Cl-}');` must be `UpdateMath('\\ce{H2O + HCl <=> H3O+ + Cl-}');`. – Peter Krautzberger Apr 14 '17 at 11:24
  • @PeterKrautzberger, hell yes, you are right. That's such an obvious error. An enormous thank to you. – Cornul11 Apr 14 '17 at 16:14

1 Answers1

0

The problem was in the way i passed the argument to the function which was updating the displayed expression, where i was not escaping the backslash:

UpdateMath('\ce{H2O + HCl <=> H3O+ + Cl-}');

Where the correct expression would be:

UpdateMath('\\ce{H2O + HCl <=> H3O+ + Cl-}');
Cornul11
  • 191
  • 1
  • 12