fix: preserve custom LaTeX env shorthand macros during translation#2273
Open
octo-patch wants to merge 1 commit into
Open
fix: preserve custom LaTeX env shorthand macros during translation#2273octo-patch wants to merge 1 commit into
octo-patch wants to merge 1 commit into
Conversation
…ixes binary-husky#2240) Physics papers commonly define shorthand commands like \def\be{\begin{equation}}. These shorthands were not recognized by the segmentation logic, causing enclosed math to be incorrectly sent to GPT for translation. Add extract_user_defined_env_patterns() to detect \def and \newcommand declarations that map to \begin{X}/\end{X} pairs, then generate corresponding preserve patterns with negative lookahead to avoid false matches on longer commands.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixes #2240
Problem
Physics and math papers frequently define shorthand commands for equation environments using
\defor\newcommand, e.g.:The LaTeX segmentation logic in
latex_actions.pyonly recognized standard\begin{equation}...\end{equation}patterns. When authors used these shorthands, the enclosed math content was not preserved from GPT translation — causing it to be sent for translation (corrupting the math) or replaced with incorrect LaTeX delimiters like\[.Solution
Added
extract_user_defined_env_patterns()inlatex_toolbox.pythat:\def\cmd{\begin{envname}}and\def\cmd{\end{envname}}declarations (also handles\newcommand/\renewcommand)(?![a-zA-Z])to avoid false matches on longer commands that share the same prefix (e.g.,\bemust not match\begin)These patterns are then applied in
split_subprocess()immediately after the standard equation/align patterns, so the custom shorthand blocks are correctly preserved from GPT translation.Testing
Manually tested with a synthetic TeX document containing
\def\be{\begin{equation}}and\def\ee{\end{equation}}:\be...\eeblocks are correctly marked as PRESERVE\begin{equation}is NOT accidentally matched by the\bepattern (negative lookahead works)