Math should be quotable

We have a discourse forum for a scientific software, and use the math plugin. Unfortunately, math is not quotable.

If I select some text, a Quote button appears:

Clicking it results in the following:

[quote]
definition of the closeness centrality:

ci=1N−1N∑j=11dij c_i = \frac{1}{N-1} \sum_{j=1}^N\frac{1}{d_{ij}}
[/quote]

Notice that in the quotation, the math appeared as a weird textual version (unreadable) followed by the actual LaTeX source, but lacking the $ delimiters.

I assume that there is no fix for this currently. It would be nice if this could be improved in the future.

8 Likes

This is still occurring and also happens with inline math, as reported by a member on our instance:

2 Likes

Note, I agree completely we should clean this up, but it feels extremely complicated.

I am putting a #pr-welcome on this in case someone with advances skills wants to give it a shot.

6 Likes

I had a look into quoting math (I’m considering writing a PR or contributing to one).

Documenting some initial thoughts and findings here.

So my initial impression is one of these points might be patchable:

  • toMarkdown matches elements against various html tags; mathjax uses custom elements I think that are detectable (mb not SVG rendering), mb katex does too?
  • selectedText does some matching for elements like code blocks and onebox – might be able to detect mathjax/katex elements and transform them? (Note: probably a good idea to have a hook or something here so the logic for transformation can be added to the discourse-math plugin instead of the main discourse codebase).
  • the other points don’t seem as suitable as either of these two.

Some other thoughts on approaches and how to get the raw math.

Depending on how katex / mathjax+svg[1] work, every instance of math should have a root HTML element I think (possibly multiple b/c there are some hidden elements for accessibility and things). So if we know the places that math exists, we might be able to parse the raw post for instances of content delimited with $ (but interpreters are tricky – mb there’s already one in the codebase?)

Alternatively, mathjax at least has a right-click > view-tex esq feature (which should be the literal string between $ pairs); I am not sure how to hook in to that, but if there’s a way to do that for mathjax v2, v3, and katex, then it should be possible to use that to replace selected formatted math with $...$ (note: that works for inline math, need to cover the case for the other kind, too – which uses $$...$$ or [/.../] delimiters.)

Problems atm:

  • IDK how to implement a hook or whether there’s something better to use / a better way of doing it in the discourse codebase.
  • Several unknowns about mathjax/katex that need to be investigated.
    • HTML elements – how to detect them reliably?
    • How to get raw TeX code?

If anyone has any other ideas for where/what to patch, other possible implementation methods, or whatever, then please post them.


  1. I think mb mathjax+svg isn’t supported, so that mb isn’t an issue ↩︎

5 Likes