msecore / graduate-coursepack

https://msecore.github.io/graduate-coursepack/
0 stars 0 forks source link

Define TeX macros for MathJax #3

Open chrisdaaz opened 3 years ago

chrisdaaz commented 3 years ago

I've added an HTML insert for configuring MathJax in mathjax.html. This is where you might want to add some TeX macros or other MathJax configurations. I added a few from jde_course.sty but there may be others I missed.

Links to relevant docs:

Note that Bookdown uses MathJax v2.7.x by default. If you want to use a different (later) version, you would follow this guide to reference a different version hosted locally or elsewhere.

emeryjdk commented 3 years ago

Great! I'll address this first to see if I can get some of the custom commands added!

emeryjdk commented 3 years ago

Fixed macros for Diff, Partial, and FullDiff with commit cf683a5.

Need to figure out how to add packages.

Looks great!

chrisdaaz commented 3 years ago

Thanks! As far as I can tell, MathJax doesn't support loading LaTeX packages. MathJax is scoped to "only the math-mode macros of TeX and LaTeX, not the text-mode macros." We'll have to use R Markdown to format text.

That said, we're using a predefined configuration of MathJax that includes a number of extensions, such as:

the TeX, MathML, and AsciiMath preprocessors and input processors, the AMSmath, AMSsymbols, noErrors, and noUndefined TeX extensions, the CommonHTML output processor definitions, and the MathMenu and MathZoom extensions

There are a couple of third-party extensions that mimic LaTeX package behaviors in this repository. It looks pretty limited, though. Maybe there's more relevant additions in MathJax 3.x

emeryjdk commented 3 years ago

It seems like there is some support for math-mode macros through packages such as "extensions" such as \color and \cancel (see here), but I don't understand how to adapt our mathjax.html to enable these extensions.

If we can put together an example with \color, I can try some other useful packages such as \units or \siunits (which are math-mode macros), which really save a lot of effort with the technical writing.

chrisdaaz commented 3 years ago

I think this is how we would edit mathjax.html to load extensions:

<!-- MathJax documentation: https://docs.mathjax.org/en/v2.7-latest/tex.html#tex-macros -->
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
  TeX: {
    extensions: ["color.js", "mhchem.js"]
    Macros: {
      Diff: ["{\\mathop{} \\mathrm{d^{#1}}{#2}}", 2],
      Partial: ["{\\mathop{} \\frac{\\partial^{#1}{#2}}{\\partial{#3}^{#1}}}", 3],
      FullDiff: ["{\\mathop{} \\frac{\\mathrm{d}^{#1}{#2}}{\\mathrm{d}{#3}^{#1}}}", 3]
    }
  }
});
</script>

The above should load color and mhchem extensions. I couldn't find \units or \siunits in MathJax proper, but there's this third-party extension we can try: https://github.com/burnpanck/MathJax-siunitx

I've pushed a testing branch that includes these configurations, including a self-hosted copy of siunitx.js. I don't know the LaTeX syntax or usage of these packages, so I haven't tried them out.

emeryjdk commented 3 years ago

Thanks, I'll take a peek and test some of the TeX commands.

chrisdaaz commented 3 years ago

I may have got this working, sort of:

image

I used a sample the author of siunitx.js provides here.

I'm using a self-hosted copy of the js with this hack in the index.Rmd file:

file.copy('siunitx.js', '_book/siunitx.js')

That line will copy static files to the output directory, which we use to serve the site.

emeryjdk commented 3 years ago

That looks perfect - math mode always switches to Computer Modern font by default, which is why the text jumps back an forth with the in-line siunitx syntax. I'll play around with it a bit and look at how you loaded the extensions.