Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Math support #36

Closed
SamyPesse opened this issue Apr 6, 2014 · 14 comments
Closed

Math support #36

SamyPesse opened this issue Apr 6, 2014 · 14 comments

Comments

@SamyPesse
Copy link
Member

I think that it would be really cool to support MathJAX for writing math formulas.

@AaronO AaronO changed the title Math supports Math support Apr 6, 2014
@ramnathv
Copy link

ramnathv commented Apr 8, 2014

👍 I would really appreciate if this feature could be added. For the interim, is there a simple workaround, other than copy pasting the required mathjax code on every page?

@pkra
Copy link

pkra commented Apr 9, 2014

Peter from the MathJax team here. Feel free to ping me know if we can help with any questions.

@SamyPesse
Copy link
Member Author

Hi @pkra, is there any way we can do the HTML generation in node.js so that we don't depend on client-side operation?

@pkra
Copy link

pkra commented Apr 9, 2014

@SamyPesse that's a complex question. The answer is probably "no" but it really depends on what you're after.

MathJax is highly modular both on input and output format (and internally, actually). On the input side, MathML, TeX and asciimath syntax are supported. (The issue is called "math support" but it's not clear if multiple input options are needed). On the output side, MathJax can generate HTML/CSS, SVG or MathML rendering (the internal format is basically MathML).

The HTML/CSS and SVG output depend strongly on the client (e.g. viewport size for linebreaking, CSS inheritance). The HTML/CSS output is too unstable to generate ahead of time (there are too many hacks we have to do to work around browser bugs and limitations). The SVG output can be generated but will also loose functionality. Generating MathML is best IMHO, but it means you'll still want to run MathJax on clients (purely as a MathML polyfill; and if you're happy with Firefox's and Safari's MathML support and rendering quality you won't need it there).

The bad news is that MathJax currently won't run in a pure nodejs environment (basically because 5 years ago that wasn't important). We're planning to remedy that in the near future (this quarter) so that at least MathML and SVG generation can work in nodejs. In the mean time you could try out mathoid which uses phantomjs as a stop gap.

@ramnathv
Copy link

ramnathv commented Apr 9, 2014

My quick hack to make it work was to save this snippet as mathjax.html in the includes folder and including it in the page template. It works great.

<!-- MathJax: Fall back to local if CDN offline but local image fonts are not supported (saves >100MB) -->
<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    tex2jax: {
      inlineMath: [['$','$'], ['\\(','\\)']],
      processEscapes: true
    }
  });
</script>
<script type="text/javascript" src="http://cdn.mathjax.org/mathjax/2.0-latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>

@pkra
Copy link

pkra commented Apr 9, 2014

Which markdown converter is used in gitbook? Clashes with TeX syntax are inevitable but some are better than others. stackedit might be a good example for how to deal with this.

@SamyPesse
Copy link
Member Author

GitBook uses https://github.com/chjj/marked. I'll take a look at all of this.

@jbryer
Copy link

jbryer commented Apr 15, 2014

@ramnathv How do you include the mathjax.html file in the pages template? I don't see where that is. Thanks

@jbryer
Copy link

jbryer commented Apr 16, 2014

I figured it out. I needed to create my own copy of the theme directory and modify the layout.html (probably works in page.html too). Here is the command to include: {% include "includes/mathjax.html" %}

One issue however, underscores seem to be converted to tags. Escaping them with a backslash (i.e. _) takes care of this. I have used other markdown tools and have not had this problem.

@SamyPesse
Copy link
Member Author

@pkra Can you help me for something?

I'm working on the new plugin architecture and I built a MathJAX plugin that will be integrated as default: https://github.com/GitbookIO/plugin-mathjax

But I need to re-run the processing of the page when "page.change" event happened: https://github.com/GitbookIO/plugin-mathjax/blob/master/book/plugin.js#L11

I didn't find in the documentation how to do that.

@ramnathv
Copy link

I think you need to use Mathjax.Hub.Rerender. See the discussion here

@pkra
Copy link

pkra commented Apr 21, 2014

@SamyPesse basically, what @ramnathv said; see http://docs.mathjax.org/en/latest/typeset.html.

(I don't know enough about the internals of gitbook to suggest optimizations. E.g. it might be worth restricting the Rerender call to the new content to prevent unnecessary processing of e.g. math in the menu; e.g. if you prefetch content, then MathJax could potentially render ahead of the reader accessing the new content. etc)

@ramnathv
Copy link

I agree with @pkra. The page I linked to is contains a sample implementation for revealjs, where Rerender is used to selectively render only the math on the current slide, based on an event that is fired on entering a slide. I haven't see gitbook internals, but I believe, if there is access to the page content that changes on entry to a page, then passing that to Rerender should do the trick.

@pkra
Copy link

pkra commented Jun 15, 2014

An early version of the MathJax-node tool I mentioned earlier is now public at https://github.com/mathjax/mathjax-node. Pre-processing MathJax input into MathML (or SVG) would solve other issues such as #295.

Nyar233 pushed a commit to Nyar233/gitbook that referenced this issue May 12, 2024
* Log cache performances

* Handle concurrent requests

* Debug log

* Remove debug log

* Comment
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants