Andrew Stacey


About
Andrew Stacey
Information about my research, teaching, and other interests.

By: Andrew Stacey
Contact details


Andrew Stacey


blosxom icon


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 search-and-replace on a LaTeX document looking only in sections of mathematics.


  • Download: you can get a copy of the latest version here. The current version is 1.0.
  • Requirements: Perl obviously! It uses the following modules: Term::ANSIColor, Getopt::Long, Pod::Usage, strict. If you don't have any of these they can be downloaded from CPAN.
  • Documentation: the manpage is here. It is contained within the program so in POD format; you can view it using any of the following commands:
    
    mathgrep --man
    pod2text mathgrep
    pod2(something else) mathgrep
    
  • License: this program is released under the GPL. To see the full license, type
    
    mathgrep --gpl
    
  • Limitations. There are two main limitations that I know of but both correspond to bad LaTeX style. The program ignores dollar signs as mathematical delimiters, and its method of determining the start and end of mathematical sections can get it wrong if you do something a little out of the ordinary. See the notes section of the manpage for more details.

EXAMPLES

These 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

mathgrep '/hbar/' quantum.tex

 %---%
 \newcommand{\texthbar}{\(\hbar\)}
 %---%
 The number \(\hbar\) is very small.  The product, \(\hbar\hbar\) is
 %---%
 \[
 \hbar = \frac{h}{2\pi}
 \]
 %---%
 Some junk involving \(\hbar\).

mathgrep --replace -- 's/hbar/hfoo/g' quantum.tex

 \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 g option on the s///, otherwise the line ``The number...'' would have been:

 The number \(\hfoo\) is very small.  The product, \(\hfoo\hbar\) is

mathgrep --replace .orig 's/hbar/hfoo/g' quantum.tex

Produces no output, but quantum.tex is now the altered text above and quantum.tex.orig is the original.

mathgrep --preamble '/hbar/' quantum.tex

 %---%
 The number \(\hbar\) is very small.  The product, \(\hbar\hbar\) is
 %---%
 \[
 \hbar = \frac{h}{2\pi}
 \]
 %---%
 Some junk involving \(\hbar\).

mathgrep --postamble '/hbar/' quantum.tex

 %---%
 \newcommand{\texthbar}{\(\hbar\)}
 %---%
 The number \(\hbar\) is very small.  The product, \(\hbar\hbar\) is
 %---%
 \[
 \hbar = \frac{h}{2\pi}
 \]

mathgrep --noinline --nodisplay --environments myenv '/hbar/' quantum.tex

 %---%
 \begin{myenv}
 Think of hbar as your friend.
 \end{myenv}

[Full link]
Last modified on:
Mon, 26th Feb 2007