我问这个问题几乎是愚蠢的,但是即使是最小的例子,我也无法让KaTeX进行工作:
<!DOCTYPE html>
<!-- KaTeX requires the use of the HTML5 doctype. Without it, KaTeX may not render properly -->
<html>
<head>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.10.0-rc.1/dist/katex.min.css" integrity="sha384-D+9gmBxUQogRLqvARvNLmA9hS2x//eK1FhVb9PiU86gmcrBrJAQT8okdJ4LMp2uv" crossorigin="anonymous">
<!-- The loading of KaTeX is deferred to speed up page rendering -->
<script src="https://cdn.jsdelivr.net/npm/katex@0.10.0-rc.1/dist/katex.min.js" integrity="sha384-483A6DwYfKeDa0Q52fJmxFXkcPCFfnXMoXblOkJ4JcA8zATN6Tm78UNL72AKk+0O" crossorigin="anonymous"></script>
<!-- To automatically render math in text elements, include the auto-render extension: -->
<script defer src="https://cdn.jsdelivr.net/npm/katex@0.10.0-rc.1/dist/contrib/auto-render.min.js" integrity="sha384-yACMu8JWxKzSp/C1YV86pzGiQ/l1YUfE8oPuahJQxzehAjEt2GiQuy/BIvl9KyeF" crossorigin="anonymous"
onload="renderMathInElement(document.body);"></script>
</head>
<body>
<p>$x^2 = \sqrt{y}$</p>
<p id="1">Foo $x^2 = \sqrt{y}$ </p>
<script>renderMathInElement(document.getElementById('1'))</script>
</body>
</html>
如果我在Firefox中运行它,我会得到:
此错误也出现在浏览器的控制台中:
我不明白。 CDN是否损坏?
答案 0 :(得分:3)
即使这个问题需要更多的解释,我想您所需要的还是以数学渲染的方式显示公式,对吗?对于我们其他以前从未看过KaTex插件的人。无论如何,您的代码会发生什么,就是您在段落元素中添加了一些文本,因此可以正常显示在您的网页上,例如:
$ x ^ 2 = \ sqrt {y} $
第二行也显示在firefox中,因为它仅位于P元素内,并且由于脚本不起作用,因此仅显示了这两段并显示了控制台错误。
通读此插件,我认为没有诸如renderMathInElement之类的方法,或者至少我没有看到它,但是从我在以下示例中看到的
https://github.com/Khan/KaTeX/
您可以看到通常人们使用katex.function,因此,如果将其用作脚本:
katex.render("YOUR FORMULAS HERE", elementById, {
throwOnError: false
});
然后您会确定要实现的目标,所以以下是完整的代码段,希望对您有所帮助:
<!DOCTYPE html>
<!-- KaTeX requires the use of the HTML5 doctype. Without it, KaTeX may not render properly -->
<html>
<head>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.10.0-rc.1/dist/katex.min.css" integrity="sha384-D+9gmBxUQogRLqvARvNLmA9hS2x//eK1FhVb9PiU86gmcrBrJAQT8okdJ4LMp2uv" crossorigin="anonymous">
<!-- The loading of KaTeX is deferred to speed up page rendering -->
<script src="https://cdn.jsdelivr.net/npm/katex@0.10.0-rc.1/dist/katex.min.js" integrity="sha384-483A6DwYfKeDa0Q52fJmxFXkcPCFfnXMoXblOkJ4JcA8zATN6Tm78UNL72AKk+0O" crossorigin="anonymous"></script>
<!-- To automatically render math in text elements, include the auto-render extension: -->
<script defer src="https://cdn.jsdelivr.net/npm/katex@0.10.0-rc.1/dist/contrib/auto-render.min.js" integrity="sha384-yACMu8JWxKzSp/C1YV86pzGiQ/l1YUfE8oPuahJQxzehAjEt2GiQuy/BIvl9KyeF" crossorigin="anonymous"
onload="renderMathInElement(document.body);"></script>
</head>
<body>
<p id="IdentificatorForElement"></p>
<script>
katex.render("f(x)^2 = \\sqrt{y}", document.getElementById('IdentificatorForElement'), {
throwOnError: false
});
</script>
</body>
</html>
答案 1 :(得分:2)
要自动渲染方程式而不必添加任何其他JavaScript代码,必须在$ \(
和\)
中将内联数学而不是美元符号包装起来,因为美元符号在普通文本中太常见了。因此,请改用此:
<!DOCTYPE html>
<!-- KaTeX requires the use of the HTML5 doctype. Without it, KaTeX may not render properly -->
<html>
<head>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.10.0-rc.1/dist/katex.min.css" integrity="sha384-D+9gmBxUQogRLqvARvNLmA9hS2x//eK1FhVb9PiU86gmcrBrJAQT8okdJ4LMp2uv" crossorigin="anonymous">
<!-- The loading of KaTeX is deferred to speed up page rendering -->
<script src="https://cdn.jsdelivr.net/npm/katex@0.10.0-rc.1/dist/katex.min.js" integrity="sha384-483A6DwYfKeDa0Q52fJmxFXkcPCFfnXMoXblOkJ4JcA8zATN6Tm78UNL72AKk+0O" crossorigin="anonymous"></script>
<!-- To automatically render math in text elements, include the auto-render extension: -->
<script defer src="https://cdn.jsdelivr.net/npm/katex@0.10.0-rc.1/dist/contrib/auto-render.min.js" integrity="sha384-yACMu8JWxKzSp/C1YV86pzGiQ/l1YUfE8oPuahJQxzehAjEt2GiQuy/BIvl9KyeF" crossorigin="anonymous"
onload="renderMathInElement(document.body);"></script>
</head>
<body>
<p>\(x^2 = \sqrt{y}\)</p>
</body>
</html>