42

I have some mathematical expressions written in LaTeX that I would like to put on a webpage. However, they use packages that don't seem to be included in MathJax (e.g. ytableau). They were sufficiently time consuming to write in the first place that I'd like to be able to use what I have, rather than figure out how to reproduce the results with the MathJax features I currently have access to.

I had thought that it may be easy to turn a LaTeX package into a MathJax extension as the extensions in the MathJax installation appear to be based on LateX packages, I looked at the cancel.sty LaTeX package and the cancel.js MathJax extension hoping it would give me some idea of how to do it but I can't see how they relate.

Is there a way of using LaTeX packages with MathJax? If so, how would I do this?

(First question, hope this is not deemed off topic.)

A Person
  • 531
  • 1
  • 5
  • 9

5 Answers5

37

Is there a way of using LaTeX packages with MathJax?

No. MathJax isn't LaTeX; it processes input that is LaTeX-like (as close as it reasonably can be to true LaTeX), but the way it works under the hood is very different. One difference in particular is that MathJax only implements the math-mode macros, not all the other text-mode or general processing macros and control sequences. So most of the tools used by LaTeX packages aren't there. That means you can not simply drop a LaTeX package into MathJax. The packages that MathJax supports have to be re-implemented in javascript, which is difficult for non-trivial packages (like the one you cite).

Davide Cervone
  • 11,211
  • 1
  • 28
  • 48
  • 4
    I'd like to add that the list of packages that _have_ been implemented is available [on the MathJax docs](http://docs.mathjax.org/en/latest/input/tex/extensions/index.html). _(This link is for MathJax 3.0 - check which version of MathJax the site has by typing `MathJax.version` in your browser console.)_ – Fons May 10 '20 at 17:06
7

Is there a way of using LaTeX packages with MathJax? If so, how would I do this?

  1. First the StackExchange site must support MathJax.

  2. You need to use the correct syntax for the particular site .

    Some sites require a backslash before the dollar sign, because when people try to write "it costs $10 shipping each time" they end up with "it costs $10 shippingeachtime", so to avoid excessive questions about why that happens the dollar sign must be escaped to use MathJax on some sites.

  3. You need to use $\require{\packagename}$ somewhere on the page, and it affects the whole page (including other people's posts).

  4. The site in question needs to support the extension. The official (but incomplete) list is here: "MathJax third-party extensions".

The most complete MathJax Tutorial and Links answer is over at QC.meta.SE.

Specific to your question, "ytableau" didn't work on the site where I tested these examples (physics.SE) but the other packages I demonstrate do work:

$\require{\AMScd}$

$$\begin{CD}
A @<<< B @>>> C\\
@. @| @AAA\\
@. D @= E
\end{CD}$$

Commutative Diagram

$\require{\mhchem}$

$$\ce{Zn^2+  <=>[+ 2OH-][+ 2H+]  
$\underset{\text{zinc hydroxide}}{\ce{Zn(OH)2 v}}
$  <=>[+ 2OH-][+ 2H+] 
$\underset{\text{tetrahydroxozincate(II)}}{\ce{[Zn(OH)4]^2-}}$}$$

Chemical Symbols The mhchem example was obtained from the MathJax-mhchem webpage.

Test a short example to determine if and how it works. Worst come to worst you can compose on a supporting site, capture and crop a screenshot, then enclose the MathJax in HTML comments <!-- comment --> and upload the screenshot - if in the future MathJax is enabled the code will be available in your question or answer within the HTML comment.

Community
  • 1
  • 1
Rob
  • 1,487
  • 2
  • 25
  • 29
  • 1
    `$\require{mhchem}$` loads the chemistry package, but `$\require{physics}$` doesn't seem to load the physics package. For instance, `$\require{physics}\dv[n]{f}{x}$` doesn't render. Any idea why? –  Jan 20 '19 at 11:40
  • 1
    @Blue - One answer is on the [Physics.SE](https://physics.meta.stackexchange.com/questions/6609/is-it-possible-to-implement-the-physics-package-of-latex-into-mathjax) site, not implemented server side. For example: $\require{physics}\dv[n]{f}{x}$ doesn't work on [Physics.SE](https://physics.stackexchange.com/) but $\require{\mhchem}\ce{H2O}$ works fine. – Rob Jan 20 '19 at 13:03
  • 1
    There are [a few tests](https://ickc.github.io/MathJax-third-party-extensions/physics/) using the extension on the [ickc *physics* MathJax Extension](https://github.com/ickc/MathJax-third-party-extensions/tree/gh-pages/physics) GitHub, so it ***is*** possible for it to work; it simply needs a reason for a Dev to enable it for a particular site. The [CTAN site](https://ctan.org/pkg/physics) and [Chemistry.Meta.SE](https://chemistry.meta.stackexchange.com/questions/3540/what-additional-formatting-features-are-available-to-mathjax-possibly-via-requ?__=1938240690) provide more information. – Rob Jan 20 '19 at 14:53
0

Yes. According to the official MathJax documentation, version 2.7:

While MathJax includes nearly all of the Plain TeX math macros, and many of the LaTeX macros and environments, not everything is implemented in the core TeX input processor. Some less-used commands are defined in extensions to the TeX processor.

MathJax will load some extensions automatically when you first use the commands they implement (for example, the \def and \newcommand macros are implemented in the newcommand.js extension, but MathJax loads this extension itself when you use those macros). Not all extensions are set up to load automatically, however, so you may need to request some extensions explicitly yourself.

To enable any of the TeX extensions, simply add the appropriate string (e.g., "AMSmath.js") to the extensions array in the TeX block of your configuration. If you use one of the combined configuration files, like TeX-AMS_CHTML, this will already include several of the extensions automatically, but you can include others using a mathjax configuration script prior to loading MathJax. For example:

<script type="text/x-mathjax-config">
  MathJax.Hub.Config({ TeX: { extensions: ["autobold.js"] }});
</script>
<script type="text/javascript"
    src="https://example.com/mathjax/MathJax.js?config=TeX-AMS_CHTML">
</script>

will load the autobold TeX extension in addition to those already included in the TeX AMS_CHTML configuration file.

You can also load these extensions from within a math expression using the non-standard \require{extension} macro. For example:

\(\require{color}\)

would load the color extension into the page. This way you you can load extensions into pages that didn’t load them in their configurations (and prevents you from having to load all the extensions into all pages even if they aren’t used).

Source: https://docs.mathjax.org/en/v2.7-latest/tex.html#tex-and-latex-extensions

If you want to work with version 3, here is the documentation: https://docs.mathjax.org/en/v3.0-latest/input/tex/extensions.html

If you want the latest version: https://docs.mathjax.org/en/latest/input/tex/extensions.html

Guzman Ojero
  • 2,812
  • 1
  • 20
  • 20
  • 1
    This only works for extensions to mathjax it won't take an arbitrary Tex package and load it...see the answer I'm posting now. – Peter Gerdes May 07 '21 at 19:27
0

So it depends on how complex the package is. If all it does is use def and newcommand to define some macros it's just a little grunt work to turn a package into a proper mathjax extension.

That process for v2 of mathjax is documented here. Particularly, note the fact that for newcommand based macros all you need to do is some basic escaping and put the macro def into JSON rep rather than a \def string. If you look a bit further in that page it also gives a very rough idea of how you would define more complex extensions but the bad news is that if you need something more than basic macro replacement (eg image drawing) you will probably need to completely reimplement the package in JavaScript without benefit of some of the low level Tex features (but you get access to the easy to use feature rich html DOM as compensation). I believe v3 is similar but docs still seem to be under construction.

Peter Gerdes
  • 2,288
  • 1
  • 20
  • 28
-1

This page gives the answer, which is yes. http://math.stackexchange.com doesn't seem to honor \(\require{foo}\), but the above link tells you how to add any package to the configuration for your website.

If you're not the administrator of the site, of course you will have to convince the powers-that-be to install the package.

Jeff Snider
  • 750
  • 1
  • 6
  • 20
  • Thanks for the response. I read through that page (before asking) and couldn't see the answer, must be missing something. Is it the section "TeX and LaTeX extensions"? The first thing I tried was \(\require{ytableau}\) but it didn't work. From reading the page you linked I figured that \require{...} only works for the extensions included in the MathJax installation and it seems I can't just drop a LaTeX package into that folder to use it because the extensions seem to be JavaScript reworkings of LaTeX packages to give the same functionality. – A Person Jan 17 '14 at 18:56
  • Could you explain a bit about how to do it? I seem to be missing the point. – A Person Jan 17 '14 at 19:02
  • 5
    This mechanism does *not* allow loading of arbitrary LaTeX packages. It allows you to load MathJax extensions, which are written in JavaScript. Some existing MathJax extensions mimic the functionality of, and have the same name as, widely used LaTeX packages (e.g. `amsmath` and `amssymbols`) but that's as far as the equivalence goes. – zwol Jan 17 '14 at 20:18
  • Is there a way to load a package with *options* using *\require* command? – Vlad Aug 23 '15 at 10:09