删除多行的Latex标记

时间:2014-11-01 21:49:03

标签: perl sed latex

我有一个LaTex文档,里面填充了我要删除的标记。假设文档看起来像这样。

Here is some text, we can have inline $math$ symbols and \emph{markup}. 
Sometimes we find offset equations,

\[
  p(\theta|y) \propto p(y|\theta)p(\theta)
\]

And then we return to some more text.

我想删除所有标记,并且我不需要保留包含在标记中的文本。

因此,对于$...$\emph{...}种类的内容,sed -E 's/\$[a-z]+\$//'之类的内容可以正常使用。

我的问题是如何删除跨越多行的方程式。我想删除\[\]之间的所有内容。

1 个答案:

答案 0 :(得分:2)

使用Range Operator ..删除多行中两种模式之间的文本:

use strict;
use warnings;

while (<DATA>) {
    next if /^\s*\\\[/ .. /^\s*\\\]/;
    print;
}

__DATA__
Here is some text, we can have inline $math$ symbols and \emph{markup}. 
Sometimes we find offset equations,

\[
  p(\theta|y) \propto p(y|\theta)p(\theta)
\]

And then we return to some more text.

输出:

Here is some text, we can have inline $math$ symbols and \emph{markup}. 
Sometimes we find offset equations,


And then we return to some more text.

或者作为一个单行:

perl -ne 'next if /^\s*\\\[/ .. /^\s*\\\]/; print' file.tex

开关:

  • -n:为输入文件中的每个“行”创建一个while(<>){...}循环。
  • -e:告诉perl在命令行上执行代码。