Rewrite MathTex to make it more robust regarding splitting#4515
Rewrite MathTex to make it more robust regarding splitting#4515behackl merged 61 commits intoManimCommunity:mainfrom
Conversation
updates: - [github.com/astral-sh/ruff-pre-commit: v0.14.7 → v0.14.8](astral-sh/ruff-pre-commit@v0.14.7...v0.14.8) Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
…of curly braces around the extracted part
|
I have located a minor issue related to the "Using text" guide. In the example |
behackl
left a comment
There was a problem hiding this comment.
This is great and I love the much more robust implementation. Kudos and thank you!
Here are some more suggestions:
- It might be nice to have some sort of validation in
SVGMobject.__init__that checks whether all ids from the SVG are contained in the id map at the end of initialising (and log warnings if some are missing?) - The PR description should mention removal/renaming of
get_parts_by_tex,index_of_part_by_tex
Additionally, my review clanker suggests adding two properties to MathTex:
@property
def _substring_matches(self) -> list[tuple[str, str]]:
"""Return only the 'ss' (substring_to_isolate) matches."""
return [(tex, id_) for tex, id_ in self.matched_strings_and_ids if id_.endswith("ss")]
@property
def _main_matches(self) -> list[tuple[str, str]]:
"""Return only the main tex_string matches."""
return [(tex, id_) for tex, id_ in self.matched_strings_and_ids if not id_.endswith("ss")]in order to make break_up_by_substring more readable,
for tex_string, tex_string_id in self._main_matches:
mtp = MathTexPart()
mtp.tex_string = tex_string
mtp.add(*self.id_to_vgroup_dict[tex_string_id].submobjects)
new_submobjects.append(mtp)Not sure whether that is worth it, and/or whether 'ss' should perhaps be made more explicit (I'd like that) -- but other than these I don't really have any substantial feedback. I like it.
Left a few more inline comments, please do take a look!
I like this idea, but is uncertain about how to implement it right now. [1] https://pypi.org/project/svgelements/.
Done
Implemented.
The string can now be customized, as it is defined as follows
The error messages are now given to the logger. Regarding the additional parsing of sub and superscripts I would like to avoid that for now. |
behackl
left a comment
There was a problem hiding this comment.
lets get this merged -- thank you very much for your effort with this! if you have a second, could you add a short description for the changelog to either a comment or in an edit of the pr description?
|
@behackl The MathTex implementation has been updated to make it more robust and fix a number of issues. |
…unity#4515) * Extracted the method get_mob_from_shape_element * Moved more functionality to get_mob_from_shape_element * More cleanup * Parse the svg file while maintaining the group structure. * Make the svg groups available * Handle PERF401 issue * [pre-commit.ci] pre-commit autoupdate (ManimCommunity#4506) updates: - [github.com/astral-sh/ruff-pre-commit: v0.14.7 → v0.14.8](astral-sh/ruff-pre-commit@v0.14.7...v0.14.8) Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> * Added an example of the issue * Experimenting with coloring elements from the latex equation * ... * Regular expression can now match more than one object * Process the string by applying the substrings in the order they match * Code refactoring and added type annotations * ... * Added a lot of test cases * More examples * More examples * Use matched_strings_and_ids to simplify existing methods * Remove unused code * Update get_part_by_tex to use matched_strings_and_ids * This is required for test_MathTable to pass * Ensure that self.texstring is set. * Added more examples from exising issues in the github repo * Ensure that latex groups are maintained by adding an additional pair of curly braces around the extracted part * ExampleScene -> Scene * Added comment * _break_up_by_substrings * Refactor code * Added comment to example * Handle integer inputs well. * Expose the original tex_string * Do not treat the content of substrings_to_isolate as regular expressions. * Updated examples * Update examples * Fix SVMobject caching issue. * Remove traces from brace_notation_split_occurred * Simplify MathTex::_break_up_by_substrings * Fix small issue in tex that in some cases moved elements a tiny bit around * No use of regular expressions for locate substrings. * Updated notes to the set of test cases * Handle issues with the center environment. * Add example * Fix issue with rectangles (e.g. from sqrt) * ConvertToOpenGL * Reduce the number of nesting levels. * Use the specified arg_seperator * Deal with the double curly brace markup * Code cleanup * Code cleanup * Rollback a few changes * Code cleanup * Adjust paths the generated artefacts in tests that rely on MathTex * Added a remark to the using text guide on enclosing snippets in curly braces for substrings_to_isolate to work * Added space around the numerator argument to frac to avoid having double curly braces in the example. This would otherwise trigger MathTex to split the string at that location. * Log errors properly and display some information about the errors and their context. * Code refactoring as suggested by Benjamin --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Benjamin Hackl <devel@benjamin-hackl.at>


Overview: What does this pull request change?
This PR changes a large part of the implementation of the MathTex class.
The reason is to deal with the issues related to splitting the tex string and then try to match the generated objects with parts of the original tex string.
The idea for the PR is based on this discussion on discord.
https://discord.com/channels/581738731934056449/1406977902788218892
The MathTex implementation has been updated to make it more robust and fix a number of issues.
A beneficial side effect is that named groups in svg files can now be accessed through SVGMobject.
Motivation and Explanation: Why and how do your changes improve the library?
This PR changes how
MathTexobjects are created / rendered.To illustrate the changes in the process, please look at the following example
of calling
MathTex.The first step in rendering the MathTex equation, is to join the three separate strings to a single string and add some markup to the string that will allow manim to extract certain parts of the equation afterwards. Each of the three input strings are put in the following markup.
This ends up looking like the following:
This allows us to access a named group in the formed svg file corresponding to each of the three tex strings.
For each string, the elements of
substrings_to_isolateis also located, and marked up with a similar markup.E.g. the
ain the equation ends up like this, where the element is placed in the named group 'unique000ss'.All there marked up strings are joined to the following string (newlines were added to increase readability).
This string is then saved to a
texfile and converted to first a.dviand then a.svgfile using thelatexanddvisvgmcommand line tools.Finally the svg file is loaded by a
SVGMobjectfrom which the named groups can be accessed as demonstrated here.The full code for this example is provided here
and it renders as follows

Links to added or changed documentation pages
Further Information and Comments
This PR is an extension of #4473 "Maintain groups when importing svg files"
The PR removes these methods from
MathTex:get_parts_by_texandindex_of_part_by_tex.Reviewer Checklist