uniapp中使用mathJax解析数学公式

需求来源

最近开发一款做题app的时候,遇到了需要解析数学公式的需求,无疑我们应该选择简单而又强大的mathJax。github地址:https://github.com/mathjax/MathJax,截止目前最新版为3.2.0,下面采用3.2.0演示。3.2.0下载地址:https://github.com/mathjax/MathJax/releases/tag/3.2.0

一、uniapp中使用mathJax

项目下载:https://download.csdn.net/download/Y00010010/84989721

<template>
	<view class="content">
		<view>极限$\lim\limits _{x \rightarrow 1} \frac{x^{2}-1}{x-1} $的值为:()</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				
			}
		},
		onLoad() {
			
		},
		methods: {

		}
	}
</script>
<script module="test" lang="renderjs">
	export default {
		mounted() {
			window.MathJax = {
			  tex: {
			    inlineMath: [['$', '$'], ['\\(', '\\)']],
			  },
			  svg:{
				  scale: 1.2
			  }
			};
			(function () {
			  var script = document.createElement('script')
			  script.id = 'MathJax-script'
			  script.async = true
			  // script.src = 'https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-chtml.js' // cdn引入比较慢
			  script.src = 'static/tex-svg.js'; // 下载源码导入比较快,svg比chtml视觉上比较好
			  // script.src = 'static/tex-chtml.js';
			  document.head.appendChild(script)
			})();
		}
	}
</script>

二、普通网页中使用mathJax

<!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, initial-scale=1.0" />
    <title>MathJax数学公式</title>
  </head>
  <body>
    <div>
      极限$\lim\limits _{x \rightarrow 1} \frac{x^{2}-1}{x-1} $的值为:()
    </div>
    <script>
      MathJax = {
        tex: {inlineMath: [['$', '$'], ['\\(', '\\)']]},
        svg: {fontCache: 'global'}
      };
      </script>
    <script
      id="MathJax-script"
      async
      src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"
    ></script>
  </body>
</html>

祝大家生活愉快。

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
您可以使用HTML和JavaScript来在画布上显示数学公式,并实现鼠标拖动的效果。首先,您需要在HTML文件创建一个画布元素和一个包含数学公式的元素。然后,使用MathJax库将数学公式渲染到该元素。最后,使用JavaScript监听鼠标事件,实现拖动效果。 以下是一个示例的HTML代码: ```html <!DOCTYPE html> <html> <head> <script src="https://polyfill.io/v3/polyfill.min.js?features=es6"></script> <script src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-chtml.js"></script> <style> #canvas { width: 500px; height: 300px; border: 1px solid black; } </style> </head> <body> <canvas id="canvas"></canvas> <div id="formula">\(x^2 + y^2 = r^2\)</div> <script> const canvas = document.getElementById('canvas'); const ctx = canvas.getContext('2d'); let isDragging = false; let offsetX = 0; let offsetY = 0; function draw() { ctx.clearRect(0, 0, canvas.width, canvas.height); ctx.fillText("Drag the formula", 10, 20); ctx.fillText("Release the mouse to stop dragging", 10, 40); // Draw the formula at the current mouse position const x = offsetX + event.clientX - canvas.getBoundingClientRect().left; const y = offsetY + event.clientY - canvas.getBoundingClientRect().top; ctx.fillText("x = " + x, 10, 80); ctx.fillText("y = " + y, 10, 100); MathJax.typesetPromise(['#formula']).then(() => { ctx.drawImage(document.getElementById('formula'), x, y); }); } canvas.addEventListener('mousedown', (event) => { isDragging = true; offsetX = canvas.width / 2; offsetY = canvas.height / 2; draw(); }); canvas.addEventListener('mousemove', (event) => { if (isDragging) { draw(); } }); canvas.addEventListener('mouseup', (event) => { isDragging = false; draw(); }); </script> </body> </html> ``` 在上面的示例,我们创建了一个500x300像素的画布和一个包含数学公式 \(x^2 + y^2 = r^2\) 的 `<div>` 元素。当鼠标在画布上按下并拖动时,会根据鼠标位置重新绘制公式,并在公式旁边显示当前鼠标的x和y坐标。当鼠标放开时,停止拖动并保持公式的位置。 请注意,示例使用MathJax库来渲染数学公式。您需要在页面引入MathJax的脚本,并使用`MathJax.typesetPromise()`方法来渲染公式。 希望这可以帮助到您!如有任何问题,请随时提问。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

1条粉肠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值