Skip to content

Commit

Permalink
Everymath (brucemiller#2461)
Browse files Browse the repository at this point in the history
* Use \everymath, \everydisplay correctly (latter without former)

* Update test case to test \everymath,\everydisplay

* Use \everymath,\everydisplay as part of Stomach->setMode

* Math entering commands no longer need to handle \everymath,\everydisplay, just beginMode appropriately

* Update and enrich \everymath,\everydisplay tests
  • Loading branch information
brucemiller authored Dec 28, 2024
1 parent 74c0e05 commit f82b733
Show file tree
Hide file tree
Showing 6 changed files with 207 additions and 40 deletions.
11 changes: 8 additions & 3 deletions lib/LaTeXML/Core/Stomach.pm
Original file line number Diff line number Diff line change
Expand Up @@ -364,13 +364,18 @@ sub setMode {
# and save the text font for any embedded text.
$STATE->assignValue(savedfont => $curfont, 'local');
$STATE->assignValue(script_base_level => scalar(@{ $$self{boxing} })); # See getScriptLevel
my $mathfont = $STATE->lookupValue('mathfont')->merge(
my $isdisplay = $mode =~ /^display/;
my $mathfont = $STATE->lookupValue('mathfont')->merge(
color => $curfont->getColor, background => $curfont->getBackground,
size => $curfont->getSize,
mathstyle => ($mode =~ /^display/ ? 'display' : 'text'));
mathstyle => ($isdisplay ? 'display' : 'text'));
$STATE->assignValue(font => $mathfont, 'local');
$STATE->assignValue(initial_math_font => $mathfont, 'local');
$STATE->assignValue(fontfamily => -1, 'local'); }
$STATE->assignValue(fontfamily => -1, 'local');
my $every = ($isdisplay ? T_CS('\everydisplay') : T_CS('\everymath'));
my $ereg = $STATE->lookupDefinition($every);
if (my $toks = $ereg && $ereg->isRegister && $ereg->valueOf()) {
$self->getGullet->unread($toks); } }
else {
# When entering text mode, we should set the font to the text font in use before the math
# but inherit color and size
Expand Down
15 changes: 4 additions & 11 deletions lib/LaTeXML/Engine/LaTeX.pool.ltxml
Original file line number Diff line number Diff line change
Expand Up @@ -2050,15 +2050,9 @@ DefConstructorI('\lx@begin@display@math', undef,
. "</ltx:Math>"
. "</ltx:equation>",
alias => '$$',
beforeDigest => sub {
$_[0]->beginMode('display_math');
if (my @everymath_toks = $STATE->lookupDefinition(T_CS('\everymath'))->valueOf->unlist()) {
$_[0]->getGullet->unread(@everymath_toks); }
if (my @everydisplay_toks = $STATE->lookupDefinition(T_CS('\everydisplay'))->valueOf->unlist()) {
$_[0]->getGullet->unread(@everydisplay_toks); }
return; },
properties => sub { RefStepID('equation') },
captureBody => 1);
beforeDigest => sub { $_[0]->beginMode('display_math'); },
properties => sub { RefStepID('equation') },
captureBody => 1);

DefEnvironment('{displaymath}',
"<ltx:equation xml:id='#id'>"
Expand All @@ -2077,8 +2071,7 @@ DefEnvironment('{math}',
. "#body"
. "</ltx:XMath>"
. "</ltx:Math>",
mode => 'inline_math',
);
mode => 'inline_math');

Let('\curr@math@size', '\@empty');

Expand Down
28 changes: 10 additions & 18 deletions lib/LaTeXML/Engine/TeX_Math.pool.ltxml
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,8 @@ DefConstructorI('\lx@begin@display@math', undef,
. "</ltx:Math>"
. "</ltx:equation>",
reversion => Tokens(T_MATH, T_MATH),
beforeDigest => sub {
$_[0]->beginMode('display_math');
if (my @everymath_toks = $STATE->lookupDefinition(T_CS('\everymath'))->valueOf->unlist()) {
$_[0]->getGullet->unread(@everymath_toks); }
if (my @everydisplay_toks = $STATE->lookupDefinition(T_CS('\everydisplay'))->valueOf->unlist()) {
$_[0]->getGullet->unread(@everydisplay_toks); }
return; }, captureBody => 1);
beforeDigest => sub { $_[0]->beginMode('display_math'); },
captureBody => 1);
DefConstructorI('\lx@end@display@math', undef, "",
reversion => Tokens(T_MATH, T_MATH),
beforeDigest => sub { $_[0]->endMode('display_math'); });
Expand All @@ -152,11 +147,8 @@ DefConstructorI('\lx@begin@inline@math', undef,
. "</ltx:XMath>"
. "</ltx:Math>",
reversion => Tokens(T_MATH),
beforeDigest => sub {
$_[0]->beginMode('inline_math');
if (my @everymath_toks = $STATE->lookupDefinition(T_CS('\everymath'))->valueOf->unlist()) {
$_[0]->getGullet->unread(@everymath_toks); }
return; }, captureBody => 1);
beforeDigest => sub { $_[0]->beginMode('inline_math'); },
captureBody => 1);
DefConstructorI('\\lx@end@inline@math', undef, "",
reversion => Tokens(T_MATH),
beforeDigest => sub { $_[0]->endMode('inline_math'); });
Expand Down Expand Up @@ -911,26 +903,26 @@ Let('\vcenter', '\vbox');
# Note that (over|under) brace accents act like \limit, but lines, arrows do NOT!
DefMath('\lx@math@overline{}', UTF(0xAF),
operator_role => 'OVERACCENT', operator_stretchy => 'true',
name => 'overline', alias => '\overline');
name => 'overline', alias => '\overline');
DefConstructor('\lx@text@overline{}',
"<ltx:text framed='overline' _noautoclose='1'>#1</ltx:text>");
DefMath('\lx@math@underline{}', UTF(0xAF),
operator_role => 'UNDERACCENT', operator_stretchy => 'true',
name => 'underline', alias => '\underline');
name => 'underline', alias => '\underline');
DefConstructor('\lx@text@underline{}',
"<ltx:text framed='underline' _noautoclose='1'>#1</ltx:text>");
DefMath('\lx@math@overrightarrow{}', "\x{2192}",
operator_role => 'OVERACCENT', operator_stretchy => 'true',
name => 'overrightarrow', alias => '\overrightarrow');
name => 'overrightarrow', alias => '\overrightarrow');
DefMath('\lx@math@overleftarrow{}', "\x{2190}",
operator_role => 'OVERACCENT', operator_stretchy => 'true',
name => 'overleftarrow', alias => '\overleftarrow');
name => 'overleftarrow', alias => '\overleftarrow');
DefMath('\lx@math@underrightarrow{}', "\x{2192}",
operator_role => 'UNDERACCENT', operator_stretchy => 'true',
name => 'underrightarrow', alias => '\underrightarrow');
name => 'underrightarrow', alias => '\underrightarrow');
DefMath('\lx@math@underleftarrow{}', "\x{2190}",
operator_role => 'UNDERACCENT', operator_stretchy => 'true',
name => 'underleftarrow', alias => '\underleftarrow');
name => 'underleftarrow', alias => '\underleftarrow');
DefMath('\lx@math@overbrace{}', "\x{23DE}",
operator_role => 'OVERACCENT', scriptpos => 'mid', operator_stretchy => 'true',
name => 'overbrace', alias => '\overbrace', robust => 1);
Expand Down
Binary file modified t/expansion/aftergroup.pdf
Binary file not shown.
27 changes: 19 additions & 8 deletions t/expansion/aftergroup.tex
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,25 @@ \section{Every Box}
\everyhbox={}
\everyvbox={}


% \everymath is NOT (yet) supported.
% \section{EveryMath}
% \everymath={.}
% $a+b$

% \everymath={}
% $a+b$
\section{Every Math/Display}
{
\everymath={\rightarrow}
$a+b$
\begin{math} a + b \end{math}
$$a+b$$
\[a+b\]
\begin{displaymath} a + b \end{displaymath}
\begin{equation} a + b \end{equation}
}
{
\everydisplay={\rightarrow}
$a+b$
\begin{math} a + b \end{math}
$$a+b$$
\[a+b\]
\begin{displaymath} a + b \end{displaymath}
\begin{equation} a + b \end{equation}
}

% $ a + \everymath={} b$

Expand Down
166 changes: 166 additions & 0 deletions t/expansion/aftergroup.xml
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,170 @@ No quirks</p>
<p>VBOX HBOX An Hbox HBOX Another Hbox</p>
</para>
</section>
<section inlist="toc" xml:id="S5">
<tags>
<tag>5</tag>
<tag role="refnum">5</tag>
<tag role="typerefnum">§5</tag>
</tags>
<title><tag close=" ">5</tag>Every Math/Display</title>
<para xml:id="S5.p1">
<p><Math mode="inline" tex="\rightarrow a+b" text="absent rightarrow a + b" xml:id="S5.p1.m1">
<XMath>
<XMApp>
<XMTok name="rightarrow" role="ARROW">→</XMTok>
<XMTok meaning="absent"/>
<XMApp>
<XMTok meaning="plus" role="ADDOP">+</XMTok>
<XMTok font="italic" role="UNKNOWN">a</XMTok>
<XMTok font="italic" role="UNKNOWN">b</XMTok>
</XMApp>
</XMApp>
</XMath>
</Math>
<Math mode="inline" tex="\rightarrow a+b" text="absent rightarrow a + b" xml:id="S5.p1.m2">
<XMath>
<XMApp>
<XMTok name="rightarrow" role="ARROW">→</XMTok>
<XMTok meaning="absent"/>
<XMApp>
<XMTok meaning="plus" role="ADDOP">+</XMTok>
<XMTok font="italic" role="UNKNOWN">a</XMTok>
<XMTok font="italic" role="UNKNOWN">b</XMTok>
</XMApp>
</XMApp>
</XMath>
</Math></p>
<equation xml:id="S5.Ex1">
<Math mode="display" tex="a+b" text="a + b" xml:id="S5.Ex1.m1">
<XMath>
<XMApp>
<XMTok meaning="plus" role="ADDOP">+</XMTok>
<XMTok font="italic" role="UNKNOWN">a</XMTok>
<XMTok font="italic" role="UNKNOWN">b</XMTok>
</XMApp>
</XMath>
</Math>
</equation>
<equation xml:id="S5.Ex2">
<Math mode="display" tex="a+b" text="a + b" xml:id="S5.Ex2.m1">
<XMath>
<XMApp>
<XMTok meaning="plus" role="ADDOP">+</XMTok>
<XMTok font="italic" role="UNKNOWN">a</XMTok>
<XMTok font="italic" role="UNKNOWN">b</XMTok>
</XMApp>
</XMath>
</Math>
</equation>
<equation xml:id="S5.Ex3">
<Math mode="display" tex="a+b" text="a + b" xml:id="S5.Ex3.m1">
<XMath>
<XMApp>
<XMTok meaning="plus" role="ADDOP">+</XMTok>
<XMTok font="italic" role="UNKNOWN">a</XMTok>
<XMTok font="italic" role="UNKNOWN">b</XMTok>
</XMApp>
</XMath>
</Math>
</equation>
<equation xml:id="S5.E1">
<tags>
<tag>(1)</tag>
<tag role="refnum">1</tag>
</tags>
<Math mode="display" tex="a+b" text="a + b" xml:id="S5.E1.m1">
<XMath>
<XMApp>
<XMTok meaning="plus" role="ADDOP">+</XMTok>
<XMTok font="italic" role="UNKNOWN">a</XMTok>
<XMTok font="italic" role="UNKNOWN">b</XMTok>
</XMApp>
</XMath>
</Math>
</equation>
<p><Math mode="inline" tex="a+b" text="a + b" xml:id="S5.p1.m3">
<XMath>
<XMApp>
<XMTok meaning="plus" role="ADDOP">+</XMTok>
<XMTok font="italic" role="UNKNOWN">a</XMTok>
<XMTok font="italic" role="UNKNOWN">b</XMTok>
</XMApp>
</XMath>
</Math>
<Math mode="inline" tex="a+b" text="a + b" xml:id="S5.p1.m4">
<XMath>
<XMApp>
<XMTok meaning="plus" role="ADDOP">+</XMTok>
<XMTok font="italic" role="UNKNOWN">a</XMTok>
<XMTok font="italic" role="UNKNOWN">b</XMTok>
</XMApp>
</XMath>
</Math></p>
<equation xml:id="S5.Ex4">
<Math mode="display" tex="\rightarrow a+b" text="absent rightarrow a + b" xml:id="S5.Ex4.m1">
<XMath>
<XMApp>
<XMTok name="rightarrow" role="ARROW">→</XMTok>
<XMTok meaning="absent"/>
<XMApp>
<XMTok meaning="plus" role="ADDOP">+</XMTok>
<XMTok font="italic" role="UNKNOWN">a</XMTok>
<XMTok font="italic" role="UNKNOWN">b</XMTok>
</XMApp>
</XMApp>
</XMath>
</Math>
</equation>
<equation xml:id="S5.Ex5">
<Math mode="display" tex="\rightarrow a+b" text="absent rightarrow a + b" xml:id="S5.Ex5.m1">
<XMath>
<XMApp>
<XMTok name="rightarrow" role="ARROW">→</XMTok>
<XMTok meaning="absent"/>
<XMApp>
<XMTok meaning="plus" role="ADDOP">+</XMTok>
<XMTok font="italic" role="UNKNOWN">a</XMTok>
<XMTok font="italic" role="UNKNOWN">b</XMTok>
</XMApp>
</XMApp>
</XMath>
</Math>
</equation>
<equation xml:id="S5.Ex6">
<Math mode="display" tex="\rightarrow a+b" text="absent rightarrow a + b" xml:id="S5.Ex6.m1">
<XMath>
<XMApp>
<XMTok name="rightarrow" role="ARROW">→</XMTok>
<XMTok meaning="absent"/>
<XMApp>
<XMTok meaning="plus" role="ADDOP">+</XMTok>
<XMTok font="italic" role="UNKNOWN">a</XMTok>
<XMTok font="italic" role="UNKNOWN">b</XMTok>
</XMApp>
</XMApp>
</XMath>
</Math>
</equation>
<equation xml:id="S5.E2">
<tags>
<tag>(2)</tag>
<tag role="refnum">2</tag>
</tags>
<Math mode="display" tex="\rightarrow a+b" text="absent rightarrow a + b" xml:id="S5.E2.m1">
<XMath>
<XMApp>
<XMTok name="rightarrow" role="ARROW">→</XMTok>
<XMTok meaning="absent"/>
<XMApp>
<XMTok meaning="plus" role="ADDOP">+</XMTok>
<XMTok font="italic" role="UNKNOWN">a</XMTok>
<XMTok font="italic" role="UNKNOWN">b</XMTok>
</XMApp>
</XMApp>
</XMath>
</Math>
</equation>
</para>
</section>
</document>

0 comments on commit f82b733

Please sign in to comment.