[system] / trunk / pg / macros / PGbasicmacros.pl Repository:
ViewVC logotype

Diff of /trunk/pg/macros/PGbasicmacros.pl

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 5688 Revision 5792
1616 $string = $envir{'imagegen'}->add($string); 1616 $string = $envir{'imagegen'}->add($string);
1617 $string; 1617 $string;
1618 } else { 1618 } else {
1619 EV3(@_); 1619 EV3(@_);
1620 } 1620 }
1621}
1622
1623=head3 EV3P
1624
1625 ######################################################################
1626 #
1627 # New version of EV3 that allows `...` and ``...`` to insert TeX produced
1628 # by the new Parser (in math and display modes).
1629 #
1630 # Format: EV3P(string,...);
1631 # EV3P({options},string,...);
1632 #
1633 # `x^2/5` will become \(\frac{x^2}{5}\) and then rendered for hardcopy or screen output
1634 #
1635 # where options can include:
1636 #
1637 # processCommands => 0 or 1 Indicates if the student's answer will
1638 # be allowed to process \{...\}.
1639 # Default: 1
1640 #
1641 # processVariables => 0 1 Indicates whether variable substitution
1642 # should be performed on the student's
1643 # answer.
1644 # Default: 1
1645 #
1646 # processMath => 0 or 1 Indicates whether \(...\), \[...\],
1647 # `...` and ``...`` will be processed
1648 # in the student's answer.
1649 # Default: 1
1650 #
1651 # processParser => 0 or 1 Indicates if `...` and ``...`` should
1652 # be processed when math is being
1653 # processed.
1654 # Default: 1
1655 #
1656 # fixDollars => 0 or 1 Specifies whether dollar signs not followed
1657 # by a letter should be replaced by ${DOLLAR}
1658 # prior to variable substitution (to prevent
1659 # accidental substitution of strange Perl
1660 # values).
1661 # Default: 1
1662 #
1663
1664=cut
1665
1666sub EV3P {
1667 my $option_ref = {}; $option_ref = shift if ref($_[0]) eq 'HASH';
1668 my %options = (
1669 processCommands => 1,
1670 processVariables => 1,
1671 processParser => 1,
1672 processMath => 1,
1673 fixDollars => 1,
1674 %{$option_ref},
1675 );
1676 my $string = join(" ",@_);
1677 $string = ev_substring($string,"\\\\{","\\\\}",\&safe_ev) if $options{processCommands};
1678 if ($options{processVariables}) {
1679 my $eval_string = $string;
1680 $eval_string =~ s/\$(?![a-z])/\${DOLLAR}/gi if $options{fixDollars};
1681 my ($evaluated_string,$PG_eval_errors,$PG_full_errors) =
1682 PG_restricted_eval("<<END_OF_EVALUATION_STRING\n$eval_string\nEND_OF_EVALUATION_STRING\n");
1683 if ($PG_eval_errors) {
1684 my $error = (split("\n",$PG_eval_errors))[0]; $error =~ s/at \(eval.*//gs;
1685 $string =~ s/&/&amp;/g; $string =~ s/</&lt;/g; $string =~ s/>/&gt;/g;
1686 $evaluated_string = $BBOLD."(Error: $error in '$string')".$EBOLD;
1687 }
1688 $string = $evaluated_string;
1689 }
1690 if ($options{processMath}) {
1691 $string = EV3P_parser($string) if $options{processParser};
1692 $string = ev_substring($string,"\\(","\\)",\&math_ev3);
1693 $string = ev_substring($string,"\\[","\\]",\&display_math_ev3);
1694 }
1695 return $string;
1696}
1697
1698#
1699# Look through a string for ``...`` or `...` and use
1700# the parser to produce TeX code for the specified mathematics.
1701# ``...`` does display math, `...` does in-line math. They
1702# can also be used within math mode already, in which case they
1703# use whatever mode is already in effect.
1704#
1705sub EV3P_parser {
1706 my $string = shift;
1707 return $string unless $string =~ m/`/;
1708 my $start = ''; my %end = ('\('=>'\)','\['=>'\]');
1709 my @parts = split(/(``.*?``\*?|`.+?`\*?|(?:\\[()\[\]]))/s,$string);
1710 foreach my $part (@parts) {
1711 if ($part =~ m/^(``?)(.*)\1(\*?)$/s) {
1712 my ($delim,$math,$star) = ($1,$2,$3);
1713 my $f = Parser::Formula($math);
1714 if (defined($f)) {
1715 $f = $f->reduce if $star;
1716 $part = $f->TeX;
1717 $part = ($delim eq '`' ? '\('.$part.'\)': '\['.$part.'\]') if (!$start);
1718 } else {
1719 ## FIXME: use context->{error}{ref} to highlight error in $math.
1720 $part = $BBOLD."(Error: $$Value::context->{error}{message} '$math')".$EBOLD;
1721 $part = $end{$start}." ".$part." ".$start if $start;
1722 }
1723 }
1724 elsif ($start) {$start = '' if $part eq $end{$start}}
1725 elsif ($end{$part}) {$start = $part}
1726 }
1727 return join('',@parts);
1621} 1728}
1622 1729
1623 1730
1624=head2 Formatting macros 1731=head2 Formatting macros
1625 1732

Legend:
Removed from v.5688  
changed lines
  Added in v.5792

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9