LaTeX 需要简单的正则表达式

发布于 2024-08-19 16:37:23 字数 881 浏览 5 评论 0原文

在我的 LaTeX 文件中,我确实出现了数千次以下构造:

$\displaystyle{...math goes here...}$

我想将其替换为

\mymath{...math goes here...}

注意 $ 消失,但大括号保留 — 如果不是尾随 $,这将是基本的查找和替换。如果我知道任何正则表达式,我确信它可以毫无问题地处理这个问题。我需要什么正则表达式来实现这一点?

非常感谢。

编辑:出现了一些问题和疑问,所以让我澄清一下:

  1. 是的, $\displaystyle{ ... }$ 可以在同一行上多次出现。
  2. 不可以,不能出现嵌套的 }$(例如 $\displaystyle{...{more math}$...}$)。我的意思是,我想如果你把它放在 \mbox 或其他东西中就可以,但我无法想象为什么有人会在 $\displaystlye{}$ 构造,其目的是显示与文本内联的数学。无论如何,这不是我曾经做过或可能会做的事情。
  3. 我尝试使用 perl 建议,但虽然 shell 没有提出异议,但文件仍然不受影响。
  4. 我尝试使用 sed 建议,但 shell 反对“`('”附近的意外标记。我以前从未使用过 sed(并且“man sed”很迟钝),但这就是我所做的:导航到包含.tex 文件并输入“sed s/\$\\displaystyle({[^}]+})\$/\\mymath\1/g *.tex”,怎么办。我使用 sed 来做我想做的事?

再次,非常感谢所有提供的帮助。

In my LaTeX files, I have literally thousands of occurrences of the following construct:

$\displaystyle{...math goes here...}$

I'd like to replace these with

\mymath{...math goes here...}

Note that the $'s disappear, but the curly braces remain---if not for the trailing $, this would be a basic find-and-replace. If only I knew any regex, I'm sure it would handle this with no problem. What's the regex I need to make this happen?

Many thanks in advance.

Edit: Some issues and questions have arisen, so let me clarify:

  1. Yes, $\displaystyle{ ... }$ can occur multiple times on the same line.
  2. No, nested }$'s (such as $\displaystyle{...{more math}$...}$) cannot occur. I mean, I suppose it could if you put it in an \mbox or something, but I can't imagine why anyone would ever do that inside a $\displaystlye{}$ construct, the purpose of which is to display math inline with text. At any rate, it's not something I've ever done or am likely to do.
  3. I tried using the perl suggestion, but while the shell raised no objections, the files remained unaffected.
  4. I tried using the sed suggestion, but the shell objected to an "unexpected token near `('". I've never used sed before (and "man sed" was obtuse), but here's what I did: navigated to a directory containing .tex files and typed "sed s/\$\\displaystyle({[^}]+})\$/\\mymath\1/g *.tex". No luck. How do I use sed to do what I want?

Again, many many thanks for all offered help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

愛放△進行李 2024-08-26 16:37:23

使用 REGEX 进行此类替换时要非常小心
因为理论上的答案是
REGEX 无法匹配这种类型的模式。

REGEX 是一个有限状态机;它不包含下推堆栈,因此
它不能与嵌套结构一起使用,例如“{...数学在这里...}”如果
存在任何嵌套的可能性,例如“{more math}$”
可以作为“数学在这里”字符串的一部分出现。你至少需要一个
描述此类构造的上下文无关语法 - 状态机
只是没有削减它!

话虽如此,您仍然可以使用 REGEX 来完成此任务
前提是你的“数学在这里”字符串都不比
状态机可以处理什么。

尝试一下......但要注意结果!

Be very careful when using REGEX to do this type of substitution
because the theoretical answer is that
REGEX is incapable of matching this type of pattern.

REGEX is a finite state machine; it does not incorporate a pushdown stack so
it cannot work with nested structures such as "{...math goes here...}" if
there is any possibility of nesting such that something like "{more math}$"
can appear as part of a "math goes here" string. You need at a minimum a
context free grammar to describe this type of construct - a state machine
just doesn't cut it!

Now having said that, you may still be able to pull this off using REGEX
provided none of your "math goes here" strings are more complex than
what a state machine can handle.

Give it a shot.... but beware of the results!

空名 2024-08-26 16:37:23

sed:

s/\$\\displaystyle({[^}]+})\$/\\mymath\1/g

sed:

s/\$\\displaystyle({[^}]+})\$/\\mymath\1/g
少女情怀诗 2024-08-26 16:37:23
perl -pi -e 's/$\\displaystyle({.*)}\$/\\mymath$1}/g' *.tex

如果多个 }$ 在同一行,您需要一个非贪婪版本:

perl -pi -e 's/$\\displaystyle({.*?)}\$/\\mymath$1}/g' *.tex
perl -pi -e 's/$\\displaystyle({.*)}\$/\\mymath$1}/g' *.tex

if multiples }$ are on the same line you need a non greedy version:

perl -pi -e 's/$\\displaystyle({.*?)}\$/\\mymath$1}/g' *.tex
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文