我尝试使用emacs来转换表单的字符串" $$ ... $$"进入" \ [... \]"。我刚开始学习如何使用emacs来编辑某些文件。
在emacs wiki上关注replace-regexp的first example之后,我尝试了一下:
M-x replace-regexp
Replace regexp: \$\$.*\$\$
Replace regexp with: \,("\[" \1 "\]")
但我收到错误:invalid function "["
。我改为尝试了
Replace regexp with: "\["\1"\]"
但我收到错误:Invalid use of `\' in replacement text
。
由于这两种方法都不起作用,我尝试在emacs wiki上修改了13th example的replace-regexp,写作
M-x replace-regexp
Replace regexp: \$\$
Replace regexp with: \,(if (evenp \#) "\[" "\]")
但我收到错误:(void-function evenp)
。有什么建议?我想了解每个实例中出现的问题,以及如何修复它们。
答案 0 :(得分:3)
您不需要宏。
(replace-regexp "\\$\\$\\(.*?\\)\\$\\$"
"\\\\[\\1\\\\]")
或者
Replace regexp: \$\$\(.*?\)\$\$
Replace with: \\[\1\\]
更新
使用贪婪的语法.*?
来处理一行中出现多次的情况(来自@ phil'以下评论)。
请注意,当$$
之间的文字跨越多行时,这不起作用。如果文本分布在多行中,则以下内容应该有效,不过在这一点上我可能更喜欢滚动自己的函数:
(replace-regexp "\\$\\$\\(\\(.\\|\n\\)*?\\)\\$\\$"
"\\\\[\\1\\\\]")
或者
替换正则表达式:\$\$\(\(.\|
C-q C-j \)*?\)\$\$
替换为:\\[\1\\]