Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Possible Duplicate:
replace often used Tex-Literals ($ and $$) for math regions into \( or \) and \[ or \]

When I started using latex, I wrote mathematical formulae separated from continuous text using $$...$$. I have read the recommendations from doc l2tabuen

Replace: $$...$$ by \[...\].

Because in my case it is a bigger project composed of many files, this can not be done manually. I think such a script in Python would be very appropriate. Maybe someone solved a similar problem.

share|improve this question

marked as duplicate by Loop Space, lockstep, Thorsten, cgnieder, Stefan Kottwitz Nov 25 '12 at 13:45

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

3  
Python, or any language you're fluent with... Basically all you have to do is count the $$ and replace odd ones with \[ and even ones with \] (hoping you never used the dirty $$ to create an empty object). –  T. Verron Nov 25 '12 at 11:08

1 Answer 1

up vote 11 down vote accepted

Here is a simple script in perl that is a stripped down version of a script I have used on a number of occaisions. It makes no changes before the \begin{document} line and then replaces $$ by alternately \[ and \] placed on lines of their own.

#! /usr/bin/env perl

use warnings;
use strict;

my $indoc = 0;
my $indisplaymath = 0;
my @dsubs;
$dsubs[0] = '\\[';
$dsubs[1] = '\\]';

while (<>) {
  if (/\\begin{document}/) {
    $indoc = 1;
  }
  if ($indoc == 1) {
    while (/\s*\${2}\s*/p) {
      my $pre = '';
      my $post = '';
      if (${^PREMATCH} ne '') { $pre = "\n"; }
      $_ =~ s/\s*\${2}\s*/$pre$dsubs[$indisplaymath]\n/;
      $indisplaymath = 1 - $indisplaymath;
    }
  }
  print;
}

Save this as a script e.g. latexdisp.pl, give it execute permissions with chmod +x latexdisp.pl and run as latexdisp.pl file.tex >out.tex. Finally, double check the output.

For example on

\documentclass[12pt]{article}
\begin{document}
Test
$$ x = \int_0^3 y(t)\, dt $$ testing
$ p/q $

$$
  e = t
$$
\end{document}

the output is

\documentclass[12pt]{article}
\begin{document}
Test
\[
x = \int_0^3 y(t)\, dt
\]
testing
$ p/q $

\[
  e = t
\]
\end{document}

Modifying the strings in the $dsubs[0] and $dsubs[1] you could change the replacement to get e.g. \begin / \end{equation*} pairs. Remeber to quote the backslashes.

share|improve this answer
1  
+1 great answer :) since this question has been closed, should we ask the moderators to move your answer to the other question? Of course, we'd have to make sure that your rep is preserved... –  cmhughes Nov 25 '12 at 17:46
    
Thanks, It helped me very. –  jafan Nov 25 '12 at 20:39
    
@cmhughes Thanks. Looking at the other question I see that (a) my solution does not address the single dollar request there and (b) it is essentially the same idea as Andrew Stacey's code with much the same limitations. If despite that, you and others consider my solution better placed there, that would be OK with me. –  Andrew Swann Nov 26 '12 at 10:53
    
That's ok; if your answer is left here, it won't receive much attention though as the question has been closed. I think you have enough rep to flag your answer and ask a Moderator if it can be moved to the answer with the duplicated question (if you think that is appropriate). Of course, it's entirely up to you, but I think your answer should be seen by a wider audience –  cmhughes Nov 26 '12 at 17:31

Not the answer you're looking for? Browse other questions tagged or ask your own question.