简体   繁体   English

Emacs(TeX):如何搜索和替换整个区域?

[英]Emacs (TeX): how to search and replace a whole region?

I bother you to have some tips for this problem: I'm working in Latex with a very dirty code, generated by writer2latex (quite good programme, anyway) and, using Emacs, I'm trying to query-replace multiple lines of code, for instance: 我打扰你有一些关于这个问题的提示:我正在使用一个非常脏的代码在Latex中工作,由writer2latex生成(相当不错的程序),使用Emacs,我正在尝试查询 - 替换多行代码, 例如:

{\centering   [Warning: Image ignored] % Unhandled or unsupported graphics:
%\includegraphics[width=11.104cm,height=8.23cm]{img34}

have to become: 必须成为:

\begin{figure}[tpb]
\begin{center}
\includegraphics[width=\textwidth]{img34}

Using Mx re-builder, I found out that I could underline the whole region I need to query-replace with the string: \\{.*centering.* .*cm] but, if I Mx replace-regexp using this, I only get: Invalid regexp: "Invalid content of \\\\{\\\\}" Any suggestion about how to perform the query? 使用Mx重构器,我发现我可以用字符串: \\{.*centering.* .*cm]来查询整个区域,但是,如果我使用这个替换-regexp,我只得到: Invalid regexp: "Invalid content of \\\\{\\\\}"有关如何执行查询的任何建议? I have a HUGE amount of lines like these to replace... :-) 我有很多像这样的线来代替...... :-)

Make sure the re-syntax is "read", Cc tab . 确保重新语法是“读取”, Cc选项卡 Remove the initial backslash. 删除初始反斜杠。 Now the regexp should work if you yank it into replace-regexp 现在regexp应该可以工作,如果你把它拉到replace-regexp

You're getting this error message because in Emacs' regular expressions the curly braces \\{ and \\} have special meaning. 您收到此错误消息,因为在Emacs的正则​​表达式中,花括号\\{\\}具有特殊含义。 These braces are used to specify that the part of the regexp immediately before the braces should be matched a certain number of times. 这些大括号用于指定紧跟在大括号之前的正则表达式部分应该匹配一定次数。

From the GNU Emacs documentation on regexps: 从regexp上的GNU Emacs 文档

\\{n\\} is a postfix operator specifying n repetitions [...] \\ {n \\}是一个后缀运算符,指定n次重复[...]

\\{n,m\\} is a postfix operator specifying between n and m repetitions [...] \\ {n,m \\}是一个后缀运算符,指定n和m次重复[...]

If you want your regexp to actually match a curly brace, do not escape it with a leading slash: 如果你希望你的正则表达式实际匹配大括号,请不要使用前导斜杠将其转义:

{.*centering.* Cq Cj .*cm] {.*centering.* Cq Cj .*cm]

In order to use a backslash in the replacement string you have to escape it with another backslash. 为了在替换字符串中使用反斜杠,您必须使用另一个反斜杠来转义它。 (When doing this in code, it quickly becomes quite ugly because inside a double-quoted string backslashes themselves have to be escaped already. However, since you are doing your replacements interactively, the double escaping is not necessary and thus two backslashs are enough.) (当在代码中执行此操作时,它很快变得非常难看,因为在双引号字符串中,反斜杠本身必须已经转义。但是,由于您以交互方式进行替换,因此不需要双重转义,因此两个反斜杠就足够了。 )

MC-% {.*centering.* Cq Cj .*cm] RET \\\\begin{figure}[tpb] Cq Cj \\\\begin{center} Cq Cj \\\\includegraphics[width=\\\\textwidth] RET MC-% {.*centering.* Cq Cj .*cm] RET \\\\begin{figure}[tpb] Cq Cj \\\\begin{center} Cq Cj \\\\includegraphics[width=\\\\textwidth] RET

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM