2021年3月10日更新 批量一键转换公式
因为又遇到了不得不往Word里放一大堆公式的作业,一个一个弄太慢了,决定弄一个一键执行的版本,效果如下。

只需要在input.txt里放入LaTeX公式,执行python脚本即可在output.txt里得到Word公式,那么一长串复制粘贴进Word里就是Word里就是Word里的公式,复制粘贴效果:

但是当公式很长的时候,想看一下公式长什么样,可以用通过.tex文件转换,在vscode的LaTeX环境预览公式,如下图那一长串就是对应下面预览的公式:

复制粘贴进Word里的效果看起来不理想,但问题不大的样子,稍微改改就行了(或者在公式里把operatorname改改):

总结
- 复制下列代码
import latex2mathml.converter as converter
word=[]
with open("input.tex", "r") as file:
for text in file:
text = text.split("\n")
for line in text:
if line !="" and line[0] == "$":
equation = line[1:len(line)-1]
word.append(converter.convert(equation))
with open("output.txt", "w") as file:
for ans in word:
ans+="\n\n"
file.write(ans)
import latex2mathml.converter as converter
word=[]
with open("input.txt", "r") as file:
for line in file:
line = line.split("\n")
for equation in line:
if equation != "":
word.append(converter.convert(equation))
with open("output.txt", "w") as file:
for ans in word:
ans+="\n\n"
file.write(ans)
- 把公式放进input.txt或input.tex中
f(I)=I^\gamma
\Omega
f=f(x)
\documentclass[a4paper]{article}
\begin{document}
$ \Omega $
$ \alpha_{u}=\operatorname{softmax}\left(w_{a 2} \tanh \left(W_{a 1} \mathbf{H}_{u}^{T}\right)\right) $
\end{document}
- 运行对应的python脚本
- 从output.txt中逐公式复制粘贴进Word,把下面的这段复制进Word之后就是下面的公式,可以试一下,最后稍微改改就行了
<math display="inline" xmlns="http://www.w3.org/1998/Math/MathML"><mrow><msub><mi>α</mi><mrow><mi>u</mi></mrow></msub><mo>=</mo><mi>\operatorname</mi><mrow><mi>s</mi><mi>o</mi><mi>f</mi><mi>t</mi><mi>m</mi><mi>a</mi><mi>x</mi></mrow><mrow><mo fence="true" form="prefix" stretchy="true">(</mo><mrow><msub><mi>w</mi><mrow><mi>a</mi><mn>2</mn></mrow></msub><mi>\tanh</mi><mrow><mo fence="true" form="prefix" stretchy="true">(</mo><mrow><msub><mi>W</mi><mrow><mi>a</mi><mn>1</mn></mrow></msub><msubsup><mi>𝐇</mi><mrow><mi>u</mi></mrow><mrow><mi>T</mi></mrow></msubsup></mrow><mo fence="true" form="postfix" stretchy="true">)</mo></mrow></mrow><mo fence="true" form="postfix" stretchy="true">)</mo></mrow></mrow></math>

下面是分割线
毕业设计要求翻译一篇英文论文,翻译后要写在word里,无奈论文里公式太复杂,用word的公式编辑器一个一个编太慢了,而且有些符号还找不到。曾经使用过一个工具,可以将LaTeX数学公式直接转换成可以粘贴成word的公式,那个工具还有很多功能,但过了几年忘了叫什么了,找到了一个在线转换的网站,可以供需要将LaTeX打好的数学公式放进word里的人参考。
- 打开网站http://engenharialivre.com/latex-para-word/
- 在第一个框里打数学公式,显示的效果在第二部分,然后复制第三个框内的所有内容,以纯文本的方式粘贴到word里。

- 粘贴到word之后就是word插入公式的格式。

有其它方便的工具记不起来是什么了,留待补充
2020.4.4更新
上面的网站上不去了,而中期答辩将近,需要写报告,latex公式转word急需新工具。
思路是:MathML代码可以直接粘贴进word显示成数学公式,所以只需要一个工具将LaTeX代码转换成MathML就行了。接下来就是使用方法。
- 安装Python3.5以上环境
- 进入latex2mathml包的官网,不进官网可以看这里,首先安装latex2mathml包
pip install latex2mathml - 创建个.py文件,写好下面的代码,然后按需修改引号内的LaTeX代码
import latex2mathml.converter
latex_input = "f(I)=I^\gamma" #latex代码写在这里!!!!!
mathml_output = latex2mathml.converter.convert(latex_input)
print(mathml_output)
- 运行写好的.py文件,将下图的输出结果复制粘贴进word即可

本文介绍了一种批量将LaTeX公式转换为Word公式的解决方案。通过编写Python脚本,将LaTeX公式输入到input.txt,执行脚本后,公式会出现在output.txt中,然后可以直接复制粘贴到Word中。此外,还提到了一个在线工具http://engenharialivre.com/latex-para-word/,但现在该网站已无法访问。更新内容推荐使用latex2mathml将LaTeX转换为MathML,再粘贴到Word中显示为公式。
&spm=1001.2101.3001.5002&articleId=104784081&d=1&t=3&u=86ae5be493c246b38b9d1d0a203a994f)
2万+

被折叠的 条评论
为什么被折叠?



