Guoguo-notes
主页
  • 常用笔记
  • 飞码篇
  • Java
  • React笔记
  • 袁果锅生态
GitHub
主页
  • 常用笔记
  • 飞码篇
  • Java
  • React笔记
  • 袁果锅生态
GitHub
  • 常用笔记

    • CSS 封装.md
    • CSS笔记.md
    • Echarts.md
    • Element Plus --vue3.0.md
    • Element ui 笔记.md
    • Git 代码管理.md
    • Html 笔记.md
    • Taro.md
    • TypeScript.md
    • Vue 代码片段.md
    • Vue 全局封装 main.js.md
    • Vue 笔记.md
    • Vue3 .md
    • Vue3+Element Plus.md
    • axios 请求拦截.md
    • ecahrts 使用地图报错问题.md
    • element plus 本地启动.md
    • jsx tsx 代码片段.md
    • jsx tsx 笔记.md
    • js常规.md
    • npm.md
    • sh 笔记.md
    • uniapp笔记.md
    • valibot校验学习.md
    • vite 笔记.md
    • vite手写插件.md
    • vue.js 下载文件.md
    • vueuse笔记.md
    • vxe-table笔记.md
    • 云开发教程.md
    • 公共API接口.md
    • 小程序笔记.md
    • 常用插件.md
    • 插件库.md
    • 服务器.md
    • 服务器部署教学.md
    • 毕业设计接单.md
    • 汇智腾远笔记.md
    • 浏览器px to rem适配.md
    • 登录流程.md
    • 登录逻辑.md
    • 网站配色.md
    • 视频播放插件.md
    • 解析数学公式.md
    • 跨域代理.md

使用

<MathJaxRenderer class="msg aiMsg" :ref="'math' + item.id">
              {{ item.content }}
</MathJaxRenderer>

组件代码

<!-- @format -->

<template>
  <div ref="mathContainer" class="math-container">
    <!-- {{ content }} -->
    <slot></slot>
  </div>
</template>

<script>
  export default {
    name: 'MathJaxRenderer',
    props: {
      content: {
        type: String,
        default: '',
      },
      options: {
        type: Object,
        default: () => ({}),
      },
    },
    data() {
      return {
        mathContainer: null,
      };
    },
    watch: {
      content: {
        handler(newContent) {
          if (this.mathContainer && newContent) {
            this.mathContainer.innerHTML = newContent;
            this.renderMathJax();
          }
        },
        immediate: true,
      },
    },
    methods: {
      refreshMathJax() {
        // 确保容器存在
        if (!this.mathContainer) {
          this.mathContainer = this.$refs.mathContainer;
        }

        // 强制重新渲染
        this.renderMathJax();

        // 返回Promise以便调用者可以在渲染完成后执行操作
        return new Promise((resolve) => {
          this.$nextTick(() => {
            resolve();
          });
        });
      },
      configureMathJax() {
        window.MathJax = {
          tex: {
            inlineMath: [
              ['\\(', '\\)'],
              ['$', '$'],
            ],
            displayMath: [
              ['$$', '$$'],
              ['\\[', '\\]'],
            ],
            packages: [
              'base',
              'ams',
              'noerrors',
              'noundefined',
              'newcommand',
              'boldsymbol',
              'cancel',
              'color',
              'enclose',
              'mhchem',
              'physics',
            ],
            macros: {
              // 数学集合符号
              '\\mathbb': '\\mathbf',
              '\\R': '\\mathbb{R}',
              '\\N': '\\mathbb{N}',
              '\\Z': '\\mathbb{Z}',
              '\\Q': '\\mathbb{Q}',
              '\\C': '\\mathbb{C}',

              // 极限、求和、积分
              '\\lim': '\\lim\\limits',
              '\\sum': '\\sum\\limits',
              '\\int': '\\int\\limits',
              '\\iint': '\\iint\\limits',
              '\\iiint': '\\iiint\\limits',
              '\\prod': '\\prod\\limits',

              // 基本函数
              '\\frac': '\\frac',
              '\\sqrt': '\\sqrt',

              // 三角函数
              '\\sin': '\\sin',
              '\\cos': '\\cos',
              '\\tan': '\\tan',
              '\\cot': '\\cot',
              '\\sec': '\\sec',
              '\\csc': '\\csc',
              '\\arcsin': '\\arcsin',
              '\\arccos': '\\arccos',
              '\\arctan': '\\arctan',

              // 双曲函数
              '\\sinh': '\\sinh',
              '\\cosh': '\\cosh',
              '\\tanh': '\\tanh',

              // 符号和常数
              '\\infty': '\\infty',
              '\\partial': '\\partial',
              '\\nabla': '\\nabla',
              '\\pi': '\\pi',
              '\\epsilon': '\\epsilon',
              '\\delta': '\\delta',
              '\\Delta': '\\Delta',

              // 矩阵相关
              '\\begin{matrix}': '\\begin{matrix}',
              '\\end{matrix}': '\\end{matrix}',
              '\\begin{pmatrix}': '\\begin{pmatrix}',
              '\\end{pmatrix}': '\\end{pmatrix}',
              '\\begin{bmatrix}': '\\begin{bmatrix}',
              '\\end{bmatrix}': '\\end{bmatrix}',

              // 对齐环境
              '\\begin{align}': '\\begin{align}',
              '\\end{align}': '\\end{align}',
              '\\begin{aligned}': '\\begin{aligned}',
              '\\end{aligned}': '\\end{aligned}',

              // 其他常用符号
              '\\to': '\\to',
              '\\mapsto': '\\mapsto',
              '\\Rightarrow': '\\Rightarrow',
              '\\Leftarrow': '\\Leftarrow',
              '\\Leftrightarrow': '\\Leftrightarrow',
            },
            processEscapes: true,
            processEnvironments: true,
            processRefs: true,
            tags: 'ams',
          },
          options: {
            enableMenu: false,
            ...this.options,
            renderActions: {
              addMenu: [0, '', ''],
              checkLoading: [1000, () => '', ''],
            },
            ignoreHtmlClass: 'tex2jax_ignore',
            processHtmlClass: 'tex2jax_process',
          },
          startup: {
            pageReady: () => {
              return window.MathJax.startup.defaultPageReady();
            },
          },
          loader: {
            load: [
              '[tex]/color',
              '[tex]/cancel',
              '[tex]/enclose',
              '[tex]/mhchem',
              '[tex]/physics',
            ],
          },
        };
      },
      renderMathJax() {
        if (window.MathJax && window.MathJax.typesetPromise) {
          // 先清除之前的渲染,避免重复渲染导致的问题
          if (window.MathJax.typesetClear) {
            window.MathJax.typesetClear([this.mathContainer]);
          }
          // 确保DOM已更新后再渲染
          this.$nextTick(() => {
            window.MathJax.typesetPromise([this.mathContainer])
              .then(() => {
                console.log('MathJax渲染完成');
              })
              .catch((err) => {
                console.error('MathJax渲染错误:', err);
              });
          });
        }
      },
      loadMathJax() {
        return new Promise((resolve) => {
          const script = document.createElement('script');
          script.src =
            'https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js';
          script.async = true;
          document.head.appendChild(script);

          script.onload = (e) => {
            if (window.MathJax && window.MathJax.typesetPromise) {
              this.configureMathJax();
              this.renderMathJax();
              resolve();
            } else {
              setTimeout(checkMathJax, 100);
            }
          };
        });
      },
    },
    mounted() {
      this.mathContainer = this.$refs.mathContainer;
      this.loadMathJax();
    },

    updated() {
      this.renderMathJax();
    },
  };
</script>

<style scoped>
  .math-container {
    font-family: 'Times New Roman', serif;
    line-height: 1.6;
  }
</style>

Edit this page
Prev
视频播放插件.md
Next
跨域代理.md