From 6cac9de94181189decfe67da9c7f3402ed45a4f0 Mon Sep 17 00:00:00 2001 From: Bruce Miller Date: Thu, 21 Dec 2023 09:52:14 -0500 Subject: [PATCH] Fixup \centering, \raggedright,... so they only start centering after the current node --- lib/LaTeXML/Package/LaTeX.pool.ltxml | 51 ++++++++++++++-------------- 1 file changed, 25 insertions(+), 26 deletions(-) diff --git a/lib/LaTeXML/Package/LaTeX.pool.ltxml b/lib/LaTeXML/Package/LaTeX.pool.ltxml index 94438288a..90fdc62b5 100644 --- a/lib/LaTeXML/Package/LaTeX.pool.ltxml +++ b/lib/LaTeXML/Package/LaTeX.pool.ltxml @@ -1364,42 +1364,41 @@ DefEnvironment('{flushright}', sub { # These add an operation to be carried out on the current node & following siblings, when the current group ends. # These operators will add alignment (class) attributes to each "line" in the current block. -#DefPrimitiveI('\centering', undef, sub { UnshiftValue(beforeAfterGroup=>T_CS('\@add@centering')); }); -# NOTE: THere's a problem here. The current method seems to work right for these operators -# appearing within the typical environments. HOWEVER, it doesn't work for a simple \bgroup or \begingroup!!! -# (they don't create a node! or even a whatsit!) -DefConstructorI('\centering', undef, - sub { AssignValue(ALIGNING_NODE => $_[0]->getElement); return; }, +sub setupAligningContext { + my ($document) = @_; + my $node = $document->getElement; + AssignValue(ALIGNING_NODE => [$node, $node->lastChild]); + return; } + +sub applyAligningContext { + my ($document, $align, $class) = @_; + if (my $container = LookupValue('ALIGNING_NODE')) { + my ($node, $previous) = @$container; + my @children = $node->childNodes($node); + while (my $skip = shift(@children)) { + last if !$previous || $previous->isSameNode($skip); } + while (my $child = shift(@children)) { + setAlignOrClass($document, $child, $align, $class); } } + return; } + +DefConstructorI('\centering', undef, \&setupAligningContext, beforeDigest => sub { UnshiftValue(beforeAfterGroup => T_CS('\@add@centering')); }); -DefConstructorI('\raggedright', undef, - sub { AssignValue(ALIGNING_NODE => $_[0]->getElement); return; }, +DefConstructorI('\raggedright', undef, \&setupAligningContext, beforeDigest => sub { UnshiftValue(beforeAfterGroup => T_CS('\@add@raggedright')); }); -DefConstructorI('\raggedleft', undef, - sub { AssignValue(ALIGNING_NODE => $_[0]->getElement); return; }, +DefConstructorI('\raggedleft', undef, \&setupAligningContext, beforeDigest => sub { UnshiftValue(beforeAfterGroup => T_CS('\@add@raggedleft')); }); DefConstructorI('\@add@centering', undef, - sub { if (my $node = LookupValue('ALIGNING_NODE')) { - map { setAlignOrClass($_[0], $_, 'center', 'ltx_centering') } - $_[0]->getChildElements($node); } }); + sub { applyAligningContext($_[0], 'center', 'ltx_centering'); }); # Note that \raggedright is essentially align left DefConstructorI('\@add@raggedright', undef, - sub { if (my $node = LookupValue('ALIGNING_NODE')) { - map { setAlignOrClass($_[0], $_, undef, 'ltx_align_left') } - $_[0]->getChildElements($node); } }); + sub { applyAligningContext($_[0], undef, 'ltx_align_left'); }); DefConstructorI('\@add@raggedleft', undef, - sub { if (my $node = LookupValue('ALIGNING_NODE')) { - map { setAlignOrClass($_[0], $_, undef, 'ltx_align_right') } - $_[0]->getChildElements($node); } }); - + sub { applyAligningContext($_[0], undef, 'ltx_align_right'); }); DefConstructorI('\@add@flushright', undef, - sub { if (my $node = LookupValue('ALIGNING_NODE')) { - map { setAlignOrClass($_[0], $_, 'right', 'ltx_align_right') } - $_[0]->getChildElements($node); } }); + sub { applyAligningContext($_[0], 'right', 'ltx_align_right'); }); DefConstructorI('\@add@flushleft', undef, - sub { if (my $node = LookupValue('ALIGNING_NODE')) { - map { setAlignOrClass($_[0], $_, 'left', 'ltx_align_left') } - $_[0]->getChildElements($node); } }); + sub { applyAligningContext($_[0], 'left', 'ltx_align_left'); }); #======================================================================- # C.6.1 Quotations and Verse