Replacing all occurences depending on parity

I want to replace all occurences of '$' in a LaTeX source code in the following way: The dollars on odd position in the file must be changed in '$latex ' and the other dollars can remain the same.

I was thinking to make a script which replaces all odd occurences of a given character with a string, and all even occurences of the same character with another string.

I would be glad to get some help.
Thanks.

Hi.

Can you post a representative sample of your input data, and expected results (output data).

Thanks.

Input:
Solve the equation $x^2=5$. Prove that $x$ is an irrational number.

Output:
Solve the equation $latex x^2=5$. Prove that $latex x$ is an irrational number.

So the 1st, 3rd, ... (2k+1)th dollar becomes '$latex '. The rest remain the same.

awk -F'$' -v OFS="$" '{$2="latex " $2; $4="latex " $4} 1' file1  
Solve the equation $latex x^2=5$. Prove that $latex x$ is an irrational number.

If you have more than 4 $'s you can always do this in a for-loop:

awk -F'$' -v OFS='$' '{ for( I = 2; I <= NF; I+=2 )
  $I = "latex " $I
} 1' file1

Solve the equation $latex x^2=5$. Prove that $latex x$ is an irrational number.

perl:

my $str='$1 first $a second $2 third $b forth $3';
print $str,"\n";
$str=~s/\$
(?=
(?:[^\$]*\$[^\$]*\$)*[^\$]*$)
/-/gx;
print $str,"\n";