Pandoc:从Markdown输入生成嵌入Latex方程的HTML。

4

我可以帮您将一个包含LaTeX公式的Markdown文件转换为Html文件,使用的工具是pandoc。

在文件test.md中,我尝试使用以下语法:

$$ \frac{1}{2} = 0.5 \neq \sqrt{2} $$

我使用以下命令调用pandoc:

pandoc test.md -o test.html --mathjax

根据这个答案所示,生成的test.html文件中只包含一行代码。

<p><span class="math">\[ \frac{1}{2} = 0.5 \neq \sqrt{2} \]</span></p>

当用网页浏览器打开test.html时,屏幕上的输出文字就是字面意思。

\[ \frac{1}{2} = 0.5 \neq \sqrt{2} \]

而不是一个漂亮的“latex编译”公式。

我错过了什么?

附言:我正在使用pandoc 1.12.2.1。

1个回答

5
生成的 test.html 文件并不完整,只生成了 html 的 body 部分而没有 header。然而,为了让公式显示得更好,必须在 header 中链接 mathjax。
要生成一个包含 <html><head><body> 标签的完整 html 文件,需要使用 pandoc 的 --standalone(又称为 -s)选项。
pandoc --standalone test.md -o test.html --mathjax

更多细节

使用调用方式

pandoc --standalone test.md -o test.html --mathjax

生成以下test.html文件。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta http-equiv="Content-Style-Type" content="text/css" />
    <meta name="generator" content="pandoc" />
    <title></title>
    <style type="text/css">code{white-space: pre;}</style>
    <script src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML" type="text/javascript"></script>
</head>
<body>
<p><span class="math">\[ \frac{1}{2} = 0.5 \neq \sqrt{2} \]</span></p>
</body>
</html>

请注意在<head>部分链接到mathjax的<script>标签, 而调用方式则是

pandoc test.md -o test.html --mathjax

生成一个仅包含单行文本的文件。
<p><span class="math">\[ \frac{1}{2} = 0.5 \neq \sqrt{2} \]</span></p>

2
来自未来的提示:cdn.mathjax.org 即将到达生命周期的尽头,请查看 https://www.mathjax.org/cdn-shutting-down/ 了解迁移技巧。 - Peter Krautzberger

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接