网页渲染数学公式之KaTeX引入

KaTeX可以排版复杂的数学公式,在网页上直接应用公式文字而不是图片是一个不错的选择。
1.首先引入对应KaTeX的css样式,js脚本文件。

  <head>
   <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  src="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.js" integrity="sha384-g7c+Jr9ZivxKLnZTDUhnkOnsh30B4H0rpLUpJ4jAIKs4fnJI+sEnkvrMWph2EDg4" crossorigin="anonymous"></script>
	<script  src="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/contrib/auto-render.min.js" integrity="sha384-mll67QQFJfxn0IYznZYonOWZ644AWYC+Pt2cHqMaRhXVrursRwvLnLaebdGIlYNa" crossorigin="anonymous"
    onload="renderMathInElement(document.body);"></script>

  </head>

可以看到引入了css文件,katex.min.js文件,还有auto-render.min.js这个script标签还注册了一个onload事件,事件回调了一个renderMathInElement函数。
2.直接定义一个script标签对渲染数学公式,然后填充元素。

<script type="text/javascript" >
	katex.render("c = \\pm\\sqrt{a^2 + b^2}", document.getElementById("math"), {
    throwOnError: false
});
</script>

可以看到,这里引入了katex变量,这个变量随着上面引入的katex.min.js已经成了全局变量,所以可以直接使用的呢。如果是再node里面应该需要再使用的地方import进来。原理是一样的。
3完整的代码如下

<!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.12.0/dist/katex.min.css" integrity="sha384-AfEj0r4/OFrOo5t7NnNe46zW/tFgW6x/bCJG8FqQCEo3+Aro6EYUG4+cU+KJWu/X" crossorigin="anonymous">
<script  src="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.js" integrity="sha384-g7c+Jr9ZivxKLnZTDUhnkOnsh30B4H0rpLUpJ4jAIKs4fnJI+sEnkvrMWph2EDg4" crossorigin="anonymous"></script>
<script  src="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/contrib/auto-render.min.js" integrity="sha384-mll67QQFJfxn0IYznZYonOWZ644AWYC+Pt2cHqMaRhXVrursRwvLnLaebdGIlYNa" crossorigin="anonymous"
    onload="renderMathInElement(document.body);"></script>

  </head>
<body>
<div id="math">

</div>

</body>


<script type="text/javascript" >
katex.render("c = \\pm\\sqrt{a^2 + b^2}", document.getElementById("math"), {
    throwOnError: false
});
</script>

</html>

这样。保存文件后缀为html,就可以直接再浏览器上测试效果了,会在浏览器左上角渲染出一个类似这样的数学等式在这里插入图片描述
4.如果你想加速网页DOM加载速度,那么,可以为上面引入的script标签加入defer属性,类似这样

<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"
    onload="renderMathInElement(document.body);"></script>

这样的话,标签里面的js是移步加载的,DOM文档渲染好了,它才开始加载,这样,网页的体验会好一点。然而如果移步加载的话,你使用katex这个全局变量的时候可能由于没有初始化好这个变量造成变量未定义错误。所以,我们需要监听一个事件,等待网页加载完毕,再执行数学公式的渲染。类似这样

 document.addEventListener("DOMContentLoaded", function() {
       katex.render("c = \\pm\\sqrt{a^2 + b^2}", document.getElementById("math"), {
    throwOnError: false
});
    });

通过注册DOMContentLoaded事件的监听,然后回调katex.render函数,那么,我们就可以游刃有余的使用katex取渲染数学公式了。
如果有什么疑问,欢迎留言讨论。如果感觉我写的对你有帮助,欢迎 donate me

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用以下步骤在 C# 中调用 MathMLControl.DLL 来渲染数学公式: 1. 引用 MathMLControl.DLL: 将 MathMLControl.DLL 复制到你的项目目录中,并在 Visual Studio 的解决方案资源管理器中右键点击“引用”,选择“添加引用”,在弹出的对话框中选择“浏览”,找到 MathMLControl.DLL 并添加引用。 2. 声明 MathMLControl 类型: 在你的代码中声明 MathMLControl 类型,例如: ```csharp using System.Runtime.InteropServices; [DllImport("MathMLControl.dll")] public static extern void SetBasePath(string pszBasePath); [DllImport("MathMLControl.dll")] public static extern bool LoadFile(string pszFileName); [DllImport("MathMLControl.dll")] public static extern bool LoadString(string pszMathML); [DllImport("MathMLControl.dll")] public static extern bool Render(IntPtr hDC, int x, int y, int cx, int cy); [DllImport("MathMLControl.dll")] public static extern string GetLastError(); public class MathMLControl { // 定义接口 } ``` 3. 调用 MathMLControl 接口: 在你的代码中调用 MathMLControl 接口来渲染数学公式,例如: ```csharp // 设置 MathMLControl.DLL 的基本路径 MathMLControl.SetBasePath("C:\\MathMLControl\\"); // 加载 MathML 文件 MathMLControl.LoadFile("C:\\MathMLControl\\example.mathml"); // 渲染 MathML 并将结果输出到指定的设备上 IntPtr hDC = this.CreateGraphics().GetHdc(); MathMLControl.Render(hDC, 0, 0, this.Width, this.Height); this.CreateGraphics().ReleaseHdc(hDC); // 获取 MathMLControl.DLL 的最后一次错误信息 string error = MathMLControl.GetLastError(); ``` 注意:以上代码仅供参考,实际使用时应根据具体情况进行调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值