Skip to content

Commit

Permalink
Normalize floatscript math to text (#2222)
Browse files Browse the repository at this point in the history
* sanitize shallow floating super/subscripts in math mode downto plain-text ltx:sup/ltx:sub

* update tests for sanitized floatsuper/floatsub-scripts
  • Loading branch information
dginev authored Jan 5, 2024
1 parent 3b56185 commit 8280f27
Show file tree
Hide file tree
Showing 25 changed files with 105 additions and 575 deletions.
29 changes: 29 additions & 0 deletions lib/LaTeXML/Package/TeX.pool.ltxml
Original file line number Diff line number Diff line change
Expand Up @@ -4531,6 +4531,35 @@ DefMacroI('\active@math@prime', undef, sub {
AssignMathcode("'" => 0x8000);
Let("'", '\active@math@prime');

# Experiment: When we detect a math element containing solely a floating superscript in the
# *Frontmatter* of a document, assume it is a note mark, and normalize it down to
# plain text.
DefRewrite(xpath => 'descendant::ltx:Math[child::ltx:XMath[child::ltx:XMApp[' .
'(@role="FLOATSUPERSCRIPT" or @role="FLOATSUBSCRIPT") and ' .
'not(preceding-sibling::*) and not(following-sibling::*) ' .
'and not(./*/*[not(self::ltx:XMTok)]) ]]]',
replace => sub {
my ($document, $math) = @_;
# We can assume the grandchild of the XMath node is the XMArg,
# which we need to normalize to scripted Unicode.
if (my @xmath = element_nodes($math)) {
if (my @xmapp = element_nodes($xmath[0])) {
if (my @xmarg = element_nodes($xmapp[0])) {
if (my $role = $xmapp[0]->getAttribute('role')) {
my $text = $xmarg[0]->textContent;
local $LaTeXML::BOX = $document->getNodeBox($xmarg[0]);
if ($role eq 'FLOATSUPERSCRIPT') {
$document->insertElement('ltx:sup', $text);
return; }
elsif ($role eq 'FLOATSUBSCRIPT') {
$document->insertElement('ltx:sub', $text);
return; }
} } } }
# should never happen, but just in case:
Info("rewrite", "footnotemark", "Failed to find floating node in: " . $math->toString(1));
$document->getNode->appendChild($math);
return; });

#======================================================================
# \choose & friends, also need VERY special argument handling

Expand Down
32 changes: 4 additions & 28 deletions t/ams/amsdisplay.xml
Original file line number Diff line number Diff line change
Expand Up @@ -945,20 +945,8 @@
</equation>
<equation xml:id="S0.E15.x3">
<tags>
<tag>(15a<Math mode="inline" tex="{}^{\prime}" text="^prime" xml:id="S0.E15.x3.m1">
<XMath>
<XMApp role="FLOATSUPERSCRIPT" scriptpos="1">
<XMTok fontsize="70%" name="prime" role="SUPOP">′</XMTok>
</XMApp>
</XMath>
</Math>)</tag>
<tag role="refnum">15a<Math mode="inline" tex="{}^{\prime}" text="^prime" xml:id="S0.E15.x3.m2">
<XMath>
<XMApp role="FLOATSUPERSCRIPT" scriptpos="1">
<XMTok fontsize="70%" name="prime" role="SUPOP">′</XMTok>
</XMApp>
</XMath>
</Math></tag>
<tag>(15a<sup><text font="italic">′</text></sup>)</tag>
<tag role="refnum">15a<sup><text font="italic">′</text></sup></tag>
</tags>
<Math mode="display" tex="N=15a" text="N = 15 * a" xml:id="S0.E15.x3.m3">
<XMath>
Expand All @@ -976,20 +964,8 @@
</equation>
<equation xml:id="S0.E15.x4">
<tags>
<tag>(15a<Math mode="inline" tex="{}^{\prime\prime}" text="^prime2" xml:id="S0.E15.x4.m1">
<XMath>
<XMApp role="FLOATSUPERSCRIPT" scriptpos="1">
<XMTok fontsize="70%" name="prime2" role="SUPOP">′′</XMTok>
</XMApp>
</XMath>
</Math>)</tag>
<tag role="refnum">15a<Math mode="inline" tex="{}^{\prime\prime}" text="^prime2" xml:id="S0.E15.x4.m2">
<XMath>
<XMApp role="FLOATSUPERSCRIPT" scriptpos="1">
<XMTok fontsize="70%" name="prime2" role="SUPOP">′′</XMTok>
</XMApp>
</XMath>
</Math></tag>
<tag>(15a<sup><text font="italic">′′</text></sup>)</tag>
<tag role="refnum">15a<sup><text font="italic">′′</text></sup></tag>
</tags>
<Math mode="display" tex="N=15b" text="N = 15 * b" xml:id="S0.E15.x4.m3">
<XMath>
Expand Down
16 changes: 2 additions & 14 deletions t/ams/mathtools.xml
Original file line number Diff line number Diff line change
Expand Up @@ -945,20 +945,8 @@ In the star form <verbatim font="typewriter upright">\ref*</verbatim> becomes <r
<p>Non-textual</p>
<equation labels="LABEL:eq:nt1" xml:id="S2.Ex13">
<tags>
<tag>(n<Math mode="inline" tex="{}^{\mathit{th}}" text="^th" xml:id="S2.Ex13.m1">
<XMath>
<XMApp role="FLOATSUPERSCRIPT" scriptpos="1">
<XMTok font="italic" fontsize="70%" role="UNKNOWN">th</XMTok>
</XMApp>
</XMath>
</Math>)</tag>
<tag role="refnum">n<Math mode="inline" tex="{}^{\mathit{th}}" text="^th" xml:id="S2.Ex13.m2">
<XMath>
<XMApp role="FLOATSUPERSCRIPT" scriptpos="1">
<XMTok font="italic" fontsize="70%" role="UNKNOWN">th</XMTok>
</XMApp>
</XMath>
</Math></tag>
<tag>(n<sup><text font="italic">th</text></sup>)</tag>
<tag role="refnum">n<sup><text font="italic">th</text></sup></tag>
</tags>
<Math mode="display" tex="d=d" text="d = d" xml:id="S2.Ex13.m3">
<XMath>
Expand Down
36 changes: 3 additions & 33 deletions t/complex/aliceblog.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,7 @@
<paragraph inlist="toc" labels="LABEL:blog:trouble" resource="/alice/posts/trouble_with_bob" typeof="schema:BlogPosting" xml:id="S2.SS0.SSS0.Px1">
<title property="dcterms:title">The Trouble with Bob</title>
<para xml:id="S2.SS0.SSS0.Px1.p1">
<p>Date: <text content="2011-09-10" datatype="xsd:date" property="dcterms:created">10<Math mode="inline" tex="{}^{th}" text="^t * h" xml:id="S2.SS0.SSS0.Px1.p1.m1">
<XMath>
<XMApp role="FLOATSUPERSCRIPT" scriptpos="1">
<XMApp>
<XMTok meaning="times" role="MULOP">⁢</XMTok>
<XMTok font="italic" fontsize="70%" role="UNKNOWN">t</XMTok>
<XMTok font="italic" fontsize="70%" role="UNKNOWN">h</XMTok>
</XMApp>
</XMApp>
</XMath>
</Math> of September, 2011</text>.<break/><text property="dcterms:creator schema:creator">Alice</text>.</p>
<p>Date: <text content="2011-09-10" datatype="xsd:date" property="dcterms:created">10<sup><text font="italic">th</text></sup> of September, 2011</text>.<break/><text property="dcterms:creator schema:creator">Alice</text>.</p>
</para>
<para xml:id="S2.SS0.SSS0.Px1.p2">
<p property="schema:articleBody">The trouble with Bob is that he takes much better photos than I do.</p>
Expand All @@ -92,17 +82,7 @@ by
<paragraph inlist="toc" labels="LABEL:blog:josbbq" resource="/alice/posts/jos_barbeque" typeof="schema:BlogPosting" xml:id="S2.SS0.SSS0.Px2">
<title property="dcterms:title">Jo’s Barbeque</title>
<para xml:id="S2.SS0.SSS0.Px2.p1">
<p>Date: <text content="2011-09-14" datatype="xsd:date" property="dcterms:created">14<Math mode="inline" tex="{}^{th}" text="^t * h" xml:id="S2.SS0.SSS0.Px2.p1.m1">
<XMath>
<XMApp role="FLOATSUPERSCRIPT" scriptpos="1">
<XMApp>
<XMTok meaning="times" role="MULOP">⁢</XMTok>
<XMTok font="italic" fontsize="70%" role="UNKNOWN">t</XMTok>
<XMTok font="italic" fontsize="70%" role="UNKNOWN">h</XMTok>
</XMApp>
</XMApp>
</XMath>
</Math> of September, 2011</text>.<break/></p>
<p>Date: <text content="2011-09-14" datatype="xsd:date" property="dcterms:created">14<sup><text font="italic">th</text></sup> of September, 2011</text>.<break/></p>
</para>
<para xml:id="S2.SS0.SSS0.Px2.p2">
<p><text property="dcterms:creator">Eve</text>.</p>
Expand All @@ -114,17 +94,7 @@ by
<paragraph inlist="toc" labels="LABEL:blog:photos" resource="/alice/posts/my_photos" typeof="schema:BlogPosting" xml:id="S2.SS0.SSS0.Px3">
<title property="dcterms:title">I will post my photos nevertheless…</title>
<para xml:id="S2.SS0.SSS0.Px3.p1">
<p>Date: <text content="2011-09-15" datatype="xsd:date" property="dcterms:created">15<Math mode="inline" tex="{}^{th}" text="^t * h" xml:id="S2.SS0.SSS0.Px3.p1.m1">
<XMath>
<XMApp role="FLOATSUPERSCRIPT" scriptpos="1">
<XMApp>
<XMTok meaning="times" role="MULOP">⁢</XMTok>
<XMTok font="italic" fontsize="70%" role="UNKNOWN">t</XMTok>
<XMTok font="italic" fontsize="70%" role="UNKNOWN">h</XMTok>
</XMApp>
</XMApp>
</XMath>
</Math> of September, 2011</text>.<break/></p>
<p>Date: <text content="2011-09-15" datatype="xsd:date" property="dcterms:created">15<sup><text font="italic">th</text></sup> of September, 2011</text>.<break/></p>
</para>
<para xml:id="S2.SS0.SSS0.Px3.p2">
<p><text property="dcterms:creator" resourceidref="me">Alice</text>.</p>
Expand Down
24 changes: 3 additions & 21 deletions t/encoding/ansinew.xml
Original file line number Diff line number Diff line change
Expand Up @@ -107,20 +107,8 @@
<XMTok meaning="plus-or-minus" name="pm" role="ADDOP">±</XMTok>
</XMath>
</Math></td>
<td align="center"><Math mode="inline" tex="{{}^{2}}" text="^2" xml:id="S1.p1.m3">
<XMath>
<XMApp role="FLOATSUPERSCRIPT" scriptpos="2">
<XMTok fontsize="70%" meaning="2" role="NUMBER">2</XMTok>
</XMApp>
</XMath>
</Math></td>
<td align="center"><Math mode="inline" tex="{{}^{3}}" text="^3" xml:id="S1.p1.m4">
<XMath>
<XMApp role="FLOATSUPERSCRIPT" scriptpos="2">
<XMTok fontsize="70%" meaning="3" role="NUMBER">3</XMTok>
</XMApp>
</XMath>
</Math></td>
<td align="center"><sup>2</sup></td>
<td align="center"><sup>3</sup></td>
<td align="center">´</td>
<td align="center"><Math mode="inline" tex="\mu" text="mu" xml:id="S1.p1.m5">
<XMath>
Expand All @@ -130,13 +118,7 @@
<td align="center">¶</td>
<td align="center">·</td>
<td align="center">¸</td>
<td align="center"><Math mode="inline" tex="{{}^{1}}" text="^1" xml:id="S1.p1.m6">
<XMath>
<XMApp role="FLOATSUPERSCRIPT" scriptpos="2">
<XMTok fontsize="70%" meaning="1" role="NUMBER">1</XMTok>
</XMApp>
</XMath>
</Math></td>
<td align="center"><sup>1</sup></td>
<td align="center">º</td>
<td align="center">»</td>
<td align="center">¼</td>
Expand Down
24 changes: 3 additions & 21 deletions t/encoding/cp1252.xml
Original file line number Diff line number Diff line change
Expand Up @@ -107,20 +107,8 @@
<XMTok meaning="plus-or-minus" name="pm" role="ADDOP">±</XMTok>
</XMath>
</Math></td>
<td align="center"><Math mode="inline" tex="{{}^{2}}" text="^2" xml:id="S1.p1.m3">
<XMath>
<XMApp role="FLOATSUPERSCRIPT" scriptpos="2">
<XMTok fontsize="70%" meaning="2" role="NUMBER">2</XMTok>
</XMApp>
</XMath>
</Math></td>
<td align="center"><Math mode="inline" tex="{{}^{3}}" text="^3" xml:id="S1.p1.m4">
<XMath>
<XMApp role="FLOATSUPERSCRIPT" scriptpos="2">
<XMTok fontsize="70%" meaning="3" role="NUMBER">3</XMTok>
</XMApp>
</XMath>
</Math></td>
<td align="center"><sup>2</sup></td>
<td align="center"><sup>3</sup></td>
<td align="center">´</td>
<td align="center"><Math mode="inline" tex="\mu" text="mu" xml:id="S1.p1.m5">
<XMath>
Expand All @@ -130,13 +118,7 @@
<td align="center">¶</td>
<td align="center">·</td>
<td align="center">¸</td>
<td align="center"><Math mode="inline" tex="{{}^{1}}" text="^1" xml:id="S1.p1.m6">
<XMath>
<XMApp role="FLOATSUPERSCRIPT" scriptpos="2">
<XMTok fontsize="70%" meaning="1" role="NUMBER">1</XMTok>
</XMApp>
</XMath>
</Math></td>
<td align="center"><sup>1</sup></td>
<td align="center">º</td>
<td align="center">»</td>
<td align="center">¼</td>
Expand Down
16 changes: 2 additions & 14 deletions t/encoding/cp437.xml
Original file line number Diff line number Diff line change
Expand Up @@ -222,20 +222,8 @@
<XMTok meaning="square-root" name="surd" role="OPERATOR">√</XMTok>
</XMath>
</Math></td>
<td align="center"><Math mode="inline" tex="{{}^{n}}" text="^n" xml:id="S1.p1.m25">
<XMath>
<XMApp role="FLOATSUPERSCRIPT" scriptpos="2">
<XMTok font="italic" fontsize="70%" role="UNKNOWN">n</XMTok>
</XMApp>
</XMath>
</Math></td>
<td align="center"><Math mode="inline" tex="{{}^{2}}" text="^2" xml:id="S1.p1.m26">
<XMath>
<XMApp role="FLOATSUPERSCRIPT" scriptpos="2">
<XMTok fontsize="70%" meaning="2" role="NUMBER">2</XMTok>
</XMApp>
</XMath>
</Math></td>
<td align="center"><sup><text font="italic">n</text></sup></td>
<td align="center"><sup>2</sup></td>
<td align="center"><rule depth="-1.0pt" height="4.0pt" width="3.0pt"/></td>
<td/>
</tr>
Expand Down
16 changes: 2 additions & 14 deletions t/encoding/cp437de.xml
Original file line number Diff line number Diff line change
Expand Up @@ -218,20 +218,8 @@
<XMTok meaning="square-root" name="surd" role="OPERATOR">√</XMTok>
</XMath>
</Math></td>
<td align="center"><Math mode="inline" tex="{{}^{n}}" text="^n" xml:id="S1.p1.m24">
<XMath>
<XMApp role="FLOATSUPERSCRIPT" scriptpos="2">
<XMTok font="italic" fontsize="70%" role="UNKNOWN">n</XMTok>
</XMApp>
</XMath>
</Math></td>
<td align="center"><Math mode="inline" tex="{{}^{2}}" text="^2" xml:id="S1.p1.m25">
<XMath>
<XMApp role="FLOATSUPERSCRIPT" scriptpos="2">
<XMTok fontsize="70%" meaning="2" role="NUMBER">2</XMTok>
</XMApp>
</XMath>
</Math></td>
<td align="center"><sup><text font="italic">n</text></sup></td>
<td align="center"><sup>2</sup></td>
<td align="center"><rule depth="-1.0pt" height="4.0pt" width="3.0pt"/></td>
<td/>
</tr>
Expand Down
Binary file modified t/encoding/cp850.pdf
Binary file not shown.
24 changes: 3 additions & 21 deletions t/encoding/cp850.xml
Original file line number Diff line number Diff line change
Expand Up @@ -204,27 +204,9 @@
<td align="center">°</td>
<td align="center">¨</td>
<td align="center">·</td>
<td align="center"><Math mode="inline" tex="{{}^{1}}" text="^1" xml:id="S1.p1.m6">
<XMath>
<XMApp role="FLOATSUPERSCRIPT" scriptpos="2">
<XMTok fontsize="70%" meaning="1" role="NUMBER">1</XMTok>
</XMApp>
</XMath>
</Math></td>
<td align="center"><Math mode="inline" tex="{{}^{3}}" text="^3" xml:id="S1.p1.m7">
<XMath>
<XMApp role="FLOATSUPERSCRIPT" scriptpos="2">
<XMTok fontsize="70%" meaning="3" role="NUMBER">3</XMTok>
</XMApp>
</XMath>
</Math></td>
<td align="center"><Math mode="inline" tex="{{}^{2}}" text="^2" xml:id="S1.p1.m8">
<XMath>
<XMApp role="FLOATSUPERSCRIPT" scriptpos="2">
<XMTok fontsize="70%" meaning="2" role="NUMBER">2</XMTok>
</XMApp>
</XMath>
</Math></td>
<td align="center"><sup>1</sup></td>
<td align="center"><sup>3</sup></td>
<td align="center"><sup>2</sup></td>
<td align="center"><rule depth="-1.0pt" height="4.0pt" width="3.0pt"/></td>
<td/>
</tr>
Expand Down
Binary file modified t/encoding/cp858.pdf
Binary file not shown.
24 changes: 3 additions & 21 deletions t/encoding/cp858.xml
Original file line number Diff line number Diff line change
Expand Up @@ -204,27 +204,9 @@
<td align="center">°</td>
<td align="center">¨</td>
<td align="center">·</td>
<td align="center"><Math mode="inline" tex="{{}^{1}}" text="^1" xml:id="S1.p1.m6">
<XMath>
<XMApp role="FLOATSUPERSCRIPT" scriptpos="2">
<XMTok fontsize="70%" meaning="1" role="NUMBER">1</XMTok>
</XMApp>
</XMath>
</Math></td>
<td align="center"><Math mode="inline" tex="{{}^{3}}" text="^3" xml:id="S1.p1.m7">
<XMath>
<XMApp role="FLOATSUPERSCRIPT" scriptpos="2">
<XMTok fontsize="70%" meaning="3" role="NUMBER">3</XMTok>
</XMApp>
</XMath>
</Math></td>
<td align="center"><Math mode="inline" tex="{{}^{2}}" text="^2" xml:id="S1.p1.m8">
<XMath>
<XMApp role="FLOATSUPERSCRIPT" scriptpos="2">
<XMTok fontsize="70%" meaning="2" role="NUMBER">2</XMTok>
</XMApp>
</XMath>
</Math></td>
<td align="center"><sup>1</sup></td>
<td align="center"><sup>3</sup></td>
<td align="center"><sup>2</sup></td>
<td align="center"><rule depth="-1.0pt" height="4.0pt" width="3.0pt"/></td>
<td/>
</tr>
Expand Down
16 changes: 2 additions & 14 deletions t/encoding/cp865.xml
Original file line number Diff line number Diff line change
Expand Up @@ -222,20 +222,8 @@
<XMTok meaning="square-root" name="surd" role="OPERATOR">√</XMTok>
</XMath>
</Math></td>
<td align="center"><Math mode="inline" tex="{{}^{n}}" text="^n" xml:id="S1.p1.m25">
<XMath>
<XMApp role="FLOATSUPERSCRIPT" scriptpos="2">
<XMTok font="italic" fontsize="70%" role="UNKNOWN">n</XMTok>
</XMApp>
</XMath>
</Math></td>
<td align="center"><Math mode="inline" tex="{{}^{2}}" text="^2" xml:id="S1.p1.m26">
<XMath>
<XMApp role="FLOATSUPERSCRIPT" scriptpos="2">
<XMTok fontsize="70%" meaning="2" role="NUMBER">2</XMTok>
</XMApp>
</XMath>
</Math></td>
<td align="center"><sup><text font="italic">n</text></sup></td>
<td align="center"><sup>2</sup></td>
<td align="center"><rule depth="-1.0pt" height="4.0pt" width="3.0pt"/></td>
<td/>
</tr>
Expand Down
Loading

0 comments on commit 8280f27

Please sign in to comment.