Skip to content

Commit

Permalink
prune an empty \bibitem when auto-opened without need (brucemiller#2409)
Browse files Browse the repository at this point in the history
  • Loading branch information
dginev authored Sep 26, 2024
1 parent 80d205d commit 2bef09e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
13 changes: 12 additions & 1 deletion lib/LaTeXML/Engine/LaTeX.pool.ltxml
Original file line number Diff line number Diff line change
Expand Up @@ -4270,11 +4270,22 @@ DefConstructor('\lx@bibitem[] Semiverbatim',
. "#tags"
. "<ltx:bibblock>",
afterDigest => sub {
# check if the previous bibitem had an empty body,
# in which case prune it and reuse its ID and counters.
# this happens due to auto-open being a bit fragile (see issue #2403)
my $pruned_prev = 0;
if (my $prev_item = $LaTeXML::LIST[-1]) {
if (ref $prev_item eq 'LaTeXML::Core::Whatsit' and
$prev_item->getDefinition->getCS->getCSName eq '\lx@bibitem' and
not($prev_item->getArg(1)) and IsEmpty($prev_item->getArg(2))) {
$pruned_prev = 1;
Info('empty', 'bibitem', $_[0], "Encountered an empty \\bibitem, likely auto-opened without need. Pruning and reusing its id.");
pop(@LaTeXML::LIST); } }
my $tag = $_[1]->getArg(1);
my $key = CleanBibKey($_[1]->getArg(2));
if ($tag) {
$_[1]->setProperties(key => $key,
RefStepID('@bibitem'),
($pruned_prev ? RefCurrentID('@bibitem') : RefStepID('@bibitem')),
tags => Digest(T_BEGIN,
T_CS('\def'), T_CS('\the@bibitem'), T_BEGIN, Revert($tag), T_END,
Invocation(T_CS('\lx@make@tags'), T_OTHER('@bibitem')),
Expand Down
16 changes: 13 additions & 3 deletions lib/LaTeXML/Package.pm
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ our @EXPORT = (qw(&DefAutoload &DefExpandable
&AddToMacro &AtBeginDocument &AtEndDocument),

# Counter support
qw(&NewCounter &CounterValue &SetCounter &AddToCounter &StepCounter &RefStepCounter &RefStepID &ResetCounter
qw(&NewCounter &CounterValue &SetCounter &AddToCounter &StepCounter &RefStepCounter
&RefStepID &ResetCounter &RefCurrentID
&GenerateID &AfterAssignment
&MaybePeekLabel &MaybeNoteLabel),

Expand Down Expand Up @@ -833,6 +834,15 @@ sub RefStepID {
my $id = CleanID(ToString(DigestLiteral(T_CS("\\the$ctr\@ID"))));
return (id => $id); }

# For UN-numbered units, recycle the last ID without incrementing
# (useful if the last ID-ed box got pruned)
sub RefCurrentID {
my ($type) = @_;
my $ctr = LookupMapping('counter_for_type', $type) || $type;
my $id = CleanID(ToString(DigestLiteral(T_CS("\\the$ctr\@ID"))));
return (id => $id);
}

sub ResetCounter {
my ($ctr) = @_;
AssignRegister('\c@' . $ctr => Number(0), 'global');
Expand Down Expand Up @@ -1779,7 +1789,7 @@ sub defmath_cons {
? $cs : $presentation->unlist); }; }
$STATE->installDefinition(LaTeXML::Core::Definition::Constructor->new($defcs, $paramlist,
($nargs == 0
# If trivial presentation, allow it in Text
# If trivial presentation, allow it in Text
? ($presentation !~ /(?:\(|\)|\\)/
? "?#isMath(<ltx:XMTok role='#role' scriptpos='#scriptpos' stretchy='#stretchy'"
. " font='#font' $cons_attr$end_tok)"
Expand Down Expand Up @@ -2456,7 +2466,7 @@ sub AddToMacro {
else {
local $LaTeXML::Core::State::UNLOCKED = 1; # ALLOW redefinitions that only adding to the macro
DefMacroI($cs, undef, Tokens(map { $_->unlist }
map { (blessed $_ ? $_ : TokenizeInternal($_)) } ($defn->getExpansion, @tokens)),
map { (blessed $_ ? $_ : TokenizeInternal($_)) } ($defn->getExpansion, @tokens)),
nopackParameters => 1, scope => 'global', locked => $$defn{locked}); }
return; }

Expand Down

0 comments on commit 2bef09e

Please sign in to comment.