facelessuser / pymdown-extensions

Extensions for Python Markdown
https://facelessuser.github.io/pymdown-extensions/
Other
957 stars 254 forks source link

Arithmatex: Small improvements #44

Closed facelessuser closed 7 years ago

facelessuser commented 7 years ago
facelessuser commented 7 years ago

Maybe add \[\] and \(\). Just give the people what they want :).

RoyiAvital commented 6 years ago

Could you please also add option to support $ ... $ and $$ ... $$ for advanced users.
It means those advanced users (You may call it LaTeX mode) are aware that for the $ sign he need to use escape \$.

I really like that currently Markdown Preview is compatible with LaTeX and StackExchane as I copy & paste a lot of data around from and to.

Yet as I found out in https://github.com/facelessuser/MarkdownPreview/issues/12#issuecomment-392253728 something breaks when using \{ or \} in Math Mode.

facelessuser commented 6 years ago

Please create a new issue. But I will go ahead and comment here.

$$ ... $$ is already possible. There is no restriction on spaces between $$.

I can consider the advanced mode for inline, but it will need ArithMatex's non-generic mode. Discussed later in this comment.

No Issue with \}

There is no issue with }. Let me explain why.

The examples posted here worked with one understandable issue. The 1st and 3rd were not parsed as expected. This is because \ is treated as a Markdown escape, but then placed back in the HTML as a single \. Then MathJax parses it because it is looking for \(...\).

\\( \forall i, \, {\mathcal{F} \left( g \right)}_{i} \in \left\{ -1, 1 \right\} \\)

\( \forall i, \, {\mathcal{F} \left( g \right)}_{i} \in \left\{ -1, 1 \right\} \)

\\(\forall i, \, {\mathcal{F} \left( g \right)}_{i} \in \left\{ -1, 1 \right\}\\)

\(\forall i, \, {\mathcal{F} \left( g \right)}_{i} \in \left\{ -1, 1 \right\}\)

What you actually need to do is double escape because Markdown will take \\\\( and turn it into HTML like this \\( and then MathJax will ignore it. Makes sense? Even curly brackets that are escaped are expected to translate from \{ to { if not handled specially. So this does not surprise me. Take a look at how other Markdown parsers handle this: https://johnmacfarlane.net/babelmark2/?text=%5C%7Btest%5C%7D+%5C%5C.

So when escaping by properly with \\\\(...\\\\). Let's look at the results:

screenshot 2018-05-26 12 55 47

Now Markdown Preview has another extension enabled called pymdownx.escapeall which will allow any character to be escaped. So you'll probably see additional things get coverted like \f --> f. But you can disable that if you find it surprising:

screenshot 2018-05-26 13 00 39

Non Generic Mode

But here's a trick. You can use Arithmatex's non generic mode (when the generic option is set to False which is the default) to actually wrap stuff in special MathJax scripts, which MathJax looks for by default. Then you don't even need to setup your JavaScript config to look for silly things like \(...\) in your HTML. Then escaping like \\(...\\) will work fine. As mentioned earlier, this would be required when using \$. Arithmatex does not process \$ even in generic mode. It treats it special, so \$ becomes $ in HTML MathJax will process it anyways unless you let it look for the scripts instead.

You can actually configure your own config in Markdown Preview and point to it instead. This one doesn't look for plain text to convert, but looks for the special non-generic MathJax scripts.

MathJax.Hub.Config({
  config: ["MMLorHTML.js"],
  jax: ["input/TeX", "output/HTML-CSS", "output/NativeMML"],
  extensions: ["MathMenu.js", "MathZoom.js"],
  TeX: {
    extensions: ["AMSmath.js", "AMSsymbols.js"],
    TagSide: "right",
    TagIndent: ".8em",
    MultLineWidth: "85%",
    equationNumbers: {
      autoNumber: "AMS",
    },
    unicode: {
      fonts: "STIXGeneral,'Arial Unicode MS'"
    }
  },
  displayAlign: "left",
  showProcessingMessages: false,
  messageStyle: 'none'
});

I actually tried to drop generic mode originally, but users requested it back again to work with non-MathJax libs. It probably makes sense for Markdown Preview to not use generic mode by default.