Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 23 additions & 15 deletions lib/LaTeXML/Package/pgfmath.code.tex.ltxml
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,9 @@ sub pgfmathparse {
Let('\widthof', '\pgfmath@calc@widthof');
Let('\heightof', '\pgfmath@calc@heightof');
Let('\depthof', '\pgfmath@calc@depthof');
my $string = (ref $tokens ? UnTeX(Expand($tokens)->stripBraces) : $tokens);
my $string = (ref $tokens ? UnTeX(Expand($tokens)->stripBraces, 1) : $tokens);
# Normalize spaces and remove braces
$string =~ s/[}{]/ /g;
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is probably incorrect, because curly braces have a meaning in PGF. We'll have to come back to this later.

$string =~ s/^\s+//; $string =~ s/\s+$//; $string =~ s/\s+/ /gs;
my $input = $string;
# simple number-like thing? return as-is without any changes.
Expand Down Expand Up @@ -615,10 +617,10 @@ BEGIN {
frac => sub { $_[0] < 0 ? ceil($_[0]) - $_[0] : $_[0] - floor($_[0]); },
gcd => sub {
# PGF computes the gcd of the underlying TeX integers!
my ($a, $b) = (abs($_[0]) * 65536, abs($_[1]) * 65536);
($a, $b) = ($b, $a) if $b > $a;
while ($b > 0) { ($a, $b) = ($b, $a % $b); }
int($a / 65536); },
my ($lhs, $rhs) = (abs($_[0]) * 65536, abs($_[1]) * 65536);
($lhs, $rhs) = ($rhs, $lhs) if $rhs > $lhs;
while ($rhs > 0) { ($lhs, $rhs) = ($rhs, $lhs % $rhs); }
int($lhs / 65536); },
# height => sub { },
hex => sub { sprintf("%x", $_[0]); },
Hex => sub { sprintf("%X", $_[0]); },
Expand All @@ -638,13 +640,18 @@ BEGIN {
# random => sub { },
real => sub { $_[0] + 0.0; },
# rnd => sub { },
round => sub { round($_[0]); },
scalar => sub { SetCondition(T_CS('\ifpgfmathunitsdeclared'), 0, 'global'); $_[0]; },
sec => sub { sec(pgfmathargradians($_[0])); },
sign => sub { ($_[0] > 0 ? 1 : ($_[0] < 0 ? -1 : 0)); },
sin => sub { sin(pgfmathargradians($_[0])); },
sinh => sub { sinh($_[0]); },
sqrt => sub { sqrt($_[0]); },
round => sub { round($_[0]); },
scalar => sub { SetCondition(T_CS('\ifpgfmathunitsdeclared'), 0, 'global'); $_[0]; },
sec => sub { sec(pgfmathargradians($_[0])); },
sign => sub { ($_[0] > 0 ? 1 : ($_[0] < 0 ? -1 : 0)); },
sin => sub { sin(pgfmathargradians($_[0])); },
sinh => sub { sinh($_[0]); },
sqrt => sub {
if ($_[0] >= 0) {
return sqrt($_[0]); }
else {
Error("pgfmath", "sqrt", undef, "Undefined sqrt on negative number: " . $_[0]);
return $_[0]; } },
subtract => sub { $_[0] - $_[1]; },
tan => sub { tan(pgfmathargradians($_[0])); },
tanh => sub { tanh($_[0]); },
Expand All @@ -670,7 +677,7 @@ BEGIN {

# Why can't I manage to import a few functions to be visible to the grammar actions?
# NOTE Not yet done: quoted strings, extensible functions
$PGFMATHGrammarSpec = << 'EoGrammar';
$PGFMATHGrammarSpec = <<'EoGrammar';
# {BEGIN { use LaTeXML::Package::Pool; }}
# { use LaTeXML::Package::Pool; }
# { LaTeXML::Package::Pool->import(qw(pgfmath_apply)); }
Expand Down Expand Up @@ -751,6 +758,7 @@ register : REGISTER postRegister[$item[1]]
/^\Z/ { $arg[0]; } # short circuit!
# really count_register dimension_register!
| REGISTER { LaTeXML::Package::Pool::pgfmath_apply('*', $item[1], $item[2]); }
| { $arg[0]; }

REGISTER : # these need to set dimension flag!!!
'\wd' CS { LaTeXML::Package::Pool::pgfmath_setunitsdeclared();
Expand All @@ -775,9 +783,9 @@ NUMBER :
UNIT :
/(?:ex|em|pt|pc|in|bp|cm|mm|dd|cc|sp)/

FUNCTION0 : /([a-zA-Z][a-zA-Z0-9]*)/ { LaTeXML::Package::Pool::pgfmath_checkuserconstant($item[1]); }
FUNCTION0 : /([a-zA-Z][a-zA-Z0-9_]*)/ { LaTeXML::Package::Pool::pgfmath_checkuserconstant($item[1]); }

FUNCTION : /([a-zA-Z][a-zA-Z0-9]*)/ { LaTeXML::Package::Pool::pgfmath_checkuserfunction($item[1]); }
FUNCTION : /([a-zA-Z][a-zA-Z0-9_]*)/ { LaTeXML::Package::Pool::pgfmath_checkuserfunction($item[1]); }

# ? array|scalar
# These take boxes!
Expand Down
Binary file modified t/pgfmathparse/pgfmathparse.pdf
Binary file not shown.
3 changes: 2 additions & 1 deletion t/pgfmathparse/pgfmathparse.tex
Original file line number Diff line number Diff line change
Expand Up @@ -190,5 +190,6 @@ \subsection{Additional predecence tests}
\test{-3!}
\test{2 * - 2 * 3!}


\subsection{Delimiters and grouping}
\test[wrapping braces]{-{sqrt( (-3.25)^6 ) }}
\end{document}
11 changes: 11 additions & 0 deletions t/pgfmathparse/pgfmathparse.xml
Original file line number Diff line number Diff line change
Expand Up @@ -530,5 +530,16 @@
<p><text font="typewriter">2 * - 2 * 3! = -24.0 = -24.0</text></p>
</para>
</subsection>
<subsection inlist="toc" xml:id="S3.SS10">
<tags>
<tag>3.10</tag>
<tag role="refnum">3.10</tag>
<tag role="typerefnum">§3.10</tag>
</tags>
<title><tag close=" ">3.10</tag>Delimiters and grouping</title>
<para xml:id="S3.SS10.p1">
<p><text font="typewriter">wrapping braces = -34.32812 = -34.32812</text></p>
</para>
</subsection>
</section>
</document>