MathJax icon indicating copy to clipboard operation
MathJax copied to clipboard

How to define \symrm in Mathjax 2.7

Open wangzhen89 opened this issue 1 year ago • 3 comments

Hi guys, I want to write bold but non-Italic Greek letters in bookdown html (rendered by Mathjax 2.7). One solution may be the commond \symrm, but it is not supported in Mathjax 2.7. And new version of Mathjax is not well supported in bookdown, see this issue.

So my problem is how to define \symrm in Mathjax 2.7.

Thanks!

wangzhen89 avatar Apr 14 '24 00:04 wangzhen89

The default MathJax-TeX fonts in v2 don't include upright versions of the Greek letters, bold or otherwise, so in order to accomplish this, you would need to use one of the other fonts. The STIX font does include the needed characters, so you could use that. Because the v2 CommonHTML output only supports MathJax-TeX, you would need to use either the older HTML-CSS output, or the SVG output renderer.

Here is a MathJax configuration that defines a version of \symbf that could be used to get bold upright Greek letters:

<script type="text/x-mathjax-config">
MathJax.Hub.Config({
  "HTML-CSS": {
    fonts: ["STIX-Web"]
  },
  SVG: {
    font: "STIX-Web"
  },
  TeX: {Augment: {
    Definitions: {macros: {symbf: 'Symbf'}},
    Parse: {prototype: {
      csMathchar0mi: function (name, mchar) {
        var MML = MathJax.ElementJax.mml;
        var def = {};
        if (Array.isArray(mchar)) {def = mchar[1]; mchar = mchar[0]}
        this.Push(this.mmlToken(MML.mi(MML.entity("#x"+mchar)).With(def)));
      },
      Symbf: function (name) {
        var MML = MathJax.ElementJax.mml;
        var math = this.ParseArg(name);
        this.Push(MML.mstyle(math).With({mathvariant: "bold"}));
      }
    }}
  }}
});
</script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/unpacked/MathJax.js?config=TeX-AMS_SVG"></script>

The csMathchar0mi function is overriden to not include an explicit mathvariant="italic" that is in the original version, so that that mstyle element's mathvariant will be applied. This shouldn't hurt anything else, I think.

dpvc avatar Apr 15 '24 09:04 dpvc

Thanks! I am completely clueless about Mathjax, HTML, and JavaScript. But your codes run for me! But how to set the default output as HTML-CSS so that I don't have to right-click on the formula and select it. Really appreciated.

wangzhen89 avatar Apr 15 '24 10:04 wangzhen89

The example I have above uses SVG output, which should work for you. If you want to use HTML-CSS, then you have to change TeX-AMS_SVG to TeX-AMS_HTML.

It looks like bookdown has TeX-MML-AM_CHTML as the default in some of its templates (e.g., here, so you have to edit that to be TeX-AMS_HTML. It also looks like there may be some sort of mathjax variable that can be set to the URL of the MathJax you want to load. If you can change that value to

https://cdn.jsdelivr.net/npm/[email protected]/unpacked/MathJax.js?config=TeX-AMS_HTML

that should also do it.

dpvc avatar Apr 15 '24 11:04 dpvc