Text parsing pb.

F

fouzi

Hi,

I'm newbie in Perl and I need some help to modify a .tex file;

My file, Toto.tex contains something like :

....
$ a+b$\\ $a$
$\alpha$
\begin{equation}
a+b
\end{equation}
....

And I want to have equations in red. Then, I want :

$\textcolor{red} {a+b}$ $\textcolor{red} {a}$ ~\\
$\textcolor{red} {\alpha}$
\begin{equation}
\textcolor{red} {a+b}
\end{equation}

ie. Transform any $ XXX $ to $\textcolor{red} { XXX }$
any $$ XXX $$ to $$\textcolor{red} { XXX }$$
and any \begin{equation} XXX \end{equation} to \begin{equation}
\textcolor{red} {XXX} \end{equation}


Thank's for any help.

Fawzi.
 
A

Anno Siegel

fouzi said:
Hi,

I'm newbie in Perl and I need some help to modify a .tex file;

My file, Toto.tex contains something like :

...
$ a+b$\\ $a$
$\alpha$
\begin{equation}
a+b
\end{equation}
...

And I want to have equations in red. Then, I want :

$\textcolor{red} {a+b}$ $\textcolor{red} {a}$ ~\\
^^^
What about those backwhacks? How are they prefixed and moved around?
I'll ignore them.
$\textcolor{red} {\alpha}$
\begin{equation}
\textcolor{red} {a+b}
\end{equation}

ie. Transform any $ XXX $ to $\textcolor{red} { XXX }$
any $$ XXX $$ to $$\textcolor{red} { XXX }$$
and any \begin{equation} XXX \end{equation} to \begin{equation}
\textcolor{red} {XXX} \end{equation}

This comes close:

my $single_dollar = qr/(?<!\$)\$(?!\$)/;
my $double_dollar = qr/(?<!\$)\$\$(?!\$)/;
my $begin_eq = qr/\\begin\{equation\}/;
my $end_eq = qr/\\end\{equation\}/;

$text =~ s<$single_dollar(.*?)$single_dollar>
<\$\\textcolor{red}{$1}\$>sg;
$text =~ s<$double_dollar(.*?)$double_dollar>
<\$\$\\textcolor{red}{$1}\$\$>sg;
$text =~ s<$begin_eq(.*?)$end_eq>
<\\begin{equation}\\textcolor{red}{$1}\\end{equation}>sg;
print $text;

Anno
 
F

fouzi

Thanks a lot for the answer.

I use it like this (I'm newbe) :

#!/usr/local/bin/perl -w
my $single_dollar = qr/(?<!\$)\$(?!\$)/;
my $double_dollar = qr/(?<!\$)\$\$(?!\$)/;
my $begin_eq = qr/\\begin\{equation\}/;
my $end_eq = qr/\\end\{equation\}/;
$sortie="";
open(MYFILE, "file.tex") or die "Can't open grades: $!\n";
while ($text = <MYFILE>) {
#print $text;
#$number = <STDIN>;
$text =~ s<$single_dollar(.*?)$single_dollar>
<\$\\textcolor{red}{$1}\$>sg;
$text =~ s<$double_dollar(.*?)$double_dollar>
<\$\$\\textcolor{red}{$1}\$\$>sg;
$text =~ s<$begin_eq(.*?)$end_eq>
<\\begin{equation}\\textcolor{red}{$1}\\end{equation}>sg;
$text =~ s<$begin_eq(.*?)$end_eq>
<\\begin{equation}\\textcolor{red}{$1}\\end{equation}>sg;

$sortie = $sortie . $text;

}
print $sortie;



The problem is that using "while ($text = <MYFILE>) " means to treat
the file line by line.
Thus, if I have something like :
\begin{equation}
a+b
\end{equation}

it doesn't work.
So, is there a better way to do it ?

Thanks again.
 
T

Tad McClellan

fouzi said:
Thanks a lot for the answer.


What answer?

Please post some context in followups like everybody else does.


The problem is that using "while ($text = <MYFILE>) " means to treat
the file line by line.


perldoc -q line

I'm having trouble matching over more than one line. What's wrong?
 
F

fouzi

OK, I repeat :
Anno answered :
my $single_dollar = qr/(?<!\$)\$(?!\$)/;
my $double_dollar = qr/(?<!\$)\$\$(?!\$)/;
my $begin_eq = qr/\\begin\{equation\}/;
my $end_eq = qr/\\end\{equation\}/;

$text =~ s<$single_dollar(.*?)$single_d­ollar>
<\$\\textcolor{red}{$1}\$>sg;
$text =~ s<$double_dollar(.*?)$double_d­ollar>
<\$\$\\textcolor{red}{$1}\$\$>­sg;
$text =~ s<$begin_eq(.*?)$end_eq>
<\\begin{equation}\\textcolor{­red}{$1}\\end{equation}>sg;
print $text;


What I did then, is :

#!/usr/local/bin/perl -w
my $single_dollar = qr/(?<!\$)\$(?!\$)/;
my $double_dollar = qr/(?<!\$)\$\$(?!\$)/;
my $begin_eq = qr/\\begin\{equation\}/;
my $end_eq = qr/\\end\{equation\}/;
$sortie="";
open(MYFILE, "toto.tex") or die "Can't open grades: $!\n";

while ($text = <MYFILE>) {
print $text;

$text =~ s<$single_dollar(.*?)$single_dollar>
<\$\\textcolor{red}{$1}\$>sg;
$text =~ s<$double_dollar(.*?)$double_dollar>
<\$\$\\textcolor{red}{$1}\$\$>sg;
$text =~ s<$begin_eq(.*?)$end_eq>
<\\begin{equation}\\textcolor{red}{$1}\\end{equation}>sg;
$text =~ s<$begin_eq(.*?)$end_eq>
<\\begin{equation}\\textcolor{red}{$1}\\end{equation}>sg;

$sortie = $sortie . $text;


}
print $sortie;



But, what I get is :

....
$\textcolor{red}{ a+b}$\\ $\textcolor{red}{a}$
$\textcolor{red}{\alpha}$
\begin{equation}
a+b
\end{equation}
....

i.e. the file is processed line by line and then,
\begin{equation}
a+b
\end{equation}

is not processed because its on 3 lines.


So, how to resolve this pb. ?

Thank you
 
F

fouzi

Well I found it.
Thank you all for your help !

Then, to read in whole file, not just one line or paragraph :
undef $/; #add this line

$/ = ''; # read in more whole paragraph, not just
one line

For more details see : perldoc -q line
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,244
Latest member
cryptotaxsoftware12

Latest Threads

Top