atom / markdown-preview

📝 Markdown preview in Atom
MIT License
1.23k stars 358 forks source link

Add math support to MarkDown preview #116

Open szhorvat opened 10 years ago

szhorvat commented 10 years ago

This is a request to add math (LaTeX) support to the MarkDown preview.


Suggestion on how it should work:

There should be a setting to enable math support. When enabled, $...$ is for inline math, $$...$$ is for display math, and \$ is for an actual dollar sign. This is identical to how it works on StackExchange sites that have math support.

Inline math support is essential to be able to use MarkDown for taking math/research notes (i.e. adding only display math support would make this much less useful).


I am not aware of any desktop application MarkDown editors that have math support like this. (There are several web applications that do this, but all the desktop applications I tried have some limitation such as only supporting display math or being buggy.)


Note that it's not sufficient to simply add MathJax to the header of the output HTML. $...$ pairs should be parsed during conversions and MarkDown syntax should not be interpreted between these delimiters. Otherwise e.g. $x_1,dots,x_2$ won't work: the two underscores must be be interpreted as emphasis by the MD converter.

szhorvat commented 9 years ago

There's a small bug with typing \$ in code blocks or inline code: the \ doesn't render. This is with version 0.2.0.

For example, try this MarkDown:

\$

It will render as $, not as \$.

szhorvat commented 9 years ago

Another way to trigger a problem is this:

screenshot 2014-11-24 15 45 53

Here's the MarkDown for copying:

start

$fgf


\$ asd
$x$
Galadirith commented 9 years ago

@szhorvat So in the current version $ are still treated as math delimiters, even in code blocks and inline. Of course thats not the expected behaviour, so I'll get to work on fixing that :D. Until I push out a fix a workaround is to use \\$ inside code blocks and inline, which will be rendered as \$.

Also feel free to use markdown-preview-plus for issues related to it specifically. Thanks :D

gjtorikian commented 9 years ago

For a fast and reliable method of turning TeX math into SVG/PNG/MathML, I've just released https://github.com/gjtorikian/mathematical-node, which is a port of my own Ruby gem, Mathematical.

This does not use MathJax, KaTeX, or any client-side JavaScript, because I think that's a bit silly. Instead, it's a Node.js binding to two C libraries: mtex2mml, which converts the math to MathML, and Lasem, which converts the MathML to SVG or PNG (the default is SVG).

mtex2mml has about a 67% compliance with MathJax (meaning it passes 67% of their tests). I'll likely spend the next few weeks getting that higher. Most of the missing TeX functionality includes changes to font size and macro support.

Try it out, let me know if you find it useful.

thedaniel commented 9 years ago

@Galadirith did you ever take a look at @gjtorikian's library? It seems like a pretty promising way to get fast math rendering into this package.

Galadirith commented 9 years ago

@thedaniel I did. @gjtorikian library looks amazing and it would be great to incorporate it, however the problem I have is I have not been able to get it running on my windows machine because of the required dependencies.

I'm sure it can be done, but even if it can, without any mature package manager on windows like apt-get I personally don't think that's something I would want to ask anyone else to do.

That said, I do honestly think MathJax is a great option and shouldn't be written off. @gjtorikian what are the main reasons you don't think MathJax is suitable?

gjtorikian commented 9 years ago

MathJax 2.5.0 now has a Node.js component, so that may be a viable option in this case. However, I found client-side MathJax to be slow, and the effect of loading images jittery. Much of the codebase also operates on a bunch of regular expressions, which I also don't think is the right way to parse and tokenize TeX items. Also, a package with 45 MB of dependencies is a very large package.

50Wliu commented 9 years ago

What about KaTeX?

szhorvat commented 9 years ago

@50Wliu The package originally used KaTeX. However, KaTeX is currently very very limited, and can't render many basic constructs. Personally, I wouldn't be able to live with its limitations. There are several posts in this thread about this, see above.

Actually this is my main concern about switching to other implementations. MathJax is a mature package that was made with the needs of mathematicians in mind. The alternatives tend to be much more limited and unsuitable for "serious" math.

Here's a summary of the problems: http://meta.math.stackexchange.com/a/16817/12384

Galadirith commented 9 years ago

@gjtorikian thanks so much for your response.

I found client-side MathJax to be slow, and the effect of loading images jittery

I definitely agree but that can be mitigated if you only update what has been modified in your source document. markmon is the best example of this which gives blazingly fast re-rendering for modified files, and it uses mathjax.

did you ever take a look at @gjtorikian's library? It seems like a pretty promising way to get fast math rendering into this package.

@thedaniel As I said it is an awesome library but without a reproducible way to install on windows machines I dont think it is the right option at the moment.

WenjieZ commented 4 years ago

Weigh in to support this issue. My personal use is research note taking.

BTW, Is there any plug-in that renders the INLINE math already?

szhorvat commented 4 years ago

@WenjieZ

BTW, Is there any plug-in that renders the INLINE math already?

https://atom.io/packages/markdown-preview-plus works very well for research note taking.