-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Support Katex #651
Comments
Another benefit of KaTeX is that it also allows pre-rendering, which can further improve speeds. |
This would be super cool. My mathjax page is getting super heavy with notation and takes forever to reflow! It's also a painful experience when hot-reloading, because the reliance on reflow means that the scrollbar loses its place. Pre-rendering would be much appreciated! |
It sounds like using Katex could make the user experience nicer for people with loads of equations, so I'm all for the idea. Adding Katex support would probably require somebody to champion the feature seeing as it's touching a non-trivial amount of the code base. The code itself shouldn't be too much, but it requires tweaking the renderer and/or creating a pre-processor to update the source document with appropriate annotations. I'm also happy to help mentor and or point people in the right direction 😁 |
How might Katex be included/distributed with the mdbook binary? Seeing as it is a JS thingy? |
I'm not 100% sure. If it's a JS thing and used purely in the browser (i.e. after a book is rendered as HTML) then it should, in theory, be no different from JQuery or highlight.js. I know @sorin-davidoi and @mattico have done some work with our JS/CSS stuff in the past, so they may be able to elaborate more. |
It's run as a preprocessing step, so would probably require the user had node installed. |
It can be done by the autorenderer, but yeah, it works best when used in advance for speed. Doing it fully in the browser, though, would be an improvement on MathJax anyway. It'd be reasonable to start there. |
Is a node module https://katex.org/docs/node.html |
is there anything I can work on to make this a reality? |
Well, it is indeed a JS thingy. Just like MathJax it is included via CDN. May be a preprecessor is better for this, see example mermaid integration. |
I am willing to help with this, can I get some pointers? |
Unfortunately there is no easy way to inject HTML into the default template, but using a preprocesor worked for me. So far this solution has no problems with using
[preprocessor.katex]
command = "python3 katex.py"
renderer = ["html"]
#!/usr/bin/env python3
import json
import sys
if len(sys.argv) > 1:
if sys.argv[1] == 'supports':
# sys.argv[2] is the renderer name
sys.exit(0)
katex = '\n' + open('./katex-header.html', 'r').read()
context, book = json.load(sys.stdin)
def fix(items):
for section in items:
if 'Chapter' in section:
section['Chapter']['content'] += katex
fix(section['Chapter']['sub_items'])
fix(book['sections'])
json.dump(book, sys.stdout)
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.11.1/dist/katex.min.css"
integrity="sha384-zB1R0rpPzHqg7Kpt0Aljp8JPLqbXI3bhnPWROx27a9N0Ll6ZP/+DiW/UqRcLbRjq"
crossorigin="anonymous">
<script defer src="https://cdn.jsdelivr.net/npm/katex@0.11.1/dist/katex.min.js"
integrity="sha384-y23I5Q6l+B6vatafAwxRu/0oK/79VlbSz7Q9aiSZUvyWYIYsd+qj+o24G5ZU2zJz"
crossorigin="anonymous"></script>
<script defer src="https://cdn.jsdelivr.net/npm/katex@0.11.1/dist/contrib/auto-render.min.js"
integrity="sha384-kWPLUVMOks5AQFrykwIup5lo0m3iMkkHrD0uJ4H5cjeGihAutqP0yW0J6dpFiVkI"
crossorigin="anonymous"></script>
<script>
document.addEventListener("DOMContentLoaded", function () {
renderMathInElement(document.body, {
delimiters: [
{ left: "$$", right: "$$", display: true },
{ left: "\\(", right: "\\)", display: false },
{ left: "$", right: "$", display: false },
{ left: "\\[", right: "\\]", display: true }
]
});
});
</script> |
I have what I think is a nice approach implemented in https://github.com/tavianator/tavianator.com Inline LaTeX is delimited by
Using code regions for this is nice because you don't have to duplicate any backslashes. A custom head.hbs is enough to render everything client side. I'm working on a custom preprocessor to pre-render everything server side. The main problem right now is it's super slow to shell out to Here's what it looks like: https://tavianator.com/2014/ellipsoid_bounding_boxes.html |
I implemented a Rust pre-processor to render Katex equations at compile-time, you can find the project here. UsageInstall the crate
Add the preprocessor to your [preprocessor.katex] Use
Works fine for me, however I'd love to get some feedback! Edit: Macros are also supported, see the project page for details. |
@lzanini Just skimmed the code but is there a way to escape a $ so it shows up literally? |
@tavianator Not right now, but it shouldn't be too hard to ignore a Edit: Done :) |
Hi, I get an error trying to install katex |
|
@lzanini's Katex preprocessor seems to be idle (No stable release for a year nor activity in issues/ pull requests by owner). Does anyone want to help? |
@Edgehere, I have recently looked at the problem you encountered. Currently, I don't have a good solution and have compromised the Windows build. You can get an uncompromised |
This issue should be gone because of lzanini/mdbook-katex#67. |
It would be good to support Katex as an alternative to MathJax. It's much faster, especially on mobile.
Comparison of speed and rendering differences here.
The text was updated successfully, but these errors were encountered: