pressbooks / pb-mathjax

A replacement for wp.com/latex.php but instead of LaTeX, it uses MathJax.
GNU General Public License v3.0
4 stars 1 forks source link

Add support for additional LaTeX delimiters #18

Open SteelWagstaff opened 5 years ago

SteelWagstaff commented 5 years ago

Many advanced math users, especially those who spend time writing in LaTeX, are used to \( ... \), \[...\] or the $ character behaving as special math delimiters. Other solutions, like WP QuickLaTeX, give users an advanced feature option to declare a page as a latexpage with a shortcode, and furthermore lets them declare $ as a math delimiter (when activated, they can use $. to denote a 'regular' dollar sign). MathJax 3.0 permits similar behavior through the use of custom delimiters: https://docs.mathjax.org/en/latest/options/input/tex.html#tex-options.

Proposed:

  1. We should add support for \( ... \) and \[...\] as valid delimiters in our LaTeX/MathJax plugin.

  2. We should allow users to choose whether or not to treat single dollar signs as math delimiters. To do so, we should add an option to the MathJax settings page that allows users to treat dollar signs as math delimiters. When selected we should add the inline math and process escapes configuration options described in the MathJax documentation. This would allow them to use additional delimiters (like those described here: i.e. $ as a math delimiter and \$ to create a 'regular' dollar sign) throughout the book.

SteelWagstaff commented 4 years ago

Regex: "/\\$\\$.+\\$\\$|\\\\\\[.+\\\\\\]|\\\\\\(.+\\\\\\)/"

pkra commented 4 years ago

This is how MathJax itself searches: https://github.com/mathjax/MathJax-src/blob/master/ts/input/tex/FindTeX.ts

SteelWagstaff commented 4 years ago

See http://docs.mathjax.org/en/latest/web/configuration.html#configuration-using-an-in-line-script

SteelWagstaff commented 4 years ago

For #1 above, I think we'd want to set up MathJax to use a local configuration file (see http://docs.mathjax.org/en/v2.7-latest/configuration.html#using-a-configuration-file) and use the following configs settings:

MathJax.Hub.Config({
  tex2jax: {
    inlineMath: [ ['\\(','\\)'] ]
    displayMath: [ ['$$','$$'], ['\[','\]'] ]
  }
});

This would cause \( ... \) to act as inline math delimiters, and `$$...$$ and \[ ... \] to act as display math delimiters.

For #2 above, we could also add a setting to the config menu that allows authors to use $ globally in the book as a math delimiter. If checked, the setting would result in a config statement that looked like this:

MathJax.Hub.Config({
  tex2jax: {
    inlineMath: [ ['$','$'], ['\\(','\\)'] ]
    displayMath: [ ['$$','$$'], ['\[','\]'] ]
   processEscapes: true
  }
});
SteelWagstaff commented 4 years ago

@connerbw recommended looking at how Jetpack handles $latex as a model for what we’ll need to do to get other delimiters working for various export formats. See https://github.com/Automattic/jetpack/blob/master/projects/plugins/jetpack/modules/latex.php

See our current method for supporting $latex as a delimiter here: https://github.com/pressbooks/pressbooks/blob/4cdee460dcc1800c7437297c9c45574650b6d814/inc/class-mathjax.php#L326-L426

SteelWagstaff commented 4 years ago

See also https://github.com/pkra/mathjax-node-page/blob/master/lib/tex.js (for v2 of MathJax)

SteelWagstaff commented 6 months ago

There is a GPL-licensed WordPress MathJax plugin that supports several kinds of delimiters: https://github.com/phillord/mathjax-latex