LaTeXML
view release on metacpan or search on metacpan
lib/LaTeXML/Package/TeX.pool.ltxml view on Meta::CPAN
# Replace XMText by XMWrap/* (this should preserve the parse?)
$textnode = $document->renameNode($textnode, 'ltx:XMWrap');
$document->replaceNode($children[0], map { $_->childNodes } $children[0]->childNodes); }
# # # RISKY!!!! If SOME nodes are math...
# # # pull the whole sequence up, unwrap the math and putting the rest back in XMText.
# # # Even with the XMWrap, this seems to wreak havoc on parsing and structure?
# # if($document->findnodes('ltx:Math',$textnode)){
# # # Replace XMText by XMWrap/* (this should preserve the parse?)
# # $textnode=$document->renameNode($textnode,'ltx:XMWrap');
# # foreach my $child (@children){
# # if($model->getNodeQName($child) eq 'ltx:Math'){
# # $document->replaceNode($child,map($_->childNodes,$child->childNodes)); }
# # else {
# # $document->wrapNodes('ltx:XMText',$child); }}}
# If a single tabular that ONLY(?) contains Math, turn into an XMArray
# Well, a tabular REALLY shouldn't be in math;
# How much math should determine the switch?
# [will alignment attributes be lost?]
elsif ((scalar(@children) == 1) && ($model->getNodeQName($children[0]) eq 'ltx:tabular')
## Should we ALWAYS do this, or just for some minimal amount of math???
## && !$document->findnodes('ltx:tabular/ltx:tr/ltx:td/text()'
## .' | ltx:tabular/ltx:tbody/ltx:tr/ltx:td/text()'
## .' | ltx:tabular/ltx:tr/ltx:td[not(ltx:Math)]'
## .' | ltx:tabular/ltx:tbody/ltx:tr/ltx:td[not(ltx:Math)]',
## $textnode)
) {
# First step is remove any ltx:tbody from the tabular!
foreach my $tb ($document->findnodes('ltx:tabular/ltx:tbody', $textnode)) {
$document->unwrapNodes($tb); }
# Now, we can start replacing tabular=>XMArray, tr=>XMRow, td=>XMCell
my $table = $document->renameNode($children[0], 'ltx:XMArray');
foreach my $row ($table->childNodes) {
$row = $document->renameNode($row, 'ltx:XMRow');
foreach my $cell ($row->childNodes) {
$cell = $document->renameNode($cell, 'ltx:XMCell');
foreach my $m ($cell->childNodes) {
if ($model->getNodeQName($m) eq 'ltx:Math') { # Math cell, unwrap the Math/XMath layer
$document->replaceNode($m, map { $_->childNodes } $m->childNodes); }
else { # Otherwise, wrap whatever it is in an XMText
$document->wrapNodes('ltx:XMText', $m); }
} } }
# And now we don't need the XMText any more.
foreach my $attr ($textnode->attributes) { # Copy the child's attributes (should Merge!!)
$table->setAttribute($attr->nodeName => $attr->getValue); }
my $newtable = $document->unwrapNodes($textnode);
if (my $id = $textnode->getAttribute('xml:id')) {
$document->unRecordID($id);
$document->recordID($id, $newtable); } }
return; }
#**********************************************************************
# Support for MathFork.
#**********************************************************************
# [Note: this block of code seems like it belongs somewhere else]
# A MathFork supports document-level alignment of math,
# by collecting equations into an equationgroup. Each equation can contain
# one or more MathFork structures which separate the semantically meaningful
# equation (if possible) from the collection of rows and/or column fragments
# for alignment. The goal is to be able to present the aligned structure
# composed of various mathematical fragments in a grid, and yet still represent
# the (presumably) meaningful complete formula.
#
# The structure looks like
# <MathFork>
# <Math><XMath>...</XMath></Math>
# <MathBranch>..</MathBranch>
# </MathFork>
# The initial, "main", Math will contain a complete formula (hopefully).
# The MathBranch will typically contain one or more <tr>, each of which
# contains one or more <td>, each of which contains a <Math> representing
# a cell of the aligned structure.
#======================================================================
# openMathFork($document,$equation) will add a MathFork structure
# to the given $equation, and return ($mainfork, $branch)
# where $mainfork is the initial <ltx:Math> and $branch is the <ltx:MathBranch>.
# You'll probably want to be adding Stuff to one or both of $mainfork & $branch.
# Most typically, you'll be finding math fragments that you've found in the
# current content of $equation and adding them into both $mainfork & $branch
# using addColumnToMathFork.
sub openMathFork {
my ($document, $equation) = @_;
my $fork = $document->openElementAt($equation, 'ltx:MathFork');
my $mainfork = $document->openElementAt($fork, 'ltx:Math', _box => MathWhatsit()); # Start EMPTY!
my $xmath = $document->openElementAt($mainfork, 'ltx:XMath');
my $branch = $document->openElementAt($fork, 'ltx:MathBranch');
return ($mainfork, $branch); }
# Close the appropriate elements of an ltx:MathFork created with openMathFork.
sub closeMathFork {
my ($document, $equation, $mainfork, $branch) = @_;
# Now, close them all.
$document->closeElementAt($branch);
$document->closeElementAt($document->getFirstChildElement($mainfork));
$document->closeElementAt($mainfork);
# $document->closeElementAt($mainfork->parentNode); }
# More defensive? Sometimes we end up with a DocumentFragment as parent of $mainfork????!?!?!?!
my @mfs = $document->findnodes('ltx:MathFork', $equation);
$document->closeElementAt($mfs[-1]);
my $fork = $branch->parentNode;
my @branches = $fork->childNodes;
if (scalar(@branches) == 1) { # Whoops, came up empty!
$fork->unbindNode; }
return; }
# Create an inline math Whatsit from a list of math Boxes, Lists or Whatsits.
# Note that we unwrap @hidden@bgroup's (!) and normalize \displaystyle (!)
# This is primarily useful for synthesizing the Box for a newly created ltx:Math
# that is synthesized from other math content within a ltx:MathFork.
sub MathWhatsit {
my (@items) = @_;
my $hbgd = LookupDefinition(T_CS('\@hidden@bgroup'));
@items = map { ((ref $_ eq 'LaTeXML::Core::Whatsit') && ($_->getDefinition eq $hbgd)
? $_->getBody->unlist : ($_)) }
map { $_->unlist } grep { $_ } @items;
my $locator = undef;
foreach my $i (@items) {
last if $locator;
$locator = $i->getLocator; }
my @styles = grep { UnTeX($_) eq '\displaystyle' } @items;
if (@styles) {
@items = ($styles[0], grep { UnTeX($_) ne '\displaystyle' } @items); }
return LaTeXML::Core::Whatsit->new(LookupDefinition(T_CS('\@@BEGININLINEMATH')), [],
body => List(@items, mode => 'math'),
trailer => T_CS('\@@ENDINLINEMATH'),
locator => $locator, isMath => 1); }
#======================================================================
( run in 0.562 second using v1.01-cache-2.11-cpan-437f7b0c052 )