Fix #3548: Match MathTex subscripts/superscripts by position #4457
+48
−1
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Overview: What does this pull request change?
Fixes issue where
MathTexsubmobjects did not correctly correspond to theirtex_stringswhen using subscripts and superscripts in different orders. The fix uses geometric position matching specifically for script elements (^,_) to handle LaTeX's reordering while preserving sequential matching for non-script elements.Motivation and Explanation: Why and how do your changes improve the library?
Problem: LaTeX compiles expressions like
A ^n _1andA _1 ^nto identical SVG output where subscripts and superscripts may appear in a different order than specified. This causedMathTex('A', '^n', '_1')andMathTex('A', '_1', '^n')to have submobjects that didn't match their originaltex_strings, breaking operations likeget_parts_by_tex()andset_color_by_tex().Solution: Modified
_break_up_by_substrings()method to detect script elements (tex strings starting with^or_) and match them to rendered submobjects based on geometric position (center point). Non-script elements continue using sequential matching to maintain backward compatibility and avoid issues with complex formulas.Impact: Users can now reliably access and manipulate subscripts/superscripts by their tex strings regardless of the order they're specified.
Links to added or changed documentation pages
No documentation changes required.
Further Information and Comments
MathTexmay not correspond to their tex strings #3548test_texmobject.py)test_tex_strings_with_subscripts_and_superscripts()Reviewer Checklist