Andrew Stacey  

Mon, 26th Feb 2007 (HowDidIDoThat :: LaTeX) MathGrep  Search and Replace in Mathematics This is a Perl script which does either a search (known in the Unix world as a grep) or a searchandreplace on a LaTeX document looking only in sections of mathematics.
EXAMPLESThese examples are taken from the manpage. Suppose that quantum.tex is \documentclass{article} \newcommand{\texthbar}{\(\hbar\)} \begin{document} The number \(\hbar\) is very small. The product, \(\hbar\hbar\) is miniscule. On the other hand, \(c\), the speed of light is very big. \[ \hbar = \frac{h}{2\pi} \] \begin{myenv} Think of hbar as your friend. \end{myenv} \end{document} Some junk involving \(\hbar\). Then the following examples produce the specified output
%% \newcommand{\texthbar}{\(\hbar\)} %% The number \(\hbar\) is very small. The product, \(\hbar\hbar\) is %% \[ \hbar = \frac{h}{2\pi} \] %% Some junk involving \(\hbar\).
\documentclass{article} \newcommand{\texthbar}{\(\hfoo\)} \begin{document} The number \(\hfoo\) is very small. The product, \(\hfoo\hfoo\) is miniscule. On the other hand, \(c\), the speed of light is very big. \[ \hfoo = \frac{h}{2\pi} \] \begin{myenv} Think of hbar as your friend. \end{myenv} \end{document} Some junk involving \(\hfoo\). Note the The number \(\hfoo\) is very small. The product, \(\hfoo\hbar\) is
Produces no output, but quantum.tex is now the altered text above and quantum.tex.orig is the original.
%% The number \(\hbar\) is very small. The product, \(\hbar\hbar\) is %% \[ \hbar = \frac{h}{2\pi} \] %% Some junk involving \(\hbar\).
%% \newcommand{\texthbar}{\(\hbar\)} %% The number \(\hbar\) is very small. The product, \(\hbar\hbar\) is %% \[ \hbar = \frac{h}{2\pi} \]
%% \begin{myenv} Think of hbar as your friend. \end{myenv}
[Full link] 
