I'm trying to follow the examples documented at https://docs.moodle.org/27/en/Chemistry_notation_using_mhchem#via_MathJax for enabling the mhchem extension. I've created a custom configuration at admin/config/content/mathjax which looks like this:

{
  TeX: {
    extensions: ["mhchem.js","color.js","AMSmath.js","AMSsymbols.js","noErrors.js","noUndefined.js"]
  },
  "tex2jax":{
    "inlineMath":[
      ["$","$"],
      ["\\(","\\)"],
      ["\[\ce{","}\]"],
      ["$$\ce{","}$$"],
      ["\(\ce{","}\)"]
    ],
    "processEscapes":"true"
  },
  "showProcessingMessages":"true",
  "messageStyle":"none"
}

This isn't working, so two questions:

- Does this module support extensions?
- Any advice for getting mhchem working properly?

Issue fork mathjax-2845974

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

ebeyrent created an issue. See original summary.

cilefen’s picture

Note, the default CDN URL is https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTM.... You may need to adjust that for this extension as per http://docs.mathjax.org/en/latest/start.html. Let us know what you find.

cilefen’s picture

There is this message in the docs:

(mhchem) is not included in any of the combined configurations, and will not be loaded automatically, so you must include it explicitly in your configuration if you wish to use these commands.

About the common configurations: http://docs.mathjax.org/en/latest/config-files.html#common-configurations

If you don't mind testing this on a plain HTML file you can probably figure out what is needed.

cilefen’s picture

Status: Active » Postponed (maintainer needs more info)
rominronin’s picture

I'm also trying to enable the mhchem extension, just started out now and found this issue thread.

I need to access the 'extensions' array of the Mathjax configuration. It seems that custom configuration text box in the Drupal configuration page is not the right place for this, since any attempts to add the mhchem extension in this box just leads to Mathjax failing to work.

Any help would be appreciated.

cilefen’s picture

If you were to post the things you tried and linked to any reference docs followed that would help.

rominronin’s picture

Following the suggested method for including mhchem here: https://mhchem.github.io/MathJax-mhchem/

and following the instructions from the documentation here: http://docs.mathjax.org/en/latest/configuration.html#using-in-line-confi...

I embedded the following configuration file:

MathJax.Ajax.config.path["mhchem"] = "https://cdnjs.cloudflare.com/ajax/libs/mathjax-mhchem/3.1.0";
MathJax.Hub.Config({
  extensions: ['tex2jax.js'],
  jax: ['input/TeX','output/HTML-CSS'],
  tex2jax: {
    inlineMath: [ ['$','$'], ['\\(','\\)'] ],
    processEscapes: true,
    processClass: 'tex2jax',
    ignoreClass: 'html'
  },
  showProcessingMessages: false,
  messageStyle: 'none',
  TeX: {
    extensions: ["[mhchem]/mhchem.js"]
  }
});

into the header, as a library:

mhchem:
  header: true
  js:
    js/mhchem.js: { attributes: { type: text/x-mathjax-config } }

On a page with regular maths expressions and chemical expressions, nothing was rendered (it all rendered as plain text). I tried the same thing with both the default cdn statement and also with just the plain MathJax.js loaded (without config query).

IF I remove the type attribute from the library definition, and also load it at the bottom of the file, THEN math statements are rendered, but chemical equations are not - which contradicts the advice given on the MathJax documentation link above, which recommends to embed the config before the mathjax.js file is loaded.

For absolute clarity, I'm using the following statements to confirm if it has loaded or not:

Maths statement:
$C_nH_m + n H_2O+\rightarrow nCO + (n + m/2)H_2$

Chemical statement:

$$\ce{H2O + HCl <=> H3O+ + Cl-}$$
$\ce{HCl}$ dissociates in water as follows:

Any ideas what is happening or what I'm doing wrong?

rominronin’s picture

I think I know why the maths statement was being rendered : When the Custom Configuration box on the module configuration page is deleted and saved, the default entry is reloaded.

rominronin’s picture

OK, I just renamed my config file to config.js, in case that was causing conflicts.
I just pasted the default module configuration code into my custom config js
{"tex2jax":{"inlineMath":[["$","$"],["\\(","\\)"]],"processEscapes":"true"},"showProcessingMessages":"false","messageStyle":"none"}

Again, this gives nothing.

The Page Source confirms that this is being loaded in the header, on line 101, and that mathjax.js is being loaded afterwards on line 1851. What's INTERESTING is that on line 1850, the config file from the module is being loaded:

<script src="/modules/mathjax/js/config.js?v=8.3.2"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js"></script>
<script src="/modules/mathjax/js/setup.js?v=8.3.2"></script>

Is this overriding any other configuration that I'm setting?

cilefen’s picture

Just guessing on a mobile here (take with appropriate grain of salt in that context) but instead of setting MathJax.Ajax.config.path perhaps you should change the URL in the module settings.

Other things to try:

Check the browser console for errors.

Try a proof of concept in a plain HTML page.

Ping me next week in Drupal’s #general Slack channel. I am in the New York City time zone.

rominronin’s picture

instead of setting MathJax.Ajax.config.path perhaps you should change the URL in the module settings.

The module appears to behave as expected, that is to say that any URL placed in the 'MathJax CDN URL' field will be loaded. However, if I replace the default URL for the mathjax cdn with the URL of the mhchem cdn, then the MathJax library itself will not be loaded (since there is only a single URL field for a CDN, this makes sense).

I tried this and it was confirmed (view the page source to see what libraries are loaded and in what order).

Normally, to load MathJax AND other extensions, you can enter a URL that contains a combined configuration into the CDN field. However, as pointed out in #3, there IS no combined configuration of MathJax and mhchem.

The browser console gives no error.

Try a proof of concept in a plain HTML page.

I'll give this a go next...

Ping me next week in Drupal’s #general Slack channel. I am in the New York City time zone.

Sure thing, thanks for the support so far!

cilefen’s picture

Status: Postponed (maintainer needs more info) » Closed (outdated)
shuklasp’s picture

I want to integrate mhchem on my site. Please guide me how to do that. I am using drupal 9 on my site http://www.vshiksha.com

cilefen’s picture

Status: Closed (outdated) » Active
shuklasp’s picture

Any luck?

cilefen’s picture

Version: 8.x-2.x-dev » 3.0.x-dev
cilefen’s picture

Version: 3.0.x-dev » 4.x-dev
fishfree’s picture

+1 pls.