rust-lang / mdBook

Create book from markdown files. Like Gitbook but implemented in Rust
https://rust-lang.github.io/mdBook/
Mozilla Public License 2.0
18.36k stars 1.65k forks source link

Improve MathJax support by enabling $$ for math equations #400

Open azerupi opened 7 years ago

azerupi commented 7 years ago

Currently, Mathjax is a little akward to use because we only support the \( ... \) and \[ ... \]method to indicate inline and block equations respectively. On top of that, backslashes need to be escaped so that the markdown parser doesn't remove them. Thus, equations need to be written like:

\\[ \mu = \frac{1}{N} \sum_{i=0} x_i \\]

So when mathjax support is enabled in the configuration, it would be great to add a preprocessor that rewrites the \[ ... \] forms to \\[ ... \\] and add support for the $$ ... $$ math delimiters.

subthedubdub commented 7 years ago

Couldn't this be implemented entirely in JavaScript (see the MathJax Docs)?

azerupi commented 7 years ago

Ah thanks for reminding me, I forgot the most important part of this issue!

Currently, math expressions will collide with markdown syntax. If you write a*b*c=d the markdown parser will emphasise b, which is not what we want.

So to make mathjax fully usable, we need to preprocess the equations and replace it with something that will be skipped by the markdown parser. The solution in my first post will therefore not work.

What I propose is to wrap the math equations with html tags. For inline <span class="inline-math">...</span> and for blocks <div class="math"></div>. The markdown parser will recognise this as raw HTML and will leave it as is.

dvberkel commented 6 years ago

I would like to look into this. Will start a branch and send in a WIP pull request

brendanzab commented 6 years ago

I'm wondering if it would be better to switch to using code-fences for embedded markup languages, as suggested in this thread on the CommonMark forums, eg:

```{latex}
\begin{tikzpicture}
\node[inner sep=0pt] (russell) at (0,0)
    {\includegraphics[width=.25\textwidth]{bertrand_russell.jpg}};
\node[inner sep=0pt] (whitehead) at (5,-6)
    {\includegraphics[width=.25\textwidth]{alfred_north_whitehead.jpg}};
\draw[<->,thick] (russell.south east) -- (whitehead.north west)
    node[midway,fill=white] {Principia Mathematica};
\end{tikzpicture}


This would also have the advantage of making the [github previews](https://github.com/brendanzab/pikelet/blob/23e9cb1a5b9b49810573f777f86b63b27adcdf83/book/src/appendix/theory.md#raw-terms) look much nicer. We might have to wait for this to go through the pipeline of standardization though. :/
ghost commented 5 years ago

I'll naively plug tectonic. It is a full LaTeX engine partially written in rust that could potentially integrate with mdBook for advanced formatting needs.

vadixidav commented 4 years ago

Does anyone happen to be working on this right now? If not, the Rust ML WG is interested in picking this task up.

ehuss commented 4 years ago

@vadixidav I doubt anyone is looking at it. Feel free to take a look, though I'm not up to speed with what the problems are (I think maybe mathjax needs to be a preprocessor?).

ghost commented 4 years ago

I am not @vadixidav.

jeanm commented 4 years ago

I'm not working on this, but I'll leave two pointers here which are hopefully useful to people who are:

bytesnake commented 4 years ago

Hey, I created recently a small plugin for scientific mdbook. This adds support for latex, gnuplot, and bibtex rendering. Equations are rendered to SVG without KaTeX or MathJax, but I'm planing to support them later on. I also added support for referencing/citations across the book. A sample can be found here and the github repository here.

Yang-Xijie commented 2 years ago

This is exactly a great feature to be considered.

pesho-ivanov commented 1 year ago

Github seems to have no problem of using MathJax over Markdown using the obvious Latex syntax $...$ and $$...$$. What is the reason this is not the case for mdBook and can't the same approach as for Github be used?

ehuss commented 1 year ago

I think this is overall blocked on https://github.com/raphlinus/pulldown-cmark/pull/622.

expikr commented 1 year ago

mdbook-katex is now quite feature-complete, capable of producing completely static outputs. It is also capable of rendering inside <detail></detail> blocks with no issue (@your-diary 's #1926), and uses the $...$ and $$...$$ delimiter by default and can add custom delimiters.

See https://github.com/SichangHe/mdbook_katex_static_css/

Any reason why the CDN-delivered MathJax must have the exclusive privilege of being the "default"?

0xPhaze commented 1 year ago

Since I got lost in this and a few other open issues: https://github.com/lzanini/mdbook-katex works well.

sanmai-NL commented 1 year ago

@ehuss Would you consdier, also from a maintainability viewpoint, to drop MathJax and recommend users to use an extension such as mdbook-katex?