找回密码
 快速注册
搜索
查看: 31|回复: 3

使MathJax報告錯誤

[复制链接]

3149

主题

8386

回帖

6万

积分

$\style{scale:11;fill:#eff}꩜$

积分
65391
QQ

显示全部楼层

hbghlyj 发表于 2023-10-31 05:50 |阅读模式
本帖最后由 hbghlyj 于 2024-2-25 18:00 编辑 AOPS发帖时自动检查TeX语法,遇錯誤提示
You have an error in your LaTeX code. I have rendered it in red. You can post with error and hope someone will tell you how to fix it, or you can try to fix the error yourself.

Screenshot 2023-10-30 214644.png

3149

主题

8386

回帖

6万

积分

$\style{scale:11;fill:#eff}꩜$

积分
65391
QQ

显示全部楼层

 楼主| hbghlyj 发表于 2024-2-26 00:23
noerror.js定義了error如何處理:
  1. /**
  2. * Generates an error node containing the erroneous expression.
  3. * @param {TexParser} parser The node factory.
  4. * @param {string} message The error message (which is ignored).
  5. * @param {string} id The error id (which is ignored).
  6. * @param {string} expr The original LaTeX expression.
  7. */
  8. function noErrors(factory: NodeFactory,
  9.                   message: string, _id: string, expr: string) {
  10.   let mtext = factory.create('token', 'mtext', {}, expr.replace(/\n/g, ' '));
  11.   let error = factory.create('node', 'merror', [mtext], {'data-mjx-error': message, title: message});
  12.   return error;
  13. }
复制代码

它接受4個参數:
  • factory是MathJax内部的;
  • message是錯誤信息,後在title: message中用到;
  • id是發生錯誤的元素id,没用到;
  • expr是發生錯誤的LaTeX.
  • 3149

    主题

    8386

    回帖

    6万

    积分

    $\style{scale:11;fill:#eff}꩜$

    积分
    65391
    QQ

    显示全部楼层

     楼主| hbghlyj 发表于 2024-2-26 00:37
    hbghlyj 发表于 2023-10-30 21:50
    建议论坛编辑器发帖时也自动检查TeX语法 ...


    我找到了一種方法:
    在上面的noErrors中,加一句showDialog來提醒用戶:

    只需在控製台運行:(如果正常加到ready裡,後面這句MathJax.startup.getComponents() 不需要。)
    1. MathJax._.input.tex.noerrors.NoErrorsConfiguration.NoErrorsConfiguration.nodes.error=function(factory, message, _id, expr) {
    2.   let mtext = factory.create('token', 'mtext', {}, expr.replace(/\n/g, ' '));
    3.   let error = factory.create('node', 'merror', [mtext], {'data-mjx-error': message, title: message});
    4.   showDialog('<h1>錯誤</h1>'+message+'<h1>位置</h1>'+expr);
    5.   return error;
    6. }
    7. MathJax.startup.getComponents()
    复制代码

    效果:
    output.gif

    3149

    主题

    8386

    回帖

    6万

    积分

    $\style{scale:11;fill:#eff}꩜$

    积分
    65391
    QQ

    显示全部楼层

     楼主| hbghlyj 发表于 2024-2-26 01:05

    建议发帖时自动检查TeX语法

    当用户提交时,在隐藏的地方排版MathJax,当遇到错误时像 AOPS 一样显示错误提示信息,如果没有错误则提交表单

    手机版|悠闲数学娱乐论坛(第3版)

    GMT+8, 2025-3-4 12:19

    Powered by Discuz!

    × 快速回复 返回顶部 返回列表