Discussion:
Search & Replace in Math Mode
Jacob Shapiro
2014-08-21 21:45:18 UTC
Permalink
It would be nice to have a search & replace within math mode.
It could work document wide or only in currently focused math-box.

It would allow you to search and replace via latex commands,
for instance, search ^{2} and replace with _{2}.

Maybe the search box itself could have a math-editor so that you don't
have to enter latex.

Can be nice if suddenly in the middle of a calculation you decide you want
to go with another convention for notation.

So far the way I do this is by editing the lyx source file with vim,

so it cannot be _too_ hard..


I guess the main hurdle would be the extra GUI box.
Scott Kostyshak
2014-08-21 22:57:37 UTC
Permalink
Post by Jacob Shapiro
It would be nice to have a search & replace within math mode.
It could work document wide or only in currently focused math-box.
LyX has this. It's called "Advanced Find". You can access it through
the edit menu or with ctrl + shift + F.
Post by Jacob Shapiro
It would allow you to search and replace via latex commands,
for instance, search ^{2} and replace with _{2}.
Maybe the search box itself could have a math-editor so that you don't
have to enter latex.
Can be nice if suddenly in the middle of a calculation you decide you want
to go with another convention for notation.
So far the way I do this is by editing the lyx source file with vim,
so it cannot be _too_ hard..
Actually it's quite hard, from what I understand.

Best,

Scott
Jacob Shapiro
2014-08-21 23:48:46 UTC
Permalink
Thanks for your reponse.

Interestingly, it seems to already sift through math-code,
but the escaping code is somewhat
messy. For example, if you try to replace \hbar with 'word' it
will work, but if you try to replace
'word' with \hbar it will place \\hbar. I bet _that_ cannot be too
hard to fix. Some of the other
escaping is also messy, for example, I wasn't able to get ^{2} to
be replaced with _{2} (even though
this is exactly how it appears in the LyX source). In the longterm,
it would probably be best to have
an ad-hoc search&replace only for math expressions, something
that would be smart enough to
know that ^{2} is equivalent to ^2 and so on, and maybe even tha
t \left( is equivalent to ( (at least
when searching, not when replacing, and of course there are
times when you want this sensitivity...
so it should be switchable).
Richard Heck
2014-08-22 02:35:32 UTC
Permalink
Post by Jacob Shapiro
Thanks for your reponse.
Interestingly, it seems to already sift through math-code,
but the escaping code is somewhat
messy. For example, if you try to replace \hbar with 'word' it
will work, but if you try to replace
'word' with \hbar it will place \\hbar. I bet _that_ cannot be too
hard to fix. Some of the other
escaping is also messy, for example, I wasn't able to get ^{2} to
be replaced with _{2} (even though
this is exactly how it appears in the LyX source). In the longterm,
it would probably be best to have
an ad-hoc search&replace only for math expressions, something
that would be smart enough to
know that ^{2} is equivalent to ^2 and so on, and maybe even tha
t \left( is equivalent to ( (at least
when searching, not when replacing, and of course there are
times when you want this sensitivity...
so it should be switchable).
This is one of the places it is so nice that LyX's own files are just
flat text files. If you
want to do something at all complicated, just open the LyX file in good
a text editor
(back it up first!), then search and replace however you wish. As you
will note, math
code is simply recorded as LaTeX, so it is easy to deal with.

If you want to do something even more complicated, write a Perl (or
python, or sed....)
script and run it on your file. I have often done this.

Note that you do not have to close and re-open the file. Just save it,
then hit File> Revert
to Saved once you have done whatever you need to do. You will quickly
see whether
you did the right thing or need to try again.

Richard

PS I believe this is more or less how Advanced Find and Replace works.
It just does it
all behind the scenes.
Tommaso Cucinotta
2014-08-22 06:40:18 UTC
Permalink
Post by Jacob Shapiro
It would be nice to have a search & replace within math mode.
It could work document wide or only in currently focused math-box.
It would allow you to search and replace via latex commands,
for instance, search ^{2} and replace with _{2}.
Maybe the search box itself could have a math-editor so that you don't
have to enter latex.
Can be nice if suddenly in the middle of a calculation you decide you want
to go with another convention for notation.
you captured exactly the original motivations that led to the birth of Advanced Find and Replace ;-)!

Please, have a look at:

http://wiki.lyx.org/Devel/Advsearch

and let us know whether you find anything else that you feel like missing.

Also, there's a number of known problems of the current implementation, some of which show up as part of:

http://www.lyx.org/trac/query?status=accepted&status=assigned&status=new&status=reopened&description=~&reporter=~&component=search&summary=~&col=id&col=summary&col=reporter&col=status&col=type&col=severity&col=keywords&desc=1&order=id

If you see further problems that are NOT on our Trac bug tracking system, please, post your own enhancements or bug-fixing request.

There's been a couple of attempts of having a GSoC for enhancing the feature

http://wiki.lyx.org/Devel/SummerOfCode2013Ideas
http://wiki.lyx.org/GSoC/GSoCProjectIdeasFor2014

but these projects have not been selected thus far.

Should you have coding skills, then you're very welcome to join and try to improve this feature, as well as others ;-)!

Thanks,

Tommaso
Jim Oldfield
2014-08-22 08:05:31 UTC
Permalink
-----Original Message-----
From: lyx-***@lists.lyx.org [mailto:lyx-***@lists.lyx.org] On Behalf Of
Jacob Shapiro
Sent: 21 August 2014 10:45 PM
To: lyx-***@lists.lyx.org
Subject: Search & Replace in Math Mode
It would allow you to search and replace via latex commands, for instance,
search ^{2} and replace with _{2}.
...
Can be nice if suddenly in the middle of a calculation you decide you want
to go with another convention for notation.

If you know in advance that a notation could potentially change, you could
use LyX's math macro facility. It's very nice and lets you have multiple
parameters which you enter in a visual way. You can even display one thing
in LyX and output a different thing in the LaTeX, which is useful for
simulating the appearance of LaTeX symbols that LyX doesn't know about.

Of course it's not always possible to know in advance, but this is just an
idea in case you're not aware of it.

Jim
Tommaso Cucinotta
2014-08-27 06:13:22 UTC
Permalink
If you know in advance that a notation could potentially change, you could use LyX's math macro facility. It's very nice and lets you have multiple parameters which you enter in a visual way. You can even display one thing in LyX and output a different thing in the LaTeX, which is useful for simulating the appearance of LaTeX symbols that LyX doesn't know about. Of course it's not always possible to know in advance, but this is just an idea in case you're not aware of it. Jim
and, in case you don't know, you can always replace a particular recurrent notation with a LyX segment containing a macro instantiation :-) using Advanced Find & Replace!

(never tried, though)

T.

Loading...