简体   繁体   English

KaTeX 与 bookdown + gitbook

[英]KaTeX with bookdown + gitbook

I am building a bookdown project and rendering it as a gitbook with numerous pages of math and it is rendering sluggishly.我正在构建一个 bookdown 项目,并将其呈现为一个包含大量数学页面的 gitbook,并且呈现缓慢。 I would like to use KaTeX instead of mathJax to render my math but I'm not sure how to get it working.我想使用KaTeX而不是 mathJax 来呈现我的数学,但我不确定如何让它工作。 There is a gitbook plugin so it should be possible but I don't quite know how to integrate it with bookdown.有一个gitbook 插件,所以它应该是可能的,但我不太知道如何将它与 bookdown 集成。

In my index.Rmd file i've tried the following:在我的index.Rmd文件中,我尝试了以下操作:

---
site: bookdown::bookdown_site
output:
  bookdown::gitbook:
    pandoc_args: [--katex]
    mathjax: NULL
    includes:
      in_header: katex.html
documentclass: book
---

where katex.html consists of the stylesheet and theme for KaTeX.其中katex.html包含katex.html的样式表和主题。

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.7.1/katex.min.css" integrity="sha384-wITovz90syo1dJWVh32uuETPVEtGigN07tkttEqPv+uR2SE/mbQcG7ATL28aI9H0" crossorigin="anonymous">
<script src="https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.7.1/katex.min.js" integrity="sha384-/y1Nn9+QQAipbNQWU65krzJralCnuOasHncUFXGkdwntGeSvQicrYkiUBwsgUqc1" crossorigin="anonymous"></script>

However, the math is not rendered (save for a few parts that are still rendered by MathJax).但是,数学不会被渲染(除了一些仍然由 MathJax 渲染的部分)。

在此处输入图片说明

Is there any way that I get get bookdown to work with KaTeX?有什么办法可以让我预订与 KaTeX 一起工作吗?

It seems you didn't read the KaTeX documentation.您似乎没有阅读 KaTeX 文档。 KaTeX does not automatically render your math expressions. KaTeX 不会自动渲染你的数学表达式。 See the section Automatic rendering of math on a page in its README on Github.请参阅 Github 上自述文件中的页面上数学的自动呈现部分。 In short, you have to load auto-render.min.js and add an event to render the math, eg in your katex.html , you need:简而言之,您必须加载auto-render.min.js并添加一个事件来呈现数学,例如在您的katex.html ,您需要:

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.7.1/katex.min.css" integrity="sha384-wITovz90syo1dJWVh32uuETPVEtGigN07tkttEqPv+uR2SE/mbQcG7ATL28aI9H0" crossorigin="anonymous">
<script src="https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.7.1/katex.min.js" integrity="sha384-/y1Nn9+QQAipbNQWU65krzJralCnuOasHncUFXGkdwntGeSvQicrYkiUBwsgUqc1" crossorigin="anonymous"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.7.1/contrib/auto-render.min.js" integrity="sha384-dq1/gEHSxPZQ7DdrM82ID4YVol9BYyU7GbWlIwnwyPzotpoc57wDw/guX8EaYGPx" crossorigin="anonymous"></script>
<script>
  document.addEventListener("DOMContentLoaded", function() {
    renderMathInElement(document.body);
  });
</script>

To disable MathJax in bookdown gitbook output, you need to set math: false in YAML, eg要在 bookdown gitbook 输出中禁用 MathJax,您需要在 YAML 中设置math: false ,例如

---
site: bookdown::bookdown_site
output:
  bookdown::gitbook:
    pandoc_args: [--katex]
    mathjax: NULL
    includes:
      in_header: katex.html
documentclass: book
math: false
---

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM