mathjax / MathJax

Beautiful and accessible math in all browsers
http://www.mathjax.org/
Apache License 2.0
10.02k stars 1.16k forks source link

How to define \symrm in Mathjax 2.7 #3218

Open wangzhen89 opened 2 months ago

wangzhen89 commented 2 months ago

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!

dpvc commented 2 months ago

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/mathjax@2.7.9/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.

wangzhen89 commented 2 months ago

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.

dpvc commented 2 months ago

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/mathjax@2.7.9/unpacked/MathJax.js?config=TeX-AMS_HTML

that should also do it.