【发布时间】:2021-03-30 20:22:42
【问题描述】:
我有一个包含 LaTeX 数学方程式的 TXT 文件,其中在每个内联方程式之前和之后使用单个 $ 分隔符。
我想在一个段落中找到每个方程,并用 XML 开始和结束标记替换分隔符 ....
例如,
以下段落:
This is the beginning of a paragraph $first equation$ ...and here is some text... $second equation$ ...and here is more text... $third equation$ ...and here is yet more text... $fourth equation$
应该变成:
This is the beginning of a paragraph <equation>first equation</equation> ...and here is some text... <equation>second equation</equation> ...and here is more text... <equation>third equation</equation> ...and here is yet more text... <equation>fourth equation</equation>
我已经尝试过如下的 sed 和 perl 命令:
perl -p -e 's/(\$)(.*[^\$])(\$)/<equation>$2<\/equation>/'
但是这些命令会导致方程的第一个和最后一个实例被转换,但没有这两个方程之间的方程:
This is the beginning of a paragraph <equation>first equation$ ...and here is some text... $second equation$ ...and here is more text... $third equation$ ...and here is yet more text... $fourth equation</equation>
我还想要一个强大的解决方案,它可以考虑到不用作 LaTeX 分隔符的单个 $ 的存在。例如,
This is the beginning of a paragraph $first equation$ ...and here is some text that includes a single dollar sign: He paid $2.50 for a pack of cigarettes... $second equation$ ...and here is more text... $third equation$ ...and here is yet more text... $fourth equation$
不会变成:
This is the beginning of a paragraph <equation>first equation$ ...and here is some text that includes a single dollar sign: He paid <equation>2.50 for a pack of cigarettes... $second equation$ ...and here is more text... $third equation$ ...and here is yet more text... $fourth equation</equation>
注意:我正在用 Bash 写作。
【问题讨论】:
-
我对LaTeX不熟悉,但我猜公式里面没有空格吧?
-
@PedroMaimere LaTeX 数学表达式的
$...$内可以有空格。 -
有什么东西可以触发美元符号(不)是否属于公式?
-
很遗憾没有,我想不到。有时在第一个 $ 之后会立即出现 LaTeX 命令(例如 \frac),但并非总是如此。有时只有数字、括号或文本,所有这些都可以在用于不同目的的 $ 之后找到...... LaTeX 方程与 $ 的其他用途的区别在于,方程总是包含在 $ 中,而单个 $ 可以用于其他目的。在这种情况下,很难排除误报。
-
粗略搜索 (la)tex、美元符号、方程式...找到了一些参考资料,也许 OP 可以评论:1) 使用
\(...\)而不是 @987654329 @ 指定方程,2) 转义独立的$(即\$)以将其指定为文字$;这些(现实的)选项中的任何一个是否可用于帮助确定应如何处理$?