Discourse Math

In the preview plane whilst drafting a post, when the SVG gets rendered the preview plane scrolls up. Is there a setting that can be changed so the scrolling returns to lowest position?

Having to scroll down every time i edit math annoys my lecturers, but i have been coping

I’ve notice a couple of ā€œdeprecateā€ in the activity.

If someone could please explain what this means for an instance with a large databases of math that renders ok

after a full update/rebuild, the math seems to be rendering fine

For those that haven’t heard, this plugin is now bundled with core. Please see rebuild/revision 2 of 3 of this post to show this.

In the deed! :+1: Sorry for causing concern. You should be good to go now.

:partying_face: This plugin is now bundled with Discourse core as part of Bundling more popular plugins with Discourse core. If you are self-hosting and use the plugin, you need to remove it from your app.yml before your next upgrade.

3 Likes

I have a math character problem in the AI answers. It gives me answers in latex format even though I have typed the required prompt. This creates an image as if there are broken characters. Can you solve this problem. Example;

\( A = \begin{pmatrix} x & -1 \\ z & 2y \end{pmatrix} \)

\[
\boxed{
x = 0, \quad y = 4, \quad z = 4
}
\]

\[
\begin{cases}
A_{11} = B^T_{11} \Rightarrow x = y - z \\
A_{12} = B^T_{12} \Rightarrow -1 = -1 \quad (\text{benar otomatis}) \\
A_{21} = B^T_{21} \Rightarrow z = y \\
A_{22} = B^T_{22} \Rightarrow 2y = 8 \\
\end{cases}
\]

I have previously shared solutions from the openai community

Hi @ozkn

I too have this problem. My recommendation would be

  • sign up for an account with the AI provider
  • tell the AI account to

remember for future answers that Discourse is compatible with MathJax/Katex $…$ $$…$$ inline or multi-line centric formatting.

  • you should find your output more copy-paste’able when logged into your AI account
you may need to keep pushing for the best memory
3 Likes

Just to add to the last point — if you’re mixing math and currency, remember:

  • $...$ = math
  • \$ = literal dollar sign

So:
$x+1$ costs \$5. → renders math for x+1 and shows the price as $5.

That’s really all you need: math with $, money with \$.

2 Likes

another good one to encourage chatGPT to remember is that you can’t render math in details pane titles

@sam and all interested in typing math in Discourse. I’ve updated the discourse-math plugin so that it uses MathJax V3, rather than the much slower and very outdated V2. As expected, the result is a much snappier user experience, while still maintaining the feature rich environment as compared to KaTeX.

I’d love to issue a pull request, if you think the results look good.


You can see it in action on my class Discourse site:

Most of the content on that site is private or unlisted. There should be several topics at the top in the MathJax V3 category that illustrate the ideas, though

You can examine the code for the plugin in this standalone discourse-mathjax plugin repo. The file that has the most modifications by far is the initializer.

You can also use that repo to install it on a standalone site right now. Just be sure to remove the old repo during installation. Thus, you’d modify the standard plugin install technique to look like this:

hooks:
  after_code:
    - exec:
        cd: $home/plugins
        cmd:
          - rm -r discourse-math
          - git clone https://github.com/discourse/docker_manager.git
          - git clone https://github.com/mcmcclur/discourse-math.git

Comments

The most recent version of MathJax is actually 4.0.0. I chose to go with V3.2.2 for several reasons

  • While V4 is certainly much faster than V2, it’s not quite as fast as V3.
  • The user experience is a bit different in V4, particularly if the user clicks on the output.
  • The 4.0.0 status makes me wonder how many bugs there might be.

Having said that, the API for V4 is identical to that of V3. It should be possible to upgrade later, by simply dropping the latest MathJax Repo in.

I had to make one small change in the locales/server.en.yml file. Of course, there are a ton more such files for various languages. My understanding is that those other files would be automatically translated later?

I really don’t use chat at all and haven’t tested it in that context.

4 Likes

Pull request to upgrade MathJax to V3 made with all tests passed!

2 Likes

Regarding:

This is fantastic :hugs: , but I wonder if we can use this an an opportunity to trim down our repo a bit.

Now that we moved mathjax to core, we can lean on pnpm to pull the package and avoid bundling all the source like we do for FullCalendar for example.

Particularly have the goal of only having ā€œlinksā€ in our repo and then we can use the build process to pull the correct dependencies.

Give us a few days I want to consult with with the dev xp team here. Thanks so much for your efforts here!

4 Likes

Yes, I think that’s certainly the right thing to do. I always kinda wondered why you packaged the whole thing!

So, I guess you’ll build a loadMathJax function for your library that’s used to load MathJax?

I will say that rolling all the plugins into core has made it a bit trickier to to play with them. Tying the dependencies to the build process would only make it harder still, though I’m sure I could pull MathJax or FullCalendar from a CDN.

I’m mostly just talking about when I tinker with plugins for use in my own forums, though, and I absolutely think you oughtta pull MathJax in during the build.

Absolutely! I’ve been using Discourse for years and am super happy you think this is fantastic! :rocket:

3 Likes

Yes, exactly. A good one to copy is morphlex:

1 Like

I wonder if you’ve been able to discuss with your developer experience folks yet? I’d be happy to help, if I can. My impression, though, is that there’s really nothing I can do without your feedback on that.

I’ve made a few additional changes in a separate branch, that I’ll post about soon. I’m aware you’ve got a ton on you plate so I don’t mean to be a bother!

I’ve modified the discourse-math plugin so that it can parse a lot more mathematical input.

@sam When I first contributed to this plugin in 2017, I recall that you were quite firm that you wanted very strict parsing. Let me say up front that my main motivation to relax and extend the parsing was so that it would work better with AI. In particular, when you chat about mathematics with an AI bot you will often find that it responds using LaTeX and there are a lot of ways that it might choose to delimit that LaTeX input. So, while I understand your motivation for strict parsing, the changes I’ve made are rather essential for that use case.

Of course, you still might not care about that use case, so I’ve put the changes in a separate branch from my V3 pull request. If you decide you like them, I’m happy to issue another pull request.

The specific changes to the pull request are:

It accepts slash-paren delimited inline math like \(a^2+b^2=c^2\).

It accepts single line double-dollar delimited display math like
$$a^2+b^2=c^2.$$

It accepts single line slash-bracket delimited display math like 
\[a^2+b^2=c^2.\]

It accepts multi line slash-bracket delimited display math like 
\[
a^2+b^2=c^2.
\]

Of course, it still accepts the inputs of the original:

Dollar delimited inline math: $a^2+b^2=c^2$.

Multi-line, double dollar delimited display math:
$$
a^2+b^2=c^2.
$$

You can find the relevant branch here.

The code also exists as a standalone plugin.

Oh, you can also see it in action!

2 Likes

@mcmcclur Thank you for your work. It would be great to see these features in the core.

1 Like

Thanks so much Mark.

My big blocker here is that I really want to move to the new patterns for dependency distribution, see:

Is this something you could look at?

Regarding relaxed syntax, it feels like a site setting to me, maybe even default on given all the LLMs out there?

2 Likes

@mcmcclur I was fiddling with this today:

Far from ready … but stuff sort of boots with 4.1 which is nice.

2 Likes

Yes, this is definitely progress!

The first key issue to address, as I suspect you know, is that the fonts are not found. In fact, I fiddled with this line in discourse-math-mathjax.js:

fontURL: getURLWithCDN("/assets/mathjax/woff-v2"),

As a test, I set the URL to simply point to a temporary directory on my own webserver, and the initial results look very good. So, it’s a matter of getting those fonts installed correctly in Discourse.

In a simple pnpm project on my machine, the following command installs the fonts:

pnpm install @mathjax/mathjax-newcm-font@4

When I run that command within discourse/frontend/discourse, the fonts appear in

/discourse/frontend/discourse/npm_modules/@mathjax/mathjax-newcm-font/chtml/woff2/

Those fonts don’t seem to land in /assets/mathjax/woff-v2 after build, though. I’ve tried a number of variations on the directory but haven’t got it to work. I assume this is some sort of routing magic that I’m no expert in. I’m pretty sure I could make good progress toward cleaning it up, once that path issue is settled.

1 Like

@sam I think I’ve made some pretty significant progress on this, with one significant caveat. I’m not sure where to load the desired components from. Expressed in code,

window.MathJax = {
    loader: {
      // This does not work:
      // paths: { mathjax: getURLWithCDN("/assets/mathjax") },
      // But this works great:
      paths: { mathjax: "https://cdn.jsdelivr.net/npm/mathjax@4.1.0" },
      load: ["core", "input/tex", "input/mml", "output/chtml", "output/svg"],
    },
    // More configuration ...
  };

When I say the commented out version doesn’t work, I mean that I get the explicit message:
MathJax(core): Can’t load ā€œ/assets/mathjax/core.jsā€

Note that, in both cases, the loadMathJax function is pulling the MathJax startup from the local copy. That is, I’ve got the following in
/discourse/frontend/discourse/app/static/mathjax-bundle.js

export * from "mathjax/startup.js";

Then, loadMathJax defined in
/discourse/frontend/discourse/app/lib/load-mathjax.js
calls

const bundle = await import("discourse/static/mathjax-bundle");

This suggests a couple of possibilities:

  1. Perhaps /assets/mathjax is not the correct location or
  2. Perhaps, these assets need to be registered in some way so that they appear in the dist?

Working off of the CDN version, it looks like I can make significant progress, but I assume that’s a major blocker for you.

I could share my code with you, if you like, but maybe that’s enough information for a diagnosis?

1 Like