# Support for KaTeX #292

Closed
opened this Issue Oct 20, 2015 · 15 comments

Projects
None yet
6 participants

### adius commented Oct 20, 2015

 Kramdown should have a math engine for KaTeX!

Owner

### gettalong commented Oct 20, 2015

 Pull-requests are welcome!

### jkm commented Jun 18, 2016

 What do you prefer a nodejs solution similar to the MathjaxNode engine or a solution based on execjs? In the former case the user needs to keep katex up to date via npm whereas in the latter case one needs to download the katex.min.js and possibly add checking if an updated version becomes available.
Owner

### gettalong commented Jun 18, 2016

 If I understand correctly KaTeX is in the same league as MathJax, so kramdown would just need to emit the correct HTML fragments for KaTeX - or am I wrong?

### jkm commented Jun 18, 2016

 Yes. I think that's the case. katex will generate the HTML for given LaTeX-like input.
Owner

### gettalong commented Jun 19, 2016

 Okay, then there is no need for NodeJS or execjs or anything which is good.

### jkm commented Jun 19, 2016

 Not sure I get it. I assume katex is installed. Then executing $node ka.js  results in  c = ± a 2 + b 2 c = \pm\sqrt{a^2 + b^2}  where ka.js is var katex = require('katex'); var html = katex.renderToString("c = \\pm\\sqrt{a^2 + b^2}"); console.log(html); Owner ### gettalong commented Jun 19, 2016  If I understand the KaTeX website correctly, you could do server side rendering but better for kramdown is client side rendering. So no need for any Javascript interpreter whatsoever. ### jkm commented Jun 19, 2016  True. You can run it client-side using javascript or render it once statically. I prefer the second mode. Because my input is static. So I won't need javascript on the client. But what came to my mind is that the statically rendered version is much larger than its input. So maybe I should try the dynamic version first. ### jkm commented Jun 27, 2016  So how do I add client side rendering? Do you have an example on how to use KaTeX inside kramdown? Owner ### gettalong commented Jun 27, 2016  This should probably help but note that I'm not familiar with KaTeX, I was just googling... ### dato commented Nov 2, 2016  TL;DR: I don’t think Kramdown should to anything (for client-side rendering). With some glue, KaTeX already works. See instructions and a longer explanation below. IMHO this pull request should be closed. (I use KaTeX myself.) As I understand it, Kramdown parses as math text wrapped in $$x$$, and outputs it verbatim inside a  If you use templating, you could wrap the above with {% if page.math %} (and set that variable in pages that need it) In summary: I believe the customization belongs in the serving step rather than the HTML generation step. MathJax and KaTeX can be exchanged freely, without Kramdown’s involvement. (Side note: a nice thing would be if Kramdown could accept single-dollar escape for math, e.g. $ x $. It would make it compatible with Pandoc.) ### dato commented Nov 2, 2016  Ah! Now that I see #342: if you don’t think you’ll swap with MathJax you could use --math-engine=nil, and use the auto-renderer. (Though I believe it’s safer to have the fallback, in case there’s a tricky input KaTeX can’t render.) Owner ### gettalong commented Nov 9, 2016  @dato Thanks for the information. I have updated the documentation on the MathJax page to include information about using KaTeX. Thanks for the script you provided. ### gettalong closed this Nov 9, 2016 ### gettalong added a commit that referenced this issue Nov 13, 2016  Add information about how to use KaTeX with the MathJax math engine  The information and script were provided in issue #292.  e9fa6dc  ### meygerjos referenced this issue Apr 26, 2017 Closed #### Add support for % comments #20 ### meygerjos commented Apr 26, 2017  @dato This problem occurs with both inline math and display math, so your code should apply the replace statement to both selectors. Something like this:   (I'm not sure if the jQuery syntax is right.) ### gaberber referenced this issue Jun 16, 2017 Closed #### kramdown and KaTeX conflict #47 ### chnn commented Jul 6, 2017  For those not using jQuery, here's a version of @dato's script that should work in IE 9+: document.querySelectorAll("script[type='math/tex']").forEach(function(el) { el.outerHTML = katex.renderToString(el.textContent, { displayMode: false }); }); document.querySelectorAll("script[type='math/tex; mode=display']").forEach(function(el) { el.outerHTML = katex.renderToString(el.textContent.replace(/%.*/g, ''), { displayMode: true }); }); ### jluttine referenced this issue Mar 18, 2018 Closed #### Display formulas with $$not working #1 ### zYeoman added a commit to zYeoman/3-Jekyll that referenced this issue Jul 17, 2018  🚑 Fix katex bug  方案来自 gettalong/kramdown#292 另，https://github.com/gettalong/kramdown/issues/342有对katex的支持方案 echo "$$x$$" | kramdown --math-engine nil -> <div class="kdmath">$$x$$</div> echo "t$$x$$t" | kramdown --math-engine nil -> <p>t<span class="kdmath">$x\$</span>t</p>
 2da2e35