--- loncom/xml/lontexconvert.pm 2005/02/24 09:37:43 1.52 +++ loncom/xml/lontexconvert.pm 2008/09/10 19:45:23 1.89 @@ -1,7 +1,7 @@ # The LearningOnline Network with CAPA # TeX Conversion Module # -# $Id: lontexconvert.pm,v 1.52 2005/02/24 09:37:43 albertel Exp $ +# $Id: lontexconvert.pm,v 1.89 2008/09/10 19:45:23 droeschl Exp $ # # Copyright Michigan State University Board of Trustees # @@ -42,44 +42,39 @@ package Apache::lontexconvert; use strict; use tth(); use vars qw($errorstring); -use Apache(); -use Apache::lonmsg(); -use Apache::lonxml(); -use Apache::lonmenu(); +#use Apache::lonxml(); use Apache::lonlocal; +use Apache::lonnet; +use lib '/home/httpd/lib/perl/'; +use LONCAPA; + # ====================================================================== Header sub init_tth { - my $options=$ENV{'course.'.$ENV{'request.course.id'}.'.tthoptions'}; - if ($ENV{'browser.mathml'}) { + my $options=$env{'course.'.$env{'request.course.id'}.'.tthoptions'}; + if ($options =~ /\S/) { + $options = ' '.$options; + } else { + undef($options); + } + if ($env{'browser.mathml'}) { &tth::ttminit(); - if ($ENV{'browser.unicode'}) { - &tth::ttmoptions('-L -u1 '.$options); + if ($env{'browser.unicode'}) { + &tth::ttmoptions('-L -u1'.$options); } else { - &tth::ttmoptions('-L -u0 '.$options); + &tth::ttmoptions('-L -u0'.$options); } } else { &tth::tthinit(); - if ($ENV{'browser.unicode'}) { - &tth::tthoptions('-L -u1 '.$options); + if ($env{'browser.unicode'}) { + &tth::tthoptions('-L -u1'.$options); } else { - &tth::tthoptions('-L -u0 '.$options); + &tth::tthoptions('-L -u0'.$options); } } } -sub header { - $errorstring=''; - my $time=time; - &init_tth(); - return &Apache::lonxml::xmlbegin(). - "\n
'; $tex=0; + $outmessage.='
'; + $tex=0; } else { - $outmessage.=&to_convert($_); $tex=0; + $outmessage.=&to_convert($fragment); + $tex=0; } } else { - $outmessage.=&smiley($_); $tex=1; + $outmessage.=&smiley($fragment); + $tex=1; + } + } + $message=$outmessage; $outmessage=''; $tex=0; + foreach my $fragment (split(/(?:\<\;|\<)\/*algebra\s*(?:\>\;|\>)/i, + $message)) { + if ($tex) { + if ($email) { + $outmessage.='
'; + $tex=0; + } else { + $outmessage.=&algebra($fragment,'web'); + $tex=0; + } + } else { + $outmessage.=$fragment; + $tex=1; } } if (wantarray) { @@ -215,7 +353,7 @@ sub msgtexconverted { sub algebra { use AlgParser; - my ($string,$target)=@_; + my ($string,$target,$style,$parstack,$safeeval)=@_; my $parser = new AlgParserWithImplicitExpand; $string=&prepare_algebra($string); my $ret = $parser->parse($string); @@ -225,9 +363,16 @@ sub algebra { $parser->normalize(); my $latex=$parser->tolatex(); $latex=&postprocess_algebra($latex); - $latex='$'.$latex.'$'; + if ($style eq 'display') { + $latex='$$'.$latex.'$$x'; + } else { + # style is 'inline' + $latex='\\ensuremath{'.$latex.'}'; + } if ($target eq 'web' || $target eq 'analyze') { - $result = &converted(\$latex); + my $display=&Apache::lonxml::get_param('display',$parstack,$safeeval); + $result = &converted(\$latex,$display); +# $result = &converted(\$latex); } else { $result = $latex; } @@ -240,7 +385,7 @@ sub prepare_algebra { my ($string)=@_; # makes the decision about what is a minus sign easier supposedly - $string =~ s/([\=\>\<\%\!\#] *)-/$1 zeroplace -/g; + $string =~ s/(\<\>|\<\=|\>\=[\=\>\<] *)-/$1 zeroplace -/g; return $string; } @@ -251,17 +396,23 @@ sub postprocess_algebra { # moodle had these and I don't know why, ignoring them for now # $string =~s/\\fun/ /g; - # sqrt(3,4) -> \sqrt\let{3,4\right}, which is annoying - $string =~s/\\left\{/\{/g; - $string =~s/\\right\}/\}/g; - # sqrt(3,4) means the 4 root of 3 $string =~s/\\sqrt{([^,]+),([^\}]+)}/\\sqrt[$2]{$1}/gs; - # remove the extra () in the denominator of a \frac - $string =~s/\\frac{(.+?)}{\\left\((.+?)\\right\)}/\\frac{$1}{$2}/gs; + # log(3,4) means the log base 4 of 3 + $string =~s/\\log\\left\((.+?),(.+?)\\right\)/\\log_{$2}\\left($1\\right)/gs; + + # log(3,4) means the log base 4 of 3 + $string =~s/\\((?:sin|cos|tan|sec|csc|cot)(?:h)?)\\left\((.+?),(.+?)\\right\)/\\$1^{$3}\\left($2\\right)/gs; + + # int(3,a,b) integral from a to b of 3 + $string =~s/\\int\\left\((.+?),(.+?),(.+?)\\right\)/\\int_{$2}^{$3}\\left($1\\right)/gs; + + # int( ... dx) -> ... + $string =~s/\\int\\left\((.+?)d[a-z]\\right\)/$1/gs; - &Apache::lonnet::logthis($string); + # + $string =~s/\\lim\\left\((.+?),(.+?),(.+?)\\right\)/\\lim_{$2\\to $3}$1/gs; return $string; } 1;