I have a tricky problem that I'm not sure how to solve. I have a latex
document with several figure environments that look like this:
\begin{figure}
\includegraphics{blah.ps}
\end{figure}
but need to look like this:
\begin{figure}
\begin{center} \includegraphics{blah.ps} \end{center}
\end{figure}
Some of the figure environments already have this however, and I don't want
to end up with something like this:
\begin{figure}
\begin{center}\begin{center} \includegraphics{blah.ps}
\end{center}\end{center}
\end{figure}
Any ideas? Also, is there a way Emacs can just wrap any selection with
custom, predefined tags?
thanks,
m121212
--
View this message in context: http://old.nabble.com/regexp-emacs-selective-replace-tp26478209p26478209.html
Sent from the Emacs - Help mailing list archive at Nabble.com.
> Hi,
>
> I have a tricky problem that I'm not sure how to solve. I have a latex
> document with several figure environments that look like this:
>
> \begin{figure}
> \includegraphics{blah.ps}
> \end{figure}
>
> but need to look like this:
>
> \begin{figure}
> \begin{center} \includegraphics{blah.ps} \end{center}
> \end{figure}
>
> Some of the figure environments already have this however, and I don't want
> to end up with something like this:
>
> \begin{figure}
> \begin{center}\begin{center} \includegraphics{blah.ps}
> \end{center}\end{center}
> \end{figure}
>
> Any ideas?
Does query-replace-regexp do what you want? It will prompt you before
each possible replacement, and you can say whether to replace that
instance.
--
Barry Margolin, bar...@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
> Hi,
>
> I have a tricky problem that I'm not sure how to solve. I have a latex
> document with several figure environments that look like this:
>
> \begin{figure}
> \includegraphics{blah.ps}
> \end{figure}
>
> but need to look like this:
>
> \begin{figure}
> \begin{center} \includegraphics{blah.ps} \end{center}
> \end{figure}
>
> Some of the figure environments already have this however, and I don't want
> to end up with something like this:
>
> \begin{figure}
> \begin{center}\begin{center} \includegraphics{blah.ps}
> \end{center}\end{center}
> \end{figure}
>
> Any ideas? Also, is there a way Emacs can just wrap any selection with
> custom, predefined tags?
Anchor to the beginning and end of line.
M-x replace-regexp RET
^ *\\includegraphics{[^}]*} *$ RET
\\begin{center} \& \\end{center} RET
M-x replace-regexp RET
^ *\\includegraphics{[^}]*} *$ RET
\\begin{center} \& \\end{center} RET
Thanks,
m121212
--
View this message in context: http://old.nabble.com/regexp-emacs-selective-replace-tp26478209p26511406.html
(info "(emacs) Regexps")
> Also, could
> you explain this part: {[^}]*} *$ RET ?
see the above info node. ;-) { and the final } are just literal characters,
[...] describes a set of alternatives. ^ negates such a set, so that [^}]
matches any character other than }.
note that {[^}]*} could also have been written as {.*?} (which IHMO is a bit
clearer and easier to read).
--
Joost Kremers joostk...@yahoo.com
Selbst in die Unterwelt dringt durch Spalten Licht
EN:SiS(9)
> Thanks for your repsonses! The solution for me in the end was just to record
> a keyboard macro. Do you use a particular guide for regexps? Also, could
> you explain this part: {[^}]*} *$ RET ?
{[^}]*} *$ means
{ first an opening brace,
[^}]* then any number of characters that are not a closing brace
} then a closing brace,
* any number of spaces,
$ and the line ends there
RET RETURN to proceed
This matches {xxx}
but not {xxx} \end{center} because the line does not end after the brace.
and because there would be a } in the matching string.
> note that {[^}]*} could also have been written as {.*?} (which IHMO is a bit
> clearer and easier to read).
strictly speaking {[^}]*} is not the same as {.*?}
The first will match
{xxx
}
because emacs regexp [^...] matches newline if it is not included in the
brackets. By the way {.*?} is probably closer to what the OP asks for.
when it comes to regexps, all that counts is "strictly speaking". ;-)
> The first will match
>
> {xxx
> }
>
> because emacs regexp [^...] matches newline if it is not included in the
> brackets.
yup, true. forgot about that...
This page answers your question exactly:
• Elisp Lesson: Repeated Find Replace
http://xahlee.org/emacs/elisp_repeat_replace.html
if you prefer emacs to ask you for each case, perhaps just to be sure
your regex didn't find a bad match, see:
• Lisp Lesson: Regex Replace with a Function
http://xahlee.org/emacs/lisp_regex_replace_func.html
> Any ideas? Also, is there a way Emacs can just wrap any selection with
> custom, predefined tags?
(defun wrap-markup ()
"Insert a markup <b></b> around a region."
(interactive)
(goto-char (region-end)) (insert "</b>")
(goto-char (region-beginning)) (insert "<b>")
)
the above is from a collection of simple elisp examples at
• Emacs Lisp Examples
http://xahlee.org/emacs/elisp_examples.html
Xah
∑ http://xahlee.org/
☄