一个典型的乳胶问题:
\SomeStyle{\otherstyle{this is the \textit{nested part} some more text...}}
现在我想删除所有\SomeStyle{...}
内容但不删除内容。内容包含嵌套括号。上面的行应改为:
\otherstyle{this is the \textit{nested part} some more text...}
问题:
- 是否有任何 Latex 编辑器可以提供此方法?
- 什么编辑器/脚本可以做到这一点?
- 如何用 sed 来实现?[🤓]
我的解决方案是使用 sed 的 bash 脚本。
- 准备文本:用 ascii 铃声标记替换字符串,在每个括号后添加换行符
- 循环:查找 { -> 将 X 添加到保持空间,查找 } -> 从保持空间中移除 X,保持空间为空 -> 移除关闭 }
- 恢复换行符和 ascii 铃声到以前的
脚本可以运行但会失败:
\badstyle{w}\badstyle{o}\badstyle{r}\badstyle{d}
它将变成:
wo}rd}
分支到 :f 似乎不起作用。
F=$(sed 's|\\|\\\\|g;s|{|\\{|g' <<< "$1" )
# mark all removestrings with ascii bell and newline
# add newline after each { and }
SEDpre='
s|'"$F"'|\a%\n|g
s|\{|\{\n|g
s|\}|\}\n|g
'
SEDpost='
:a;N;$!ba;
s|\a%\n||g
s|\{\n|\{|g
s|\}\n|\}|g
'
# count the brackets
SED='
/\a%/{
:a
n
:f
/\{/{x;s|$|X|;x;ba}
/\}/{x;
s|X||;
/^$/{x;bb}
x
ba
}
}
b
:b
/\}/{
s|\}||;
N;
s|\n||;
/\a%/bf
}
'
sed -r -E "$SEDpre" "$2" | sed -rE "$SED" | sed -rE "$SEDpost"