Skip to content

Setbox adjustments#2531

Merged
brucemiller merged 5 commits intomasterfrom
setbox-adjustments
Mar 13, 2025
Merged

Setbox adjustments#2531
brucemiller merged 5 commits intomasterfrom
setbox-adjustments

Conversation

@brucemiller
Copy link
Copy Markdown
Owner

@brucemiller brucemiller commented Mar 12, 2025

In TeX, \setbox doesn't record the current color or background in the box it saves, but when the box is used, it inherits the then current colors. In LaTeXML, we do record those colors (perversely, within the font). So, we have to be careful to preset them to defaults before \setbox creates the box content. When using the box we should update only those colors which were the inherited defaults. Moreover, we must create a copy of the boxes, rather than modifying them in place, since some commands (eg. \copy) still keeps the box in the register. This PR achieves those aims.

Fixes #2455
And is a better alternative to the approaches in:
Closes #2480
Closes #2456

… adjusting a used box later, it should only modify colors that had been the defaults, and it should create a copy of the boxes rather than modify them in place
…with respect to color and background color adjustment
@brucemiller brucemiller requested a review from dginev March 12, 2025 20:19
elsif ($type =~ /^LaTeXML::Core::(?:Box|List|Whatsit|Alignment)$/) { # blessed hashes!
$$cache{$box} = $adj = bless {%$box}, $type; # Cache BEFORE recursion!
map { $$adj{$_} = adjustBoxColor_rec($fg, $bg, $cache, $$box{$_}); } keys %$box; }
elsif($type eq 'LaTeXML::Common::Font'){
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A Font shouldn't make it as an argument to a box-oriented subroutine, surely? This makes porting this to typed languages rather painful, but it is also rather confusing to think about. Why is a font object passed in as $box?

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, bad naming. It's oriented to (digested) objects, not just boxes. Fixed.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is a Font object a Digested object?

I do not understand the design here.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A Font object is inside of a Digested object, and it is exactly the Font objects inside of the digested objects that we're adjusting.

Copy link
Copy Markdown
Collaborator

@dginev dginev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have read this little function five times now and I still find it confusing. I'll approve the PR, but I am really not looking forward to porting it.

Did you check if the tests from my older PR all continued to pass? I noticed that you didn't carry them over.

@brucemiller
Copy link
Copy Markdown
Owner Author

Sure, I did, and more! (there were other tests in your PR, but those were covered in #2527)

@brucemiller
Copy link
Copy Markdown
Owner Author

Maybe this improved comment helps a little?

{\color{red}\colorbox{green}{\copy\mybox}}

\setbox\mybox=\hbox{Red on Green, but \color{green}\colorbox{blue}{Green on Blue!}}
{\color{red}\colorbox{green}{\copy\mybox}}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest adding a test where the color is set outside of the box, before \setbox, and likewise before using \copy.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done!

@brucemiller brucemiller merged commit 0abdec1 into master Mar 13, 2025
26 checks passed
@brucemiller brucemiller deleted the setbox-adjustments branch March 13, 2025 20:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

color from surrounding scope overrides colors inside boxes

3 participants