Forgot password?
 Create new account
View 371|Reply 6

mathjax如何通过js得到公式的tex源码

[Copy link]

3151

Threads

8498

Posts

610K

Credits

Credits
66208
QQ

Show all posts

hbghlyj Posted at 2022-1-29 01:28:59 |Read mode
Last edited by hbghlyj at 2022-1-29 03:22:00如何通过js得到公式的tex源码?菜单里面的那个Copy As...是怎么实现的?
Menu.js中找到了几行相关的代码
  1. e.prototype.post=function(e,n){
  2.     if(this.mathItem){
  3.         if(void 0!==n){
  4. var o=this.mathItem.inputJax.name,i=this.findID("Show","Original");i.content="MathML"===o?"Original MathML":o+" Commands",this.findID("Copy","Original").content=i.content;var r=this.findID("Settings","semantics");"MathML"===o?r.disable():r.enable(),this.getAnnotationMenu(),this.dynamicSubmenus()
  5.         }
  6.         t.prototype.post.call(this,e,n)
  7.     }
  8. }
Copy the Code
那个Show Original和Copy Original应该是我们所需要的

3151

Threads

8498

Posts

610K

Credits

Credits
66208
QQ

Show all posts

 Author| hbghlyj Posted at 2022-1-29 02:32:33
Last edited by hbghlyj at 2022-1-29 03:18:00好像找到了show original
Screenshot 2022-01-28 191744.png

3151

Threads

8498

Posts

610K

Credits

Credits
66208
QQ

Show all posts

 Author| hbghlyj Posted at 2022-1-29 04:22:46
弄好了,把右键菜单去掉了,点击公式就会复制代码(背景变绿2秒)
测试

15

Threads

948

Posts

110K

Credits

Credits
12412

Show all posts

色k Posted at 2022-1-29 11:47:01
回复 3# hbghlyj


   

700

Threads

110K

Posts

910K

Credits

Credits
94172
QQ

Show all posts

kuing Posted at 2022-1-29 15:06:57
我这里暂时不打算这样弄。
倒是希望解决以下问题:
  1. \begin{align*}
  2. f(x)&=ax^2+bx+c\\
  3. &=a(x-x_1)(x-x_2)\\
  4. &=\cdots
  5. \end{align*}
Copy the Code
\begin{align*}
f(x)&=ax^2+bx+c\\
&=a(x-x_1)(x-x_2)\\
&=\cdots
\end{align*}
上述公式在右键 copy 代码之后变成
  1. \begin{align*}
  2. f(x)&=ax^2+bx+c\\
  3. &=a(x-x_1)(x-x_2)\\
  4. &=\cdots
  5. \end{align*}
Copy the Code
多了空行,怎么去掉?

另外,在草稿本不存在这个问题。

700

Threads

110K

Posts

910K

Credits

Credits
94172
QQ

Show all posts

kuing Posted at 2022-1-29 15:44:14
回复 5# kuing

哦,好像是因为帖子源码里既有 <br> 又有 \n 造成的,只要去掉 \n ?


是这样吗?

======

对所有 postmessage 去掉了所有的 \n ,还真 OK 了
也不影响帖子内容本身的换行(有 <br> 就行)

竟然一不小心就解决了……

3151

Threads

8498

Posts

610K

Credits

Credits
66208
QQ

Show all posts

 Author| hbghlyj Posted at 2022-5-18 08:49:49
hbghlyj 发表于 2022-1-28 21:22
弄好了,把右键菜单去掉了,点击公式就会复制代码(背景变绿2秒)
测试
如果公式里面有文字(mtext)或者链接(mathjax的\href指令,转换为MathML后变成一个mrow的href属性)的话,选择文字/点击链接以后会误触那个点击复制.这时我们需要stop event propogation(阻止事件的进一步传播),链接中举的例子是两个按钮重叠,如果点击内部按钮(child)触发了事件,阻止事件的进一步传播,从而不会触发外部按钮(parent)的事件.

在MathJax的config中加上
  1.         pageReady() {
  2.                 return MathJax.startup.defaultPageReady().then(function () {
  3.                 document.querySelectorAll('math mtext, math *[href]').forEach(e=>e.addEventListener('click',ev=>{ev.stopPropagation();}))
  4.                 })
  5.         }
Copy the Code
即可.
如果是chtml就加上mjx-前缀,变成
  1.         pageReady() {
  2.                 return MathJax.startup.defaultPageReady().then(function () {
  3.                 document.querySelectorAll('mjx-math mjx-mtext, mjx-math *[href]').forEach(e=>e.addEventListener('click',ev=>{ev.stopPropagation();}))
  4.                 })
  5.         }
Copy the Code


JS event.currentTarget( ) 和 event.target( ) 的区别
这两个方法都是监听事件触发的目标。区别是,event.currentTarget( ) 会返回当前触发事件的元素;而event.target( ) 会返回触发事件触发的源头元素。

用法:可以用来监听触发事件的元素是否事件发生的源头元素。这个源头元素指的是,当我点击子元素,虽然父元素的点击事件也会被触发(冒泡机制),但子元素才是事件的源头元素。

手机版Mobile version|Leisure Math Forum

2025-4-21 14:06 GMT+8

Powered by Discuz!

× Quick Reply To Top Return to the list