Latex语法学习08:打通latex、mathml和word公式转换
目錄
1 基于工具的轉換
?1.1 獲取mathml源碼
?1.2 將mathml代碼轉換為latex
1.3 latex向mathml的轉換
1.4 mathml粘貼到word
1.5 word轉mathml
1.5.1 干法
1.5.2 注意要點
?2 離線工具
2.1 mathml2latex
2.2 latex轉word
2.2.1 一個前端開源項目
2.2.2 一個pyqt的界面程序
2.2.3 使用神器typora
2.3 從word獲取mathml
3 其他參考連接
通過science direct閱讀論文時,看到有別人寫好的公式,不想自己敲怎么辦?這篇小文章帶你快速將生成公式代碼,并粘貼到word中。
首先在SD中隨便打開一篇文章,隨便選擇一個公式,如下圖的約束3:
1 基于工具的轉換
?1.1 獲取mathml源碼
我們可以得到如下源碼:
<math xmlns="http://www.w3.org/1998/Math/MathML"><mtable displaystyle="true" is="true"><mtr is="true"><mtd is="true" /><mtd columnalign="left" is="true"><mrow is="true"><munder is="true"><mo is="true">∑</mo><mrow is="true"><mo is="true">(</mo><mi is="true">i</mi><mo is="true">,</mo><mi is="true">j</mi><mo is="true">)</mo><mo is="true">∈</mo><mi is="true">A</mi></mrow></munder><msubsup is="true"><mi is="true">x</mi><mrow is="true"><mi is="true">i</mi><mi is="true">j</mi></mrow><mi is="true">k</mi></msubsup><mo is="true">−</mo><munder is="true"><mo is="true">∑</mo><mrow is="true"><mo is="true">(</mo><mi is="true">n</mi><mo is="true">+</mo><mi is="true">i</mi><mo is="true">,</mo><mi is="true">j</mi><mo is="true">)</mo><mo is="true">∈</mo><mi is="true">A</mi></mrow></munder><msubsup is="true"><mi is="true">x</mi><mrow is="true"><mi is="true">n</mi><mo is="true">+</mo><mi is="true">i</mi><mo is="true">,</mo><mi is="true">j</mi></mrow><mi is="true">k</mi></msubsup><mo is="true">=</mo><mn is="true">0</mn><mspace width="1em" is="true" /><mo is="true">∀</mo><mi is="true">i</mi><mo is="true">∈</mo><mi is="true">P</mi><mo is="true">,</mo><mo is="true">∀</mo><mi is="true">k</mi><mo is="true">∈</mo><mi is="true">K</mi><mo is="true">,</mo></mrow></mtd></mtr></mtable> </math>?1.2 將mathml代碼轉換為latex
轉換網站
網站的操作也很簡單,步驟如下:
- 將代碼粘貼到左側框內
- 選擇原類型
- 選擇目標類型
- 點擊convert按鈕
?剛才提取的公式轉換結果如下:
\begin{array}{cl} & {\sum\limits_{(i,j) \in A}x_{ij}^{k} - \sum\limits_{(n + i,j) \in A}x_{n + i,j}^{k} = 0\quad\forall i \in P,\forall k \in K,} \\ \end{array}顯示如下:
這樣我們就完美的實現mathml向latex的轉換
1.3 latex向mathml的轉換
同樣是這個網站,我們將原類型和目標類型換換,就可以實現latex到mathml的轉換
1.4 mathml粘貼到word
由于并不是每個人都精通latex,一部分小伙伴還是喜歡word那種簡單粗暴的方式,有沒有辦法將這個公式搞到word里邊嗎?當然有,我們直接把mathml源碼粘貼到word中,然后選擇只保留文本即可。
?不過貌似不太好玩,里邊的空格還需要咱自己拖拖拽拽。沒辦法,能偷的懶已經偷了;如有高手能一步到位,歡迎指教。
1.5 word轉mathml
1.5.1 干法
如果一個公式是word敲的,你還想偷懶直接干成latex,有沒辦法呢?也有,咱慢慢來。
操作也不難,直接選中公式,找個記事本粘貼一下,你會得到如下內容:
<mml:math xmlns:mml="http://www.w3.org/1998/Math/MathML" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math"><mml:mtable><mml:mtr><mml:mtd></mml:mtd><mml:mtd><mml:mrow><mml:munder><mml:mo stretchy="true">∑</mml:mo><mml:mrow><mml:mo>(</mml:mo><mml:mi>i</mml:mi><mml:mo>,</mml:mo><mml:mi>j</mml:mi><mml:mo>)</mml:mo><mml:mo>∈</mml:mo><mml:mi>A</mml:mi></mml:mrow></mml:munder><mml:mrow><mml:msubsup><mml:mrow><mml:mi>x</mml:mi></mml:mrow><mml:mrow><mml:mi>i</mml:mi><mml:mi>j</mml:mi></mml:mrow><mml:mrow><mml:mi>k</mml:mi></mml:mrow></mml:msubsup></mml:mrow></mml:mrow><mml:mo>-</mml:mo><mml:mrow><mml:munder><mml:mo stretchy="true">∑</mml:mo><mml:mrow><mml:mfenced separators="|"><mml:mrow><mml:mi>n</mml:mi><mml:mo>+</mml:mo><mml:mi>i</mml:mi><mml:mo>,</mml:mo><mml:mi>j</mml:mi></mml:mrow></mml:mfenced><mml:mo>∈</mml:mo><mml:mi>A</mml:mi></mml:mrow></mml:munder><mml:mrow><mml:msubsup><mml:mrow><mml:mi>x</mml:mi></mml:mrow><mml:mrow><mml:mi>n</mml:mi><mml:mo>+</mml:mo><mml:mi>i</mml:mi><mml:mo>,</mml:mo><mml:mi>j</mml:mi></mml:mrow><mml:mrow><mml:mi>k</mml:mi></mml:mrow></mml:msubsup></mml:mrow></mml:mrow><mml:mo>=</mml:mo><mml:mn>0</mml:mn><mml:mo>,</mml:mo><mml:mo>?</mml:mo><mml:mi>i</mml:mi><mml:mo>∈</mml:mo><mml:mi>P</mml:mi><mml:mo>,</mml:mo><mml:mo>?</mml:mo><mml:mi>k</mml:mi><mml:mo>∈</mml:mo><mml:mi>K</mml:mi><mml:mo>,</mml:mo></mml:mtd></mml:mtr></mml:mtable></mml:math>?
這一坨是啥,咱也看不懂,也不敢問,干就是了。還是前面那個網站,設置如下:
- 原類型選mathml
- 目標類型選tex
?我們得到了如下tex代碼:
\begin{matrix}& {{\sum\limits_{(i,j) \in A}x_{ij}^{k}} - {\sum\limits_{{({n + i,j})} \in A}x_{n + i,j}^{k}} = 0,\forall i \in P,\forall k \in K,} \\ \end{matrix}?顯示一下這個公式:
也能正常顯示,但是兩個代碼的還是有區別的:
- 使用SD提供的mathml源碼得到的latex用的是array環境
- 使用word干出來的mathml源碼得到的latex用的是matrix環境
1.5.2 注意要點
看我操作的賊溜,有些小伙伴可能從word粘貼公式到記事本,并不是mathml源碼。這里要做一個設置:
?2 離線工具
上面一通操作,都是必須得有網。沒網就瞎了,這時候,我們的python神器就排上用場了。
2.1 mathml2latex
這里要使用一個github的一個開源項目:
GitHub - oerpub/mathconverter: Converts from AsciiMath, LaTeX, MathML to LaTeX, MathMLConverts from AsciiMath, LaTeX, MathML to LaTeX, MathML - GitHub - oerpub/mathconverter: Converts from AsciiMath, LaTeX, MathML to LaTeX, MathMLhttps://github.com/oerpub/mathconverter
實現的源碼也賊簡單:
import os from lxml import etreedef mathml2latex_yarosh(equation):""" MathML to LaTeX conversion with XSLT from Vasil Yaroshevich """xslt_file = os.path.join('xsl_yarosh', 'mmltex.xsl')dom = etree.fromstring(equation)xslt = etree.parse(xslt_file)transform = etree.XSLT(xslt)newdom = transform(dom)return newdommathml = """<mml:math xmlns:mml="http://www.w3.org/1998/Math/MathML" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math"><mml:mtable><mml:mtr><mml:mtd></mml:mtd><mml:mtd><mml:mrow><mml:munder><mml:mo stretchy="true">∑</mml:mo><mml:mrow><mml:mo>(</mml:mo><mml:mi>i</mml:mi><mml:mo>,</mml:mo><mml:mi>j</mml:mi><mml:mo>)</mml:mo><mml:mo>∈</mml:mo><mml:mi>A</mml:mi></mml:mrow></mml:munder><mml:mrow><mml:msubsup><mml:mrow><mml:mi>x</mml:mi></mml:mrow><mml:mrow><mml:mi>i</mml:mi><mml:mi>j</mml:mi></mml:mrow><mml:mrow><mml:mi>k</mml:mi></mml:mrow></mml:msubsup></mml:mrow></mml:mrow><mml:mo>-</mml:mo><mml:mrow><mml:munder><mml:mo stretchy="true">∑</mml:mo><mml:mrow><mml:mfenced separators="|"><mml:mrow><mml:mi>n</mml:mi><mml:mo>+</mml:mo><mml:mi>i</mml:mi><mml:mo>,</mml:mo><mml:mi>j</mml:mi></mml:mrow></mml:mfenced><mml:mo>∈</mml:mo><mml:mi>A</mml:mi></mml:mrow></mml:munder><mml:mrow><mml:msubsup><mml:mrow><mml:mi>x</mml:mi></mml:mrow><mml:mrow><mml:mi>n</mml:mi><mml:mo>+</mml:mo><mml:mi>i</mml:mi><mml:mo>,</mml:mo><mml:mi>j</mml:mi></mml:mrow><mml:mrow><mml:mi>k</mml:mi></mml:mrow></mml:msubsup></mml:mrow></mml:mrow><mml:mo>=</mml:mo><mml:mn>0</mml:mn><mml:mo>,</mml:mo><mml:mo>?</mml:mo><mml:mi>i</mml:mi><mml:mo>∈</mml:mo><mml:mi>P</mml:mi><mml:mo>,</mml:mo><mml:mo>?</mml:mo><mml:mi>k</mml:mi><mml:mo>∈</mml:mo><mml:mi>K</mml:mi><mml:mo>,</mml:mo></mml:mtd></mml:mtr></mml:mtable></mml:math>""" tex = mathml2latex_yarosh(mathml) print(tex)轉換結果如下:
$ \begin{array}{cc}& \sum _{(i,j)\in A}{x}_{ij}^{k}-\sum _{\left(n+i,j\right)\in A}{x}_{n+i,j}^{k}=0,\forall i\in P,\forall k\in K,\end{array}$2.2 latex轉word
2.2.1 一個前端開源項目
可以通過github的一個開源項目可以直接粘貼到word
2.2.2 一個pyqt的界面程序
?生成的mathml代碼:
<math xmlns="http://www.w3.org/1998/Math/MathML" display="inline"><mrow><mi>$</mi><mtable><mtr><mtd columnalign="center" /><mtd columnalign="center"><msub><mo>∑</mo><mrow><mo stretchy="false">(</mo><mi>i</mi><mo>,</mo><mi>j</mi><mo stretchy="false">)</mo><mo>∈</mo><mi>A</mi></mrow></msub><msubsup><mrow><mi>x</mi></mrow><mrow><mi>i</mi><mi>j</mi></mrow><mrow><mi>k</mi></mrow></msubsup><mo>−</mo><msub><mo>∑</mo><mrow><mrow><mo stretchy="true" fence="true" form="prefix">(</mo><mi>n</mi><mo>+</mo><mi>i</mi><mo>,</mo><mi>j</mi><mo stretchy="true" fence="true" form="postfix">)</mo></mrow><mo>∈</mo><mi>A</mi></mrow></msub><msubsup><mrow><mi>x</mi></mrow><mrow><mi>n</mi><mo>+</mo><mi>i</mi><mo>,</mo><mi>j</mi></mrow><mrow><mi>k</mi></mrow></msubsup><mo>=</mo><mn>0</mn><mo>,</mo><mo>∀</mo><mi>i</mi><mo>∈</mo><mi>P</mi><mo>,</mo><mo>∀</mo><mi>k</mi><mo>∈</mo><mi>K</mi><mo>,</mo></mtd></mtr></mtable><mi>$</mi></mrow></math>
2.2.3 使用神器typora
直接在公式上右鍵,復制為mathml源碼
?注意這里的公式不能是行內公式,即通過如下方式插入的公式塊
2.3 從word獲取mathml
word公式中做如下設置:
?相當于之前的擬操作
3 其他參考連接
公式圖片轉latex神器Mathpix以及latex公式與word公式的相互轉換 - 簡書 (jianshu.com)https://www.jianshu.com/p/66ed4c8edfd4
https://binb.droppages.com/https://binb.droppages.com/GitHub - xiaoyvyv/LatexToMathML: Latex轉Word格式的公式Latex轉Word格式的公式. Contribute to xiaoyvyv/LatexToMathML development by creating an account on GitHub.https://github.com/xiaoyvyv/LatexToMathML
總結
以上是生活随笔為你收集整理的Latex语法学习08:打通latex、mathml和word公式转换的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: Dapr Workflow构建块的.NE
- 下一篇: html文本框监听粘贴,JavaScri
