运行 Jekyll 上的 KaTeX?

Running KaTeX on Jekyll?

有几天我在我的静态 Jekyll 网站上尝试使用 KaTeX 而不是 MathJax。我尝试了很多在互联网上找到的建议但没有成功。最后一个是 jekyll-katex 插件。我遵循了自述文件中的所有步骤。

这是我现在的 Gemfile:

source "https://rubygems.org"

gem "jekyll", "~> 3.8.3"
gem "minima", "~> 2.0"

group :jekyll_plugins do
  gem "jekyll-feed", "~> 0.6"
  gem 'jekyll-katex', "~> 0.2"
end

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem "tzinfo-data", platforms: [:mingw, :mswin, :x64_mingw, :jruby]

# Performance-booster for watching directories on Windows
gem "wdm", "~> 0.1.0" if Gem.win_platform?

这是我当前_config.yml中的相关部分:

markdown: kramdown
theme: minima
plugins:
  - jekyll-feed
  - jekyll-katex

当我 运行 bundle exec jekyll serve 我收到这个错误:

bundler: failed to load command: jekyll (/usr/local/bin/jekyll)
Bundler::GemRequireError: There was an error while trying to load the gem 'jekyll-katex'.
Gem Load Error is: undefined method `[]' for nil:NilClass
Backtrace for gem load error is:
/var/lib/gems/2.5.0/gems/jekyll-katex-0.2.0/lib/jekyll-katex/configuration.rb:22:in `js_path'
/var/lib/gems/2.5.0/gems/jekyll-katex-0.2.0/lib/jekyll/tags/katex.rb:14:in `<class:Katex>'
/var/lib/gems/2.5.0/gems/jekyll-katex-0.2.0/lib/jekyll/tags/katex.rb:12:in `<module:Tags>'
/var/lib/gems/2.5.0/gems/jekyll-katex-0.2.0/lib/jekyll/tags/katex.rb:7:in `<module:Jekyll>'
/var/lib/gems/2.5.0/gems/jekyll-katex-0.2.0/lib/jekyll/tags/katex.rb:6:in `<top (required)>'
/var/lib/gems/2.5.0/gems/jekyll-katex-0.2.0/lib/jekyll-katex.rb:7:in `require'
/var/lib/gems/2.5.0/gems/jekyll-katex-0.2.0/lib/jekyll-katex.rb:7:in `<top (required)>'
/usr/local/lib/site_ruby/2.5.0/bundler/runtime.rb:81:in `require'
/usr/local/lib/site_ruby/2.5.0/bundler/runtime.rb:81:in `block (2 levels) in require'
/usr/local/lib/site_ruby/2.5.0/bundler/runtime.rb:76:in `each'
/usr/local/lib/site_ruby/2.5.0/bundler/runtime.rb:76:in `block in require'
/usr/local/lib/site_ruby/2.5.0/bundler/runtime.rb:65:in `each'
/usr/local/lib/site_ruby/2.5.0/bundler/runtime.rb:65:in `require'
/usr/local/lib/site_ruby/2.5.0/bundler.rb:114:in `require'
/var/lib/gems/2.5.0/gems/jekyll-3.8.3/lib/jekyll/plugin_manager.rb:51:in `require_from_bundler'
/var/lib/gems/2.5.0/gems/jekyll-3.8.3/exe/jekyll:11:in `<top (required)>'
/usr/local/bin/jekyll:23:in `load'
/usr/local/bin/jekyll:23:in `<top (required)>'
/usr/local/lib/site_ruby/2.5.0/bundler/cli/exec.rb:74:in `load'
/usr/local/lib/site_ruby/2.5.0/bundler/cli/exec.rb:74:in `kernel_load'
/usr/local/lib/site_ruby/2.5.0/bundler/cli/exec.rb:28:in `run'
/usr/local/lib/site_ruby/2.5.0/bundler/cli.rb:424:in `exec'
/usr/local/lib/site_ruby/2.5.0/bundler/vendor/thor/lib/thor/command.rb:27:in `run'
/usr/local/lib/site_ruby/2.5.0/bundler/vendor/thor/lib/thor/invocation.rb:126:in `invoke_command'
/usr/local/lib/site_ruby/2.5.0/bundler/vendor/thor/lib/thor.rb:387:in `dispatch'
/usr/local/lib/site_ruby/2.5.0/bundler/cli.rb:27:in `dispatch'
/usr/local/lib/site_ruby/2.5.0/bundler/vendor/thor/lib/thor/base.rb:466:in `start'
/usr/local/lib/site_ruby/2.5.0/bundler/cli.rb:18:in `start'
/usr/lib/ruby/gems/2.5.0/gems/bundler-1.16.2/exe/bundle:30:in `block in <top (required)>'
/usr/local/lib/site_ruby/2.5.0/bundler/friendly_errors.rb:124:in `with_friendly_errors'
/usr/lib/ruby/gems/2.5.0/gems/bundler-1.16.2/exe/bundle:22:in `<top (required)>'
/usr/local/bin/bundle:23:in `load'
/usr/local/bin/bundle:23:in `<main>'
Bundler Error Backtrace:

  /usr/local/lib/site_ruby/2.5.0/bundler/runtime.rb:84:in `rescue in block (2 levels) in require'
  /usr/local/lib/site_ruby/2.5.0/bundler/runtime.rb:80:in `block (2 levels) in require'
  /usr/local/lib/site_ruby/2.5.0/bundler/runtime.rb:76:in `each'
  /usr/local/lib/site_ruby/2.5.0/bundler/runtime.rb:76:in `block in require'
  /usr/local/lib/site_ruby/2.5.0/bundler/runtime.rb:65:in `each'
  /usr/local/lib/site_ruby/2.5.0/bundler/runtime.rb:65:in `require'
  /usr/local/lib/site_ruby/2.5.0/bundler.rb:114:in `require'
  /var/lib/gems/2.5.0/gems/jekyll-3.8.3/lib/jekyll/plugin_manager.rb:51:in `require_from_bundler'
  /var/lib/gems/2.5.0/gems/jekyll-3.8.3/exe/jekyll:11:in `<top (required)>'
  /usr/local/bin/jekyll:23:in `load'
  /usr/local/bin/jekyll:23:in `<top (required)>'

感谢任何帮助。

我做到了!但是我没有使用 jekyll-katex 插件而是 jekyll-katek-block。我只是按照其 readme 中的说明进行操作。基本上唯一需要的步骤是:

  1. katex_block.rb 放入 _plugins 文件夹
  2. katex.min.js 存储在您网站的任何位置。默认是 /public/js/katex.min.js,否则你必须修改你的 _config.yml 添加

    katex:
      path_to_js: "./your/path/to/katex/js"
    
  3. (几乎可选)Link katek.min.css 和字体。

katex.min.jskatex.min.css,字体可以从KaTeX GitHub page.

下载

正如我所说,我的目的是 运行 KaTeX 与 Jekyll 并且此方法有效。但是,如果其他人能够使 jekyll-katex 工作,因为这是最初的请求,我会将其标记为已接受。

关于jekyll-katex插件,我好像遇到了新版本由于新的配置选项导致的bug。我在项目的 github 仓库上发布了一个 issue,它很快就会被修复。在那之前,解决方法(根据开发人员的说法)似乎应该是向 _config.yml:

添加空白配置选项
katex:

备注:等bug解决了我再更新这个答案。

自从 OP 提出这个问题以来已经有一段时间了,但是经过一整天的各种来源的试验和错误,将 KaTeX 集成到我的 Jekyll 静态站点中(有额外的限制,我使用 GitHub 页面),this blog post 中的步骤使它对我有用。我决定在下面复制它,因为它回答了问题,其他用户可能会觉得它有用,而且因为博客不容易找到(它被埋在 jekyll-katex GitHub 的问题部分回购,并且无法通过 Google 搜索访问):

使用 Jekyll 自动渲染 KaTeX,适用于 GitHub 页。

  1. 将以下行添加到您的 _config.yml 文件中:
kramdown:
  math_engine: katex
  1. _includes/head.html 中的 <head> 标签内添加以下代码片段,注意缩进:
  <!--KaTeX-->
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css" integrity="sha384-AfEj0r4/OFrOo5t7NnNe46zW/tFgW6x/bCJG8FqQCEo3+Aro6EYUG4+cU+KJWu/X" crossorigin="anonymous">
  <script defer src="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.js" integrity="sha384-g7c+Jr9ZivxKLnZTDUhnkOnsh30B4H0rpLUpJ4jAIKs4fnJI+sEnkvrMWph2EDg4" crossorigin="anonymous"></script>
  <script defer src="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/contrib/auto-render.min.js" integrity="sha384-mll67QQFJfxn0IYznZYonOWZ644AWYC+Pt2cHqMaRhXVrursRwvLnLaebdGIlYNa" crossorigin="anonymous"></script>
  <script>
      document.addEventListener("DOMContentLoaded", function() {
          renderMathInElement(document.body, {
              // ...options...
          });
      });
  </script>

所以 _includes/head.html 看起来像:

<head>

  ...

  <!--KaTeX-->
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css" integrity="sha384-AfEj0r4/OFrOo5t7NnNe46zW/tFgW6x/bCJG8FqQCEo3+Aro6EYUG4+cU+KJWu/X" crossorigin="anonymous">
  <script defer src="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.js" integrity="sha384-g7c+Jr9ZivxKLnZTDUhnkOnsh30B4H0rpLUpJ4jAIKs4fnJI+sEnkvrMWph2EDg4" crossorigin="anonymous"></script>
  <script defer src="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/contrib/auto-render.min.js" integrity="sha384-mll67QQFJfxn0IYznZYonOWZ644AWYC+Pt2cHqMaRhXVrursRwvLnLaebdGIlYNa" crossorigin="anonymous"></script>
  <script>
      document.addEventListener("DOMContentLoaded", function() {
          renderMathInElement(document.body, {
              // ...options...
          });
      });
  </script>

</head>
  1. 要在 .md 博客 post 中使用 KaTeX,请插入 katex: True。例如:
---
layout: post
title: KaTeX with Jekyll
katex: True
---
inline: $$f(x) = \int_{-\infty}^\infty \hat f(\xi)\,e^{2 \pi i \xi x} \,d\xi$$
display mode (centered):

$$f(x) = \int_{-\infty}^\infty \hat f(\xi)\,e^{2 \pi i \xi x} \,d\xi$$
  1. $$ 标志内的所有内容现在都将显示为 KaTeX。