goessner / markdown-it-texmath

Support TeX math equations with your Markdown documents.
MIT License
156 stars 28 forks source link

Thanks #41

Closed williamstein closed 2 years ago

williamstein commented 2 years ago

I'm not aware of a discussion board, but just wanted to say thanks for this plugin. I just integrated it extensively into https://cocalc.com in this commit.

Regarding "Why doesn't markdown-it-texmath work with other engines ?" from the README, I needed to make this also work with mathjax (though we mainly use katex), and found it very easy to do so by using this option when creating the plugin:

engine: {
        renderToString: (tex, options) => {
          // We need to continue to support rendering to MathJax as an option,
          // but texmath only supports katex.  Thus we output by default to
          // html using script tags, which are then parsed later using our
          // katex/mathjax plugin.
          return `<script type="math/tex${
            options.displayMode ? "; mode=display" : ""
          }">${tex}</script>`;
        },
      },

I also needed to parse math environments that is determined by \begin/\end environments, but no dollar signs at all, e.g.,

Consider
\begin{equation}
x^3
\end{equation}

None of the delimiters you have support that, so I just extended the built in dollars one via a new delimiter called "cocalc" that does right after loading as follows. This didn't require changing the code of this extension at all. The regular expression below took me a while to come up with:

// The markdown-it-texmath plugin is very impressive, but it doesn't parse
// things like \begin{equation}x^3$\end{equation} without dollar signs.
// However, that is a basic requirement for cocalc in order to preserve
// Jupyter classic compatibility.  So we monkey patch the plugin
// and extend the regexps to also recognize these.  We do this with a new
// delim object, to avoid any potential conflicts.
texmath.rules["cocalc"] = { ...texmath.rules["dollars"] };
texmath.rules["cocalc"].block.push({
  name: "math_block",
  rex: /(\\(?:begin)\{[a-z]*\*?\}[\s\S]*?\\(?:end)\{[a-z]*\*?\})/gmy, // regexp to match \begin{...}...\end{...} environment.
  tmpl: "<section><eqn>$1</eqn></section>",
  tag: "\\",
});

In any case, many thanks for writing (and maintaining!) this plugin.

goessner commented 2 years ago

@William, thanks a lot for your kind words. I continue maintaining markdown-it-texmath at current, after a while of doing other things.

I consider integrating MathJax worth an attempt in the near future. I will try out your interesting script then also, which - I must confess - I do not understand completely (perhaps I need to learn a little more about MathJax first).

Your solution for \begin{...}...\end{...} seems to be of general interest (Yupiter + cocalc), so I will support it starting with version 0.9.8 .

Thanks for your support.

goessner commented 2 years ago

Your regular expression (slightly modified by me)

/(\\(?:begin)\{([a-z]+)\}[\s\S]+?\\(?:end)\{[a-z]+\})/gmy

won't work with nested \begin{...}...\end{...} content like

\begin{equation}
  \begin{pmatrix}
     A & B \\ B & C
  \end{pmatrix} 
\end{equation} 

Using Regex backreference here helps ...

/(\\(?:begin)\{([a-z]+)\}[\s\S]+?\\(?:end)\{\2\})/gmy

thanks

goessner commented 2 years ago

Your workaround above can now be replaced by using:

delimiters: ['dollars', 'beg_end']

with Version 1.0

williamstein commented 2 years ago

Thanks. I ended up getting a lot of feedback from my users about exactly what regexps, and in what order, etc., I was using.

They are a little different than when I wrote the comments above. I also added support for using \begin{math}...\end{math} for inline math, like in latex. Anyway, the exact rules I'm using are here, and they seem to have resulted in the least amount of complaints from people used to what's in Jupyter notebooks:

https://github.com/sagemathinc/cocalc/blob/master/src/packages/frontend/markdown/math-plugin.ts#L129

It does differ in some other ways from what you do, e.g., here's one:

        {
          // We modify this from what's included in markdown-it-texmath to allow for
          // multiple line inline formulas, e.g., "$2+\n3$" should work, but doesn't in upstream.
          name: "math_inline",
          rex: /\$((?:[^\$\s\\])|(?:[\S\s]*?[^\\]))\$/gmy,
          tag: "$",
          outerSpace: false,
          pre,
          post,
        },

That said, of course they way you did it by adding beg_end is nice since you can preserve backward compatibility for users of your plugin. If nothing else, the point of this comment here is just that it could be useful for other users wanting more options...

vladtimss commented 1 year ago

@williamstein @goessner Hi! You are very cool and your dev! Thanks. Could you advise me how can i use markdown-it-texmath with type script? where i can find types for it?