KaTex
官网:https://katex.org/KaTex 支持 浏览器(Browser)和服务器(NodeJs),NodeJs渲染成html可以再发送给Browser。官方的API浏览器器实现准备katex.min.css 必须katex.min.js 必须auto-render.min.jscopy-tex.min.js 根据需要选择加入下载katex:之后拷贝必须的js官方的API 需要 tex的
·
官网:https://katex.org/
KaTex 支持 浏览器(Browser)和服务器(NodeJs),NodeJs渲染成html可以再发送给Browser。
根据官方的启动模版创建
Starter template
<!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.15.6/dist/katex.min.css" integrity="sha384-ZPe7yZ91iWxYumsBEOn7ieg8q/o+qh/hQpSaPow8T6BwALcXSCS6C6fSRPIAnTQs" crossorigin="anonymous">
<!-- The loading of KaTeX is deferred to speed up page rendering -->
<script defer src="https://cdn.jsdelivr.net/npm/katex@0.15.6/dist/katex.min.js" integrity="sha384-ljao5I1l+8KYFXG7LNEA7DyaFvuvSCmedUf6Y6JI7LJqiu8q5dEivP2nDdFH31V4" 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.15.6/dist/contrib/auto-render.min.js" integrity="sha384-+XBljXPPiv+OzfbB3cVmLHf4hdUFHlWNZN5spNQ7rmHTXpd7WvJum6fIACpNNfIR" crossorigin="anonymous"
onload="renderMathInElement(document.body);"></script>
</head>
...
</html>
官方的API
katex.render("c = \\pm\\sqrt{a^2 + b^2}", element, {
throwOnError: false
});
现在开始浏览器器在渲染公式
准备
katex.min.css 必须
katex.min.js 必须
auto-render.min.js copy-tex.min.js 根据需要选择加入
下载KaTeX
npm install katex
之后拷贝必须的js
官方的API 需要 tex的表达式和 DOM 元素来渲染页面
- 我们在页面创建渲染元素
<span id="sp1"></span>
- 由于,官方模版中的
<script defer></script>
, defer 的使用一般是页面有许多内容,可以使脚本没有引入之前先渲染body体的内容,等脚本加载完成后再执行,这就要求我们必须要等待document加载完再执行js,如果body里有引用defer的脚本里的变量的时候,浏览器就不认识这个变量了,浏览器会认为undefined. 所以我们监听 body的 onload事件。
html5
<!DOCTYPE html>
<!-- KaTeX requires the use of the HTML5 doctype. Without it, KaTeX may not render properly -->
<html lang="en">
<head>
<meta charset="UTF-8">
<title>KATEX</title>
<link rel="stylesheet" href="/katex/0.15.6/dist/katex.min.css" integrity="sha384-ZPe7yZ91iWxYumsBEOn7ieg8q/o+qh/hQpSaPow8T6BwALcXSCS6C6fSRPIAnTQs" crossorigin="anonymous">
<!-- The loading of KaTeX is deferred to speed up page rendering -->
<script defer src="/katex/0.15.6/dist/katex.min.js" integrity="sha384-ljao5I1l+8KYFXG7LNEA7DyaFvuvSCmedUf6Y6JI7LJqiu8q5dEivP2nDdFH31V4" crossorigin="anonymous"></script>
<!-- To automatically render math in text elements, include the auto-render extension: -->
<script defer src="/katex/0.15.6/dist/contrib/auto-render.min.js" integrity="sha384-+XBljXPPiv+OzfbB3cVmLHf4hdUFHlWNZN5spNQ7rmHTXpd7WvJum6fIACpNNfIR" crossorigin="anonymous"
onload="renderMathInElement(document.body);"></script>
<link href="/katex/0.15.6/dist/contrib/copy-tex.min.css" rel="stylesheet">
<script defer src="/katex/0.15.6/dist/contrib/copy-tex.min.js"></script>
<script defer src="/webfontloader/1.6.28/webfontloader.js" integrity="sha256-4O4pS1SH31ZqrSO2A/2QJTVjTPqVe+jnYgOWUVr7EEc=" crossorigin="anonymous"></script>
</head>
<body>
<span id="sp1">...</span>
<script>
window.WebFontConfig = {
custom: {
families: ['KaTeX_AMS', 'KaTeX_Caligraphic:n4,n7', 'KaTeX_Fraktur:n4,n7',
'KaTeX_Main:n4,n7,i4,i7', 'KaTeX_Math:i4,i7', 'KaTeX_Script',
'KaTeX_SansSerif:n4,n7,i4', 'KaTeX_Size1', 'KaTeX_Size2', 'KaTeX_Size3',
'KaTeX_Size4', 'KaTeX_Typewriter'],
},
};
document.body.onload = function() {
let divEle = document.createElement("div")
katex.render(String.raw`c = \pm\sqrt{a^2 + b^2}`, divEle ,{throwOnError: false});
console.log(divEle);
document.body.appendChild(divEle);
let sp1Ele = document.getElementById('sp1');
katex.render(String.raw`c = \pm\sqrt{a^2 + b^2}`, sp1Ele, {
throwOnError: false
});
console.log(sp1Ele);
}
</script>
</body>
</html>
效果
更多推荐
已为社区贡献1条内容
所有评论(0)