Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Thanks #41

Closed
williamstein opened this issue Mar 16, 2022 · 5 comments
Closed

Thanks #41

williamstein opened this issue Mar 16, 2022 · 5 comments

Comments

@williamstein
Copy link
Contributor

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
Copy link
Owner

@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
Copy link
Owner

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
Copy link
Owner

Your workaround above can now be replaced by using:

delimiters: ['dollars', 'beg_end']

with Version 1.0

@williamstein
Copy link
Contributor Author

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
Copy link

vladtimss commented Nov 2, 2022

@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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants