## LaTeX forum ⇒ Kile ⇒ searching in math mode only

Information and discussion about Kile, an integrated LaTeX environment for Linux KDE
mf205
Posts: 7
Joined: Mon May 25, 2009 12:18 pm

### searching in math mode only

When I do search-and-replace in Kile (or any other editor which recognises TeX markup), can I tell it to search in math mode only? I have a variable called $e$ that I want to change to $n$, but obviously searching for all occurrences of the letter e is not efficient.

localghost
Site Moderator
Posts: 9203
Joined: Fri Feb 02, 2007 12:06 pm
Just search for $e$ and replace it with $n$ within your editor.

Best regards
Thorsten
LaTeX Community Moderator

¹ System: openSUSE 42.2 (Linux 4.4.52), TeX Live 2016 (vanilla), TeXworks 0.6.1

mf205
Posts: 7
Joined: Mon May 25, 2009 12:18 pm
Thanks for the reply, but this will only work when e appears by itself; it won't change $e+1$ to $n+1$, for example.

frabjous
Posts: 2064
Joined: Fri Mar 06, 2009 12:20 am
Putting the search mode to "regular expressions" and searching for:

^[^\$]*\$[^\$]*e[^\$]*\$will find any e's within the first set of$ ... $on a given line, but it'll still find things like the e in$\epsilon$, so it's not safe to use as part of a Find and Replace. If you use$...$more than once on a given line it won't find those. (I could alter it so it does, but then you'd also find things between the$ that ends the first mathmode and the $that begins the second which isn't useful.) There may be ways to improve that though, especially with more detailed knowledge about exactly in what contexts your "e" may appear... but I don't know of a search mechanism especially for math mode. localghost Site Moderator Posts: 9203 Joined: Fri Feb 02, 2007 12:06 pm mf205 wrote:Thanks for the reply, but this will only work when e appears by itself; it won't change$e+1$to$n+1$, for example. You should have specified that correctly right at the beginning. You only talked about$e$and not about the variable e in math expressions. But perhaps I misunderstood something. LaTeX Community Moderator ¹ System: openSUSE 42.2 (Linux 4.4.52), TeX Live 2016 (vanilla), TeXworks 0.6.1 ntcarruth Posts: 1 Joined: Fri Apr 30, 2021 9:07 pm frabjous wrote:Putting the search mode to "regular expressions" and searching for: ^[^\$]*\$[^\$]*e[^\$]*\$

will find any e's within the first set of $...$ on a given line, but it'll still find things like the e in $\epsilon$, so it's not safe to use as part of a Find and Replace. If you use $...$ more than once on a given line it won't find those. (I could alter it so it does, but then you'd also find things between the $that ends the first mathmode and the$ that begins the second which isn't useful.)

There may be ways to improve that though, especially with more detailed knowledge about exactly in what contexts your "e" may appear... but I don't know of a search mechanism especially for math mode.

Just a note that with extended regular expressions one can find all occurrences of e in math mode on the line via the following simple modification:

^([^\$]*\$[^\$]*e[^\$]*\\$)+

On the other hand, this won’t find occurrences of e occurring in display math mode, and I haven’t found any way of finding those using just a search with extended regular expressions (though it might be possible).

Alternatively, if you are in a position like me and have to change most occurrences of n inside any kind of math mode in a very long file, it is possible to write a fairly short script using awk, egrep and sed to extract everything in math mode and display math mode, together with original file line numbers, into separate files which you can then search — though this does mean that one’s searching and replacing have to be done in different files. If anybody is interested I can post my version here.