使用 Regexp 替换 Latex 文件中的数学表达式

发布于 2024-08-03 07:45:53 字数 1514 浏览 1 评论 0原文

我正在尝试用粗体版本替换数学环境中的字符。不幸的是,这些字符也出现在文本的其余部分中。

我的文字:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

心凉 2024-08-10 07:45:53

仅靠正则表达式这几乎是不可能的。您使用什么语言?如果是 Perl,有一个模块 LaTeX::TOM 可以帮助您解决很多问题。

但是,如果您知道 \begin\end 标记始终位于其自己的行上,则以下伪代码将起作用:

foreach (line in file)
    if line.matches( /\\begin{align}/ )
        inAlign = true
    end

    if inAlign
        line.replace( /(G)/\\mathbf{$1}/ )
    else
        line.replace( /\$(G)\$/\$\\mathbf{$1}\$/ )
    end

    if line.matches( /\\end{align}/ )
        inAlign = false;
    end
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:

foreach (line in file)
    if line.matches( /\\begin{align}/ )
        inAlign = true
    end

    if inAlign
        line.replace( /(G)/\\mathbf{$1}/ )
    else
        line.replace( /\$(G)\$/\$\\mathbf{$1}\$/ )
    end

    if line.matches( /\\end{align}/ )
        inAlign = false;
    end
end
楠木可依 2024-08-10 07:45:53

这将在对齐块中找到 G:

s/(?<=\\begin\{align\})(((?!\\end\{align\}).)*?)G/{$1}\mathbf{G}/g

This will find the Gs within the align blocks:

s/(?<=\\begin\{align\})(((?!\\end\{align\}).)*?)G/{$1}\mathbf{G}/g
心头的小情儿 2024-08-10 07:45:53

要真正解决这个问题,您需要编写一个解析器。解析一般的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文