This is a feature request, which I'll admit is probably not too important for most users.
I think it would be really amazing if text in between $dollar signs$
could be rendered as inline math formulas using MathJax, similar to what you see on some of the stackexchange sites. It's also common to use $$double dollar signs$$
to denote full equation blocks.
This may obe an easy addition, seeing as markdown is already supported. With names like "matrix" and "vector", there's bound to be at least a small subset of users who actually use this chat client to talk about math! It's hopefully at least worth considering, anyway.
Thanks for the feature request - agreed this would be very cool. What
typically goes between the dollar signs? MathML? TeX? ASCIImath?
On 10/08/2016 19:38, Jesse Maes wrote:
This is a feature request, which I'll admit is probably not too
important for most users.I think it would be really amazing if text in between |$dollar
signs$| could be rendered as inline math formulas using MathJax
https://www.mathjax.org/, similar to what you see on some of the
stackexchange sites. It's also common to use |$$double dollar
signs$$| to denote full equation blocks.This may obe an easy addition, seeing as markdown is already
supported. With names like "matrix" and "vector", there's bound to be
at least a small subset of users who actually use this chat client to
talk about math! It's hopefully at least worth considering, anyway.— You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/vector-im/vector-web/issues/1945, or mute the
thread
https://github.com/notifications/unsubscribe-auth/ABO_vZsg18vTcaqttEVn7_YKZBAeGrB0ks5qem7tgaJpZM4Jhta4.
I'm definitely most used to seeing TeX notation for inline math. As far as I know it's the most commonly used and versatile of those three - I've never found anything it couldn't write down. It's also the only one I've used though, so maybe I'm biased there.
As a CS student with a friend circle of massively nerdy people (math, physics, programming etc) I certainly would have some uses for this, although it's by no means critical.
+1 for TeX
As a DSP engineer I would much appreciate this feature, as sharing math with remote co-workers and friends has been rather painful for a long time.
+1 for TeX
markdown-preview-plus from Atom text editor supports something like this, btw.
So... When are we going to have it? :)
It will really come in handy in the physics chat.
+1 for TeX
It could be worthwhile looking into Khan Academy's KaTeX as a speedy, lightweight alternative to MathJax.
As far as encoding the math in the message event goes, I think that it makes sense to send it as MathML (obviously in a "format": "org.matrix.custom.html"
message). MathJax can be used to convert various formats (including LaTeX) into MathML, and it can be used to display the MathML in browsers that don't support MathML. An alternative to sending MathML would be to stick LaTeX math inside a special element. For example, MathJax uses a <script type="math/tex">
element to indicate math text, or it could be a <span>
with a special class..
Pros of sending MathML as opposed to sending the raw LaTeX wrapped in an element:
Cons of sending MathML:
This would all also depend on what can be used to do math on iOS and Android.
Note: MathML allows embedding the original LaTeX source via annotations
(BTW, a note about KaTeX: it is much faster than MathJax, but also doesn't support as much LaTeX as MathJax does. What we do on our site at work is that for each chunk of math, we first run it through KaTeX, and if it errors, then we run MathJax on it.)
Robotics engineer here, yes please!
LaTeX as default would definitely be most sensible I reckon; I don't think any of my colleagues know MathML or ASCIImath. Every single one knows LaTeX.
As a CS researcher, I would highly appreciate this feature. Including math formulas in our daily conversations is very common among my colleagues. It would be nice if we could do it instantly in chat flow.
+1 for TeX
I would love to see this!
I would too!!!! But we have no choice but throw ourselves on the generousity and creativity of the internetz for this one...
Just throwing a possible dep: https://www.npmjs.com/package/react-mathjax
Not sure yet how to use it in riot-web exactly but seems to be pretty simple.
I made a proof of concept where the client renders math formulas between $ ... $
or $$ ... $$
using KaTeX on
https://github.com/hgustafsson/matrix-react-sdk/tree/hgustafsson/katex
https://github.com/hgustafsson/riot-web/tree/hgustafsson/katex
Ideally, there should be a room-specific option for turning this on and off (probably off by default), and for specifying different delimiters, but I don't know how to do that.
I was also playing with the idea that the sender generates an already formatted message (formatted_body
) in html or mathml using KaTeX, but the riot-web receiver removed these tags in the html sanitizer of the function bodyToHtml
.
From an inter-client compatibility standpoint transforming to html on the way out makes sense to me? In that if (not riot) client B can render html then if riot with KaTeX sends html it will display as html and not latex source.
@Cadair for consistency the HTML transform is always done by the sender
@t3chguy sure, that makes sense. @hgustafsson was saying that that approach isn't working.
yeah, currently the subset of allowed HTML is very strict, it may need to be loosened or another format being added to support it, currently the format is a custom html one, adding another would be pretty simple
@Cadair @t3chguy I agree that this would be the best, but all markup was removed by the html sanitizer. I'm not confident enough with the security implications to change the allowed tags and attributes in sanitizeHtmlParams
of HtmlUtils.js
. (The same security implications would apply even if we make another message type/format for math which would allow more tags.)
The result of, for example, katex.renderToString("\cos(x)")
looks like
<span class="katex-display">
<span class="katex">
<span class="katex-mathml">
<math><semantics><mrow><mi>cos</mi><mo>(</mo><mi>x</mi><mo>)</mo></mrow><annotation encoding="application/x-tex">\cos(x)</annotation></semantics></math>
</span>
<span class="katex-html" aria-hidden="true">
<span class="strut" style="height: 0.75em;"></span>
<span class="strut bottom" style="height: 1em; vertical-align: -0.25em;"></span>
<span class="base">
<span class="mop">cos</span>
<span class="mopen">(</span>
<span class="mord mathit">x</span>
<span class="mclose">)</span>
</span>
</span>
</span>
</span>
Unfortunately, all the work I did in this direction was when marked
was still used for Markdown for which I could use ViktorQvarfordt/marked and write a custom rendering engine for math using KaTeX. Hopefully something similar can be done with commonmark
but I haven't looked at it.
then make an alternate format which is done client side, formatted_body being the LaTeX formatting and render clientside, assuming that the KaTeX implementation is sane and 100% consistent
Any progress in it?
Please don't use KaTeX.
Use MathJaX as it is much more feature rich.
In my experience, MathJaX covers much more LaTeX than KaTeX does, but KaTeX still covers the vast majority of what most people need. However KaTeX is much faster than MathJaX (though I believe the MathJaX developers have been working on improving speed lately). Also, MathJaX would randomly fail some times when we had a lot of math. So what we did was we rendered math in KaTeX, and if KaTeX was unable to render it, then we fell back to MathJaX.
My experience matches @uhoreg's, and I would urge whoever ends up developing this to consider using KaTeX by default.
KaTeX is much faster, and does not have the annoying reflowing issues. MathJax's performance made it almost unusable on my old Android phone. KaTeX also seems to cover the vast majority of what people normally need (at least in physics and compsci). New commands are constantly being added as the project is very active.
<braindump>
(or maybe \begin{braindump}
)
In order for this to happen, several tasks need to be done:
\newcomand
. But should it support AMSMath? For MathML, it may make sense to just limit it to Presentation or Content MathML. etc.)$...$
notation may cause non-math to be accidentally matched, so it may be better to use \(...\)
instead). AsciiMath might be another option for hand-typed math. For a user-friendly, glossy math entry, something like MathQuill might be worth looking at.This issue needs someone to work through all of these things and write out a proposal.
\end{braindump}
(or </braindump>
)
I think Tex would ideally be transmitted in its raw format using extensible events.
While I agree, there is no need to block this on that proposal becoming reality.
Any updates on LaTeX being supported?
Here's a demo which compares Katex and Latex performance.
Also I think having a delimiter for inputting Latex outside of Markdown mode is unnecessary.
Mathquill looks really nice.
@Evidlo ,
This test is spread by KaTeX.
KaTeX is faster, yet in real world does it make any difference? In my opinion, No.
I would chose MathJaX over KaTeX any time any day.
Riot is already loading a huge pile of JavaScript. What difference is a tiny bit of MathJax going to make. 😃
But even getting KaTeX will make me more than happy because we will have _something_.
Riot needs to get faster, not slower!
On that note, in the aforementioned demo/benchmark KaTeX is over 10 times as fast for me, across three browsers. I'm unsure why but it seems MathJaX isn't very efficient.. I have no idea how they compare in terms of features though.
There is a lot more to this issue than a selection of MathJax vs KaTeX, the hardest things to sort out is how the messages will be encoded and how fall backs will be provided. If done well the section of the actual js library should be entirely down the the client implementation.
There is a lot more to this issue than a selection of MathJax vs KaTeX, the hardest things to sort out is how the messages will be encoded and how fall backs will be provided. If done well the section of the actual js library should be entirely down the the client implementation.
I thought the idea was to not block this while waiting for extensible events?
Regardless of whether it comes before or after extensible events, we still need to decide on how the math is transmitted. https://github.com/vector-im/riot-web/issues/1945#issuecomment-387925133 outlines what needs to be done.
I think extensible events might be too restrictive, as they limit the granularity of switching math typesetting on/off to that of a message. In practice, inline math is far more useful IMO. One might use a separate message type for 'may contain math' though.
As for encoding, a reasonable option might be 'an implementation-defined subset of AMSLatex', with fallback to showing the raw markup.
Regardless of whether it comes before or after extensible events, we still need to decide on how the math is transmitted. #1945 (comment) outlines what needs to be done.
I say leave the TeX as-is for transmission (in some container). As for devices that don't support rendering, just display the TeX. The sort of people who will be using this are going to be familiar with reading TeX source anyways.
The most obvious is $ ... $
with a button for enabling Latex, similar to the Markdown button. I don't think we should worry about fancy input options like MathQuill right now.
I vote KaTeX. For those opposed, are you able to think of any declarations or commands off the top of your head which are not in this list? I couldn't.
Since a TeX renderer is more heavyweight than markdown, it might not make sense to have it enabled all the time. This could be a checkbox under USER INTERFACE
in the global Riot settings. Alternatively, you might enable KaTeX automatically if there is a TeX message on the current page.
- I vote KaTeX. For those opposed, are you able to think of any declarations or commands off the top of your head which are not in this list? I couldn't.
This page lists unsupported LaTeX features in KaTeX. Just a relevant information to have here.
Edit: Hit Enter too soon.
I think KaTeX is good enough. We won't need a chat client to provide the most comprehensive LaTeX support. We can temporarily switch to other methods, such as attaching documents, to discuss more complex ideas.
I also agree with @Evidlo 's "leave the TeX as-is in a container" idea. Those who deal with math should be familiar enough to recognize it in case the client does not support it.
I also agree that without extensible events the best thing to do is to leave the on-wire format as the raw tex source. I think with extensible events we could provide the maths in a couple of useful formats.
I've been slowly writing a MSC over the past week to answer question 1 in https://github.com/vector-im/riot-web/issues/1945#issuecomment-387925133. The result is https://github.com/matrix-org/matrix-doc/pull/1722.
are there any updates on this?
are there any updates on this?
Unless there is a secret cabal of mathematicians plotting somewhere, I'm not aware of anyone who has done any work on this recently. It's on my to-do list to look at this again after I'm done cross-signing, but that list is getting pretty long, so if anyone wants to jump on it, feel free to.
are there any updates on this?
Unless there is a secret cabal of mathematicians plotting somewhere, I'm not aware of anyone who has done any work on this recently. It's on my to-do list to look at this again after I'm done cross-signing, but that list is getting pretty long, so if anyone wants to jump on it, feel free to.
I would love to be such a secret mathematician, but am not too sure what the current status is/what needs to be done. Does Matrix itself support displaying maths in messages? https://github.com/matrix-org/matrix-doc/pull/1722 seems to still be open.
OK, so what needs to be done is:
# 1 is the main part that I'm going to be looking at. Someone else could probably start looking at # 2 and 3 right away, even though matrix-org/matrix-doc#1722 isn't finalized, as the hard part (IMHO) is integrating everything into Riot, and it should not be too hard to switch formats if needed, if it is done in a way that's flexible. Though that's just a guess on my part.
in regards to the first two points, it seems like converting from LaTeX syntax to MathML is reasonably simple (in particular, there are already a bunch of libraries online that do such things), so in terms of input, I would say that the standard "put maths between $ delimiters in LaTeX syntax" would be enough? I'm sure that a fancy GUI would be nice, but I feel like the target audience for this capability would mostly be happy without such a thing.
The standard "put LaTeX inside delimiters" would certainly work as a first cut. I'd be a bit cautious of just using plain $, as there is a chance of accidentally triggering math when it isn't intended, but there are ways to reduce that (e.g. only triggering if there are no spaces at the beginning/end of the math). A GUI, even a simple one, would be helpful for some users, as not all people who want to enter math are as experienced with LaTeX (e.g. student asking questions from an instructor), but could be added later.
Using \( and \) instead of $ $ could be a good alternative.
@uhoreg Just have a LaTeX button next to the markdown button for enabling $ $. I'm not a fan of ( ).
The standard "put LaTeX inside delimiters" would certainly work as a first cut. I'd be a bit cautious of just using plain $, as there is a chance of accidentally triggering math when it isn't intended, but there are ways to reduce that (e.g. only triggering if there are no spaces at the beginning/end of the math). A GUI, even a simple one, would be helpful for some users, as not all people who want to enter math are as experienced with LaTeX (e.g. student asking questions from an instructor), but could be added later.
Yeah, adding this in later seems like a good idea. I think it would be good to just get some sort of basic support first, because I really think it would attract quite a few people to Riot.
Edit: Having reread this thread, I just wanted to contribute towards the MathML vs LaTeX encoding debate: I think it would be lovely to extend Matrix HTML support to cover Presentation MathML, but it seems like the idea of just wrapping the LaTeX-format input and sending this, and just displaying this as a fallback, seems like a perfectly workable idea. Every mathematician that I know already just types things like $\mathcal{C}(a,b)\xrightarrow{\sim}Y$ in emails/whatsapp messages, so this would be an acceptable fallback. This also means that (as far as I can tell) the choice of KaTeX vs. MathJax would be rendered almost entirely arbitrary by just letting the user pick between the two.
As another suggestion, would it be possible to realise a solution via an integration? That is, find some safe server-side way of compiling LaTeX (of which there seem to be many) and just provide an integration with such a service?
Yes, it should be pretty easy to write a bot that responds to, say, !math x^2+1
with an image of the math. You could write a service that uses MathJax/KaTeX and something like phantomjs to create the image. It wouldn't be ideal, but it could be a quick way to get math support, and would probably be enough for a lot of people.
Yes, it should be pretty easy to write a bot that responds to, say,
!math x^2+1
with an image of the math. You could write a service that uses MathJax/KaTeX and something like phantomjs to create the image. It wouldn't be ideal, but it could be a quick way to get math support, and would probably be enough for a lot of people.
ok, I'd be happy to look into this. are there any resources on writing bots/integrations for riot? I agree that it's not ideal, but I really think it would suffice for most uses.
Ideally we should have some support in at least Riot itself but a bot can certainly fill in the gaps till we sort that out.
are there any resources on writing bots/integrations for riot?
There are bot frameworks for different languages. https://github.com/matrix-org/go-neb is the one that powers all the other bots in Riot's default "Manage integrations" button. For JavaScript, https://matrix.org/docs/guides/usage-of-matrix-js-bot-sdk may be a good choice. Other libraries you can use are listed at https://matrix.org/docs/projects/sdks
So I wrote a quick bot (https://github.com/thosgood/matrix-maths-bot) that can reply to !latex ...
with a PNG of the maths rendered, but it's not entirely satisfying — the main thing that people [weasel words] want is the ability to have _inline_ maths, i.e. rendered maths in the middle of a sentence.
I've been thinking about matrix-doc#1722 and really believe that the second suggestion (the so-called LaTeX method with <mx-math>
tags) is ideal, and rendering should be done with KaTeX. The point, though, is (as previously mentioned) that the choice of KaTeX vs. MathJax can be entirely client-side, once a Matrix method format is decided upon. If anything, I really think that just sending something even less than the suggested spec would suffice, such as
~
{
"content": {
"body": "This is an equation: $\sin(x)=\frac{a}{b}$",
"format": "org.matrix.custom.html",
"formatted_body": "This is an equation:
"msgtype": "m.text"
},
"event_id": "$eventid:example.com",
"origin_server_ts": 1234567890
"sender": "@alice:example.com",
"type": "m.room.message",
"room_id": "!soomeroom:example.com"
}
~
since this would be easy to render in a client — from what I can tell, it would really just be a case of (for example) adding a KaTeX dependency and the relevant CSS file (there is even react-katex.)
Edit: this is now a pull request (https://github.com/matrix-org/matrix-react-sdk/issues/3251).
Inspired by @hgustafsson 's proof-of-concept, below is a fork which has functional maths integration:
The idea is that “math mode” can be toggled on and off in the message composer, just like for Markdown. When on, it just makes the message send with format org.matrix.custom.html
. There is then a global/room setting for KaTeX rendering, which renders the mathematical content, with nice error support and all these sorts of things that KaTeX does so well.
When the Matrix proposal for how to send mathematics is finalised, it could easily be implemented simply by changing the format that the math mode sends messages as (e.g. org.matrix.custom.math
or something); similarly, the KaTeX rendering could be replaced with another method if desired.
Unlike some of the previous proposals, I opted, in the case of KaTeX rendering being disabled, to simply send the source code (e.g. $xy=z$
), because this is how “most” people are used to seeing such things anyway. Again, if there were some other nice fallback method, then I think it wouldn’t be too hard to implement.
The only problem that I have with this fork is that I can't seem to get the per-room/per-room-account setting to work: I just don't understand the settings model well enough. So if anybody has any suggestions on how to fix this (or, indeed, any other comments at all) then please do let me know.
https://github.com/matrix-org/matrix-doc/pull/2191 is a counter-proposal to https://github.com/matrix-org/matrix-doc/pull/1722 that uses LaTeX instead of MathML.
lots of experimentation going on currently on this between @thosgood, @Cadair, myself with input from @uhoreg. Approaches are split between experiments with render-at-receiver-client (https://github.com/matrix-org/matrix-react-sdk/pull/3251), render-at-sender-client, and render-at-sender-mediarepo (or combinations of all 3).
Before I lose it, https://jsfiddle.net/8Logkhr5/35/ is a POC for render-at-sender-client via MathJax.
Sorry to bump this but I was wondering what the status of this feature is at and when we can expect it to land on develop?
From the user perspective, render-at-sender-client
will fill the media history of a room, and requires much more space server-side. I find KaTeX (or other equivalent) fast enough to be used on large rooms, so the render-at-receiver-client
approach seems better.
I would really like to see this feature in develop in the following weeks (my university is using Matrix to chat during lessons). How can I help ? May I rebase https://github.com/matrix-org/matrix-react-sdk/pull/3251 in matrix-react-sdk:develop
as a new pull-request ?
Another way to render LaTeX is Matrix would be to have a bot uploading the rendered LaTeX but I find this approach non user-friendly.
I've played around with a bot that responds to messages with the rendered image, but it really wasn't very pleasing to use, as you say. (There was another bot a while back that also did this (much better than mine), but I think the general consensus was that it was not really the best solution to this specific need).
If you wish to advance this project a bit more, then I suggest dropping by #pigeon.digital:matrix.org
, where we've been having discussions about this pull request (and the issue in general). I really want to keep working on this, but sadly have a bunch of deadlines in these coming weeks that take priority. I'm still more than happy to discuss what we've done so far/ideas we had heading forwards though!
We must decide on a protocol first - https://github.com/vector-im/riot-web/issues/1945#issuecomment-511599822 seems to be nice
I agree on the fallback issue - https://github.com/matrix-org/matrix-doc/pull/1722#issuecomment-442495508 - as for security, we may initially limit their usage to those having proper safeguards, and later roll in extras
https://github.com/vector-im/riot-web/issues/1945#issuecomment-438448586
We can ask for permission to render the math henceforth if it is encountered in the message (and then add a toggle in the quick settings for those inclined toward math but also give them speed whenever needed; we may keep the toggle chat based...)
@uhoreg I want to participate in GSoC and would love to contribute towards this issue
Hi,
I did not know about this Issue here and created a Firefox extension to inject KaTeX into riot.
https://addons.mozilla.org/en-US/firefox/addon/riot-katex/ (Beware, it is quite a hack job :P)
This is of little relevance for an actual implementation in riot but may be of interest for some people who don't want to wait.
Cheers,
Valentin
I've mentioned this a bunch of times in the other pull request, but have apparently forgotten to do so here: this test build is actually up and running at https://pigeon.digital , and I think it's currently being used quite a bit, and I haven't had any error complaints so far! Please feel free to use it, but just note that you have to enable KaTeX rendering in your user settings, and make sure that the 'maths' toggle is turned on in the composer. (There are some per-room settings, but these don't do anything).
Great!
wouldn't it be good to make a bot that creates png's to involve bridge users and users with other clients?
found this for telegram: https://github.com/vdrhtc/InLaTeXbot
Great! I tried it out on https://pigeon.digital and it worked well, though I was unable to edit a post while keeping the katex formatting. Specifically,
@thosgood Any progress on this? pigeon.digital seems down to me.
@Esokrates For me, this room works. This is where issues are discussed. The good news is that they are close to it!
The current PR to watch seems to be https://github.com/matrix-org/matrix-react-sdk/pull/5244
Most helpful comment
It could be worthwhile looking into Khan Academy's KaTeX as a speedy, lightweight alternative to MathJax.