user1106732
user1106732

Reputation: 3

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:

{\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 M-x 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 M-x replace-regexp using this, I only get: Invalid regexp: "Invalid content of \\{\\}" Any suggestion about how to perform the query? I have a HUGE amount of lines like these to replace... :-)

Upvotes: 0

Views: 549

Answers (2)

Thomas
Thomas

Reputation: 17422

You're getting this error message because in Emacs' regular expressions the curly braces\{ and \} have special meaning. 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:

\{n\} is a postfix operator specifying n repetitions [...]

\{n,m\} is a postfix operator specifying between n and m repetitions [...]

If you want your regexp to actually match a curly brace, do not escape it with a leading slash:

{.*centering.*C-q C-j.*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.)

M-C-% {.*centering.*C-q C-j.*cm] RET \\begin{figure}[tpb]C-q C-j\\begin{center}C-q C-j\\includegraphics[width=\\textwidth] RET

Upvotes: 1

event_jr
event_jr

Reputation: 17707

Make sure the re-syntax is "read", C-c tab. Remove the initial backslash. Now the regexp should work if you yank it into replace-regexp

Upvotes: 1

Related Questions