[OS X TeX] Find and replace with regular expressions

Ross Moore ross at ics.mq.edu.au
Mon Nov 19 21:22:04 CET 2007

Hello Alessandro,

On 19/11/2007, at 11:20 PM, Alessandro Andretta wrote:

> I would like to replace the $ signs in a LaTeX file with \( and \)
> thus I would have to search for $...$ and replace it with \( ... \)
> where ... is any string of symbols (letters, numbers, \ , {} &,etc)
> EXCEPT $. I tried the help menu of TeXShop, TextMate, Textwrangler,
> but their general treatment seems be a bit too technical for my taste.

That is because this is a very difficult thing to do automatically
and reliably.

> Is there a simple way to archive this with any of the editors above?

Probably not.
There have been many replies and hints at the difficulty.

As yet none has mentioned patterns like:

      $...math... \mbox{...text...$...$...} ...more math... $

or setting a table inside mathematics, or a table that uses
math-delimiters in the column-spec part.
Then there is the case of $ occurring within verbatim environments,
or \verb+....+ constructions; e.g. for coding examples.

There is also the problem of unmatched $s due to %-comments on
lines within your LaTeX source.

Also, if you have $...$  delimiters, then you probably also
have  $$....$$  displays. So you must cope with these first,
before handling the single-dollar delimiters.
Alternatively, cope with the resulting  $\(.....\)$ afterwards.

LaTeX2HTML has approx 150 lines of Perl coding, with comments,
in the form of two subroutines:

    sub wrap_shorthand_environments
    sub wrap_math_environments

devoted to correctly recognising math environments, whether
the delimiters be $...$ or \(....\)  or $$....$$  or  \[....\] .

But this coding is not stand-alone, needing a detailed knowledge
of nesting levels of {....}s, and all the %-comments having been
replaced already.

If you are serious about doing this, then IMHO it is best to
use 'Find' and 'Replace & Find Next', looking at each instance
to verify that you are indeed making the correct delimiter
replacements for the specific math-environment.

You could perhaps speed this up by first handling the more
complicated cases, such as the ones mentioned above.
Only then try tackling the bulk of your coding...

  ... and expect to make errors, which will then need to be
found and fixed.

> Thanks in advance,
> 	--aa

Hope this helps,

	Ross Moore

Ross Moore                                         ross at maths.mq.edu.au
Mathematics Department                             office: E7A-419
Macquarie University                               tel: +61 +2 9850 8955
Sydney, Australia  2109                            fax: +61 +2 9850 8114

More information about the macostex-archives mailing list