使用 Regexp 替换 Latex 文件中的数学表达式
我正在尝试用粗体版本替换数学环境中的字符。不幸的是,这些字符也出现在文本的其余部分中。
我的文字:
text text text text Gtext G G text ....
\begin{align}
f&=gG \\
G &= tG
\end{align}
text textG text $G$ text.
里面的每一个G \开始{对齐} \结束{对齐} 以及美元符号之间 $G$ 应替换为
\mathbf{G}。
其他的保持不变。
我很欣赏每一个想法:)
谢谢你
大编辑: 到目前为止,我已经有了一个可以工作的程序(Python),这要归功于 stackoverflow 中的建议和其他一些发现。
但程序将 fe \quad 替换为 \q"replace"ad。如果我想用 "replace" 替换所有 "u" 。
from tempfile import mkstemp
from shutil import move
from os import remove, close
import shutil
def replace(file, outputfile, pattern, subst, boundary1, boundary2):
#Create temp file
fh, abs_path = mkstemp()
newfile="tempfile.tmp"
new_file = open(newfile,'w')
old_file = open(file)
inAlign=False
for line in old_file:
if boundary1 in line:
inAlign = True
if inAlign:
print line
print line.replace(pattern, subst)
new_file.write(line.replace(pattern, subst))
else:
new_file.write(line)
if boundary2 in line:
inAlign = False;
#close temp file
new_file.close()
close(fh)
old_file.close()
shutil.move(newfile,outputfile)
replace("texfile.tex","texfile_copy.tex","G", "\\mathbf{G}", "\\begin{align}", "\\end{align}")
希望我的格式正确......
I am trying to replace characters inside a math environment with their boldface versions. Unfortunately, these characters occur inside the rest of the text, as well.
My text:
text text text text Gtext G G text ....
\begin{align}
f&=gG \\
G &= tG
\end{align}
text textG text $G$ text.
Every G inside
\begin{align} \end{align}
and between the dollar signs
$G$
shall be replaced with
\mathbf{G}.
The others shall remain untouched.
I appreciate every idea :)
Thank you
BIG EDIT:
So far, I have a working Program (Python), thanks to the advice and some other findings in stackoverflow.
But the program replaces f.e \quad to \q"replace"ad. if I want to replace all the "u" s with "replace".
from tempfile import mkstemp
from shutil import move
from os import remove, close
import shutil
def replace(file, outputfile, pattern, subst, boundary1, boundary2):
#Create temp file
fh, abs_path = mkstemp()
newfile="tempfile.tmp"
new_file = open(newfile,'w')
old_file = open(file)
inAlign=False
for line in old_file:
if boundary1 in line:
inAlign = True
if inAlign:
print line
print line.replace(pattern, subst)
new_file.write(line.replace(pattern, subst))
else:
new_file.write(line)
if boundary2 in line:
inAlign = False;
#close temp file
new_file.close()
close(fh)
old_file.close()
shutil.move(newfile,outputfile)
replace("texfile.tex","texfile_copy.tex","G", "\\mathbf{G}", "\\begin{align}", "\\end{align}")
Hopefully I got the formatting right...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
仅靠正则表达式这几乎是不可能的。您使用什么语言?如果是 Perl,有一个模块 LaTeX::TOM 可以帮助您解决很多问题。
但是,如果您知道
\begin
和\end
标记始终位于其自己的行上,则以下伪代码将起作用:This will be hard-to-impossible with regexes alone. What language are you using? It it's perl, there's a module LaTeX::TOM that will help you out a great deal.
However, if you know that your
\begin
and\end
tags are always on their own line, the following pseudocode would work:这将在对齐块中找到 G:
This will find the
G
s within the align blocks:要真正解决这个问题,您需要编写一个解析器。解析一般的 TeX 是一项委婉地称为不平凡的任务(尝试运行 this文件 通过 TeX),但对于典型的 LaTeX 数学表达式,您可以从 matplotlib 的解析器 并破解它以进行您想要的替换。它仍然不是微不足道的,但也不应该是不可克服的。
To really solve this, you need to write a parser. Parsing general TeX is a task that might euphemistically be called nontrivial (try running this file through TeX), but for typical LaTeX math expressions you could start from matplotlib's parser and hack it to do the replacements you want. It still won't be trivial but shouldn't be insurmountable either.