H5:MathJax解析数学公式
一、概述
MathJax是一款开源的JavaScript显示引擎,适用所有现代浏览器,使用MathJax可以方便的在浏览器中显示数学公式,不需要使用图片。
目前,MathJax可以解析Latex、MathML和ASCIIMathML的标记语言。
MathJax官网:MathJax官网
Github地址:MathJax Github
中文文档地址:MathJax中文文档
英文文档地址:MathJax英文文档
MathJax语法:MathJax语法
二、安装
1、引入
MathJax使用网络字体(大部分浏览器都支持)去产生高质量的排版,使其在所有分辨率都可缩放和显示。
字体资源较大,所以,通常推荐使用CDN网络分发进行加载。
国外用户基本上使用Jsdelivr。
<script id="MathJax-script" <strong>async</strong> src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>
国内用户使用以下cdn
3.x版本
<script type="text/javascript" id="MathJax-script" asyncsrc="https://cdnjs.cloudflare.com/ajax/libs/mathjax/3.0.0/es5/tex-mml-chtml.js">
</script>
2.x版本
<script type="text/javascript" async src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-MML-AM_CHTML"></script>
需要注意的是,MathJax 3.x和2.x版本差异比较大,3.x版本采用模块化开发,可以按需加载。部分2.x版本API在3.x中过时不可用。出于简便性和稳定性考虑,我选择了2.x版本。
2、配置
mathjax.js
let isMathjaxConfig = false;//用于标识是否配置const initMathjaxConfig = () => {if (!window.MathJax) {return;}window.MathJax.Hub.Config({showProcessingMessages: true, //关闭js加载过程信息messageStyle: "none", //none不显示信息 normal和simple显示jax: ["input/TeX", "output/HTML-CSS"],tex2jax: {inlineMath: [["$", "$"], ["\\(", "\\)"]], //行内公式选择符displayMath: [["$$", "$$"], ["\\[", "\\]"]], //段内公式选择符// skipTags: ["script", "noscript", "style", "textarea", "pre", "code", "a"], //跳过指定标签//includeHtmlTags: { //配置在数学公式里可以出现哪些标签,例如公式里出现 n可用于换行 // br: '\n', wbr: '', '#comment': ''//}},"HTML-CSS": {availableFonts: ["STIX", "TeX"],showMathMenu: false //右击菜单显示}});isMathjaxConfig = true; //配置完成,改为true
};const MathQueue = function (elementId) {if (!window.MathJax) {return;}window.MathJax.Hub.Queue(["Typeset", window.MathJax.Hub, document.getElementById(elementId)]);
};export default {isMathjaxConfig,initMathjaxConfig,MathQueue
}
更多配置详见概述中的文档链接。
其中,需要注意的是,以上配置基于2.7.5版本,而window.MathJax.Hub.Queue(["Typeset", MathJax.Hub])
该方法在3.x版本中已过时。
在以下示例2中,使用的是MathJax.tex2chtmlPromise()
函数,但也存在渲染的dom中存在非公式组件会样式丢失的问题。
查阅官方文档,看到如下:
因此,在3.x版本中,可以使用MathJax.typesetPromise()
替代实现window.MathJax.Hub.Queue(["Typeset", MathJax.Hub])
index.js
import mathjax from '@/math/mathjax.js'
Vue.prototype.$mathjax = mathjax;
三、例子
1、静态页面渲染公式
<!DOCTYPE html>
<head><meta charset="utf-8"><meta http-equiv="x-ua-compatible" content="ie=edge"><meta name="viewport" content="width=device-width"><title>MathJax v3 with TeX input and HTML output</title><script src="https://polyfill.io/v3/polyfill.min.js?features=es6"></script><script>MathJax = {tex: {inlineMath: [['$', '$'], ['\\(', '\\)']]}};</script><script id="MathJax-script" async src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-chtml.js"></script>
</head>
<body><h1>MathJax v3 beta: TeX input, HTML output test</h1><p>When $a \ne 0$, there are two solutions to \(ax^2 + bx + c = 0\) and they are$$x = {-b \pm \sqrt{b^2-4ac} \over 2a}.$$</p><h2>The Lorenz Equations</h2><p>\begin{align}\dot{x} & = \sigma(y-x) \\\dot{y} & = \rho x - y - xz \\\dot{z} & = -\beta z + xy\end{align}</p><h2>The Cauchy-Schwarz Inequality</h2><p>\[\left( \sum_{k=1}^n a_k b_k \right)^{\!\!2} \leq\left( \sum_{k=1}^n a_k^2 \right) \left( \sum_{k=1}^n b_k^2 \right)\]</p><h2>A Cross Product Formula</h2><p>\[\mathbf{V}_1 \times \mathbf{V}_2 =\begin{vmatrix}\mathbf{i} & \mathbf{j} & \mathbf{k} \\\frac{\partial X}{\partial u} & \frac{\partial Y}{\partial u} & 0 \\\frac{\partial X}{\partial v} & \frac{\partial Y}{\partial v} & 0 \\\end{vmatrix}\]</p><h2>The probability of getting \(k\) heads when flipping \(n\) coins is:</h2><p>\[P(E) = {n \choose k} p^k (1-p)^{ n-k} \]</p><h2>An Identity of Ramanujan</h2><p>\[\frac{1}{(\sqrt{\phi \sqrt{5}}-\phi) e^{\frac25 \pi}} =1+\frac{e^{-2\pi}} {1+\frac{e^{-4\pi}} {1+\frac{e^{-6\pi}}{1+\frac{e^{-8\pi}} {1+\ldots} } } }\]</p><h2>A Rogers-Ramanujan Identity</h2><p>\[1 + \frac{q^2}{(1-q)}+\frac{q^6}{(1-q)(1-q^2)}+\cdots =\prod_{j=0}^{\infty}\frac{1}{(1-q^{5j+2})(1-q^{5j+3})},\quad\quad \text{for $|q| < 1$}.\]</p><h2>Maxwell's Equations</h2><p>\begin{align}\nabla \times \vec{\mathbf{B}} -\, \frac1c\, \frac{\partial\vec{\mathbf{E}}}{\partial t} & = \frac{4\pi}{c}\vec{\mathbf{j}} \\\nabla \cdot \vec{\mathbf{E}} & = 4 \pi \rho \\\nabla \times \vec{\mathbf{E}}\, +\, \frac1c\, \frac{\partial\vec{\mathbf{B}}}{\partial t} & = \vec{\mathbf{0}} \\\nabla \cdot \vec{\mathbf{B}} & = 0\end{align}</p><h2>In-line Mathematics</h2><p>Finally, while display equations look good for a page of samples, theability to mix math and text in a paragraph is also important. Thisexpression $\sqrt{3x-1}+(1+x)^2$ is an example of an inline equation. Asyou see, MathJax equations can be used this way as well, without undulydisturbing the spacing between lines.</p></body>
</html>
2、事件触发渲染
<!DOCTYPE html>
<html lang="en"><head><meta charset="utf-8"><meta http-equiv="x-ua-compatible" content="ie=edge"><meta name="viewport" content="width=device-width"><title>MathJax v3 with interactive TeX input and HTML output</title><script src="https://polyfill.io/v3/polyfill.min.js?features=es6"></script><script id="MathJax-script" async src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-chtml.js"></script><script>function convert() {//// Get the TeX input//var input = document.getElementById("input").value.trim();//// Disable the display and render buttons until MathJax is done//var display = document.getElementById("display");var button = document.getElementById("render");button.disabled = display.disabled = true;//// Clear the old output//output = document.getElementById('output');output.innerHTML = '';//// Reset the tex labels (and automatic equation numbers, though there aren't any here).// Get the conversion options (metrics and display settings)// Convert the input to CommonHTML output and use a promise to wait for it to be ready// (in case an extension needs to be loaded dynamically).//MathJax.texReset();var options = MathJax.getMetricsFor(output);options.display = display.checked;MathJax.tex2chtmlPromise(input, options).then(function (node) {//// The promise returns the typeset node, which we add to the output// Then update the document to include the adjusted CSS for the// content of the new equation.//output.appendChild(node);MathJax.startup.document.clear();MathJax.startup.document.updateDocument();}).catch(function (err) {//// If there was an error, put the message into the output instead//output.appendChild(document.createElement('pre')).appendChild(document.createTextNode(err.message));}).then(function () {//// Error or not, re-enable the display and render buttons//button.disabled = display.disabled = false;});}</script><style>#frame {max-width: 40em;margin: auto;}#input {border: 1px solid grey;margin: 0 0 .25em;width: 100%;font-size: 120%;box-sizing: border-box;}#output {font-size: 120%;margin-top: .75em;border: 1px solid grey;padding: .25em;min-height: 2em;}#output > pre {margin-left: 5px;}.left {float: left;}.right {float: right;}</style>
</head><body>
<div id="frame"><h1>MathJax v3: TeX to HTML</h1><textarea id="input" rows="15" cols="10">
%
% Enter TeX commands below
%
x = {-b \pm \sqrt{b^2-4ac} \over 2a}.
</textarea>
<br />
<div class="left">
<input type="checkbox" id="display" checked onchange="convert()"> <label for="display">Display style</label>
</div>
<div class="right">
<input type="button" value="Render TeX" id="render" onclick="convert()" />
</div>
<br clear="all" />
<div id="output"></div>
</div></body></html>
3、动态数据解析
// v-html 加载动态数据
<div v-html="contnt">// 之后调用MathJax重新渲染
this.$nextTick(() => {if (this.$mathjax.isMathjaxConfig) {this.$mathjax.initMathjaxConfig();}this.$mathjax.MathQueue("app");
});
3、更多例子,见 MathJax
参考:
基于vue渲染Latex数学公式(simplemde-editor)
Vue使用MathJax动态识别数学公式
MathJax的基本使用
H5:MathJax解析数学公式相关推荐
- uniapp中使用mathJax解析数学公式
需求来源 最近开发一款做题app的时候,遇到了需要解析数学公式的需求,无疑我们应该选择简单而又强大的mathJax.github地址:https://github.com/mathjax/MathJa ...
- 安卓Android与H5双向交互MathJax展示数学公式(源码+解析)
安卓Android与H5双向交互MathJax展示数学公式(源码+解析) 博主就今天周五又做了个需求(安卓Android与H5交互),原来上线的功能是服务器配置过来的学习报告(一个H5页面)并提供原始 ...
- iOS--3分钟教会你用mathjax显示数学公式
iOS–3分钟教会你用mathjax显示数学公式 最近开发数学教程,需要显示数学公式和图形,找了很多资料,找到2种方法,1.服务器返回Latex的字符串 我在客户端用webView来展示与交互,但是有 ...
- vue+MathJax 显示数学公式
1.在线cdn引入 1.1 引入后行内公式显示小括号一定要加"\\ ",注意斜杠后面有个空格 <script type="text/javascript" ...
- 前端通过MathJax显示数学公式(vue+MathJax)
文章目录 一.关于MathJax 二.应用场景 三.遇到的问题 问题1(没有渲染效果) 问题2(行间公式不被渲染) 四.未来的方向 一.关于MathJax 官方网址:MathJax使用文档 简介:Ma ...
- vue中使用MathJax展示数学公式
效果 引入第三方包 <script type="text/javascript" async src="https://cdnjs.cloudflare.com/a ...
- php中高光显示的高数,[技术博客]React Native——HTML页面代码高亮数学公式解析...
问题起源 原有博文显示时代码无法高亮,白底黑字的视觉效果不好. 原有博文中无法解析数学公式,导致页面会直接显示数学公式源码. 为了解决这两个问题,尝试了一些方法,最终利用开源类库实现了页面美化. (失 ...
- uni-app h5解析二维码
项目原本是要做h5扫描解析二维码,后来发现h5不支持扫描(基于uni-app开发),所以做成上传二维码图片然后解析 刚开始用的qrcode-decoder解析,后来反馈拍照的图片不清晰识别不出来,然后 ...
- 数学表达式解析器简介
2019独角兽企业重金招聘Python工程师标准>>> 在实际项目开发中如果需要解析数学公式,无须再运用解释器模式进行设计,可以直接使用一些第三方解析工具包,它们可以统称为数学表达式 ...
- H5实例教学--微信内嵌视频1(案例浅析)
以上为案例二维码 首个H5案例解析 从头开始分析 在 iOS 上,APP 都是使用的系统自带的浏览器进行页面渲染,video 播放视频的效果是统一的,只需要考虑不同的 iOS 版本是否有不一致的地方. ...
最新文章
- b站看那个的python_用Python获取B站播放历史记录 !男友居然天天背着我看这些!...
- c语言枚举3位数相加等于10,C语言 联合和枚举
- MaxCompute(原ODPS) Studio 2.7.0 版本发布,让代码效率更高
- 仿写strncmp函数
- 数据库-优化-通过执行计划查询分析SQL执行计划-每个字段的说明
- CDN - 域名解析错误排查
- 字符串之字符数组种是否所有的字符都只出现过一次
- dedecms模版php,dedecms专题模板怎么用
- dosbox edit.exe 如何使用_宅家30天,2万字节java高级工程师面试题解析,如何斩获阿里p7...
- sql server 查询本周、本月所有天数的数据
- C文件操作函数fscanf和fprintf的使用
- 韦氏评级:担心比特币近期价格走势的人都过于关注短期
- BZOJ3171:[TJOI2013]循环格
- 百度谭中意:我和开源20年
- 动画效果编程基础详解--Animation Android
- 用秩讨论线性方程组的解/三个平面的位置关系
- S.M.A.R.T. 参数(smartctl)计算硬盘精确健康值
- Keil 5(MDK 5)中的 Pack Installer下载不了库文件包的解决替代方法(在Keil官网下载Packs库文件)
- 美国空运专线 美国空运专线时效多久
- c++高级编程学习笔记4
热门文章
- 如何更改XP系统默认字体 讨论话题……
- SEO基础知识完美教程
- 服务认证的介绍-实施依据及作用
- K3S执行命令提示You must be logged in to the server (Unauthorized)
- 720不能建立远程计算机连接,宽带连接错误720:不能建立到远程计算机的连接 正确解决方法...
- 一年读了八十本书之后,我推荐这五本给你
- Asp.net 对路径的访问被拒绝 解决办法
- 怎样把m4a转换mp3格式?
- 用java给pdf压缩并加密_Java实现多文件压缩加密并重命名压缩文件对象的方法
- 分布式监控平台CAT原理