Fix LaTeX math mode in adjustbox - wrap formula in $...$#48
Fix LaTeX math mode in adjustbox - wrap formula in $...$#48bdtran2002 wants to merge 2 commits intomainfrom
Conversation
There was a problem hiding this comment.
Pull request overview
This PR updates the LaTeX generation in build_latex_for_formulas to ensure formulas rendered inside \adjustbox are explicitly wrapped in math mode delimiters, improving LaTeX compatibility when scaling equations.
Changes:
- Wrap formula content inside
\adjustbox{...}{...}with$...$to force math mode. - Adjust the formula rendering line used for non-“special class” formulas.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
backend/api/latex_utils.py
Outdated
| escaped_name = name.replace("\\", "\\textbackslash ").replace("&", "\\&").replace("%", "\\%").replace("#", "\\#").replace("_", "\\_").replace("^", "\\textasciicircum ").replace("{", "\\{").replace("}", "\\}") | ||
| body_lines.append("\\textbf{" + escaped_name + "}") | ||
| body_lines.append("\\[ \\adjustbox{max width=\\linewidth}{\\displaystyle " + latex + "} \\]") | ||
| body_lines.append("\\[ \\adjustbox{max width=\\linewidth}{$" + latex + "$} \\]") |
There was a problem hiding this comment.
Wrapping the formula in $...$ inside \adjustbox forces textstyle math, so fractions/summations will render smaller than intended even though the outer environment is display math (\[ ... \]). To preserve the previous display-style rendering, include \displaystyle inside the math delimiters (e.g., {$\displaystyle ...$}) or use an equivalent display-style wrapper inside the adjustbox content.
backend/api/latex_utils.py
Outdated
| escaped_name = name.replace("\\", "\\textbackslash ").replace("&", "\\&").replace("%", "\\%").replace("#", "\\#").replace("_", "\\_").replace("^", "\\textasciicircum ").replace("{", "\\{").replace("}", "\\}") | ||
| body_lines.append("\\textbf{" + escaped_name + "}") | ||
| body_lines.append("\\[ \\adjustbox{max width=\\linewidth}{\\displaystyle " + latex + "} \\]") | ||
| body_lines.append("\\[ \\adjustbox{max width=\\linewidth}{$" + latex + "$} \\]") |
There was a problem hiding this comment.
This changes the LaTeX emitted by the generate-sheet endpoint but there are currently no tests asserting the \adjustbox{...}{...} formula wrapping. Adding a regression test that checks the generated TeX contains the expected math-mode wrapper (and \displaystyle if kept) would prevent future formatting/compilation regressions.
There was a problem hiding this comment.
@copilot apply changes based on this feedback
There was a problem hiding this comment.
Applied both changes in commit ebd0868:
latex_utils.py: Changed$...$back to$\displaystyle ...$inside\adjustbox, so fractions/summations render at display size rather than the smaller text-style.tests.py: Addedtest_generate_sheet_formula_uses_displaystyle_in_adjustboxwhich asserts the generated TeX contains both\adjustbox{max width=\linewidth}and$\displaystyle.
Agent-Logs-Url: https://github.com/ChicoState/cheat-sheet/sessions/6cf938c1-4164-4c40-b003-4cd063d56458 Co-authored-by: bdtran2002 <19805162+bdtran2002@users.noreply.github.com>
This pull request makes a small change to the LaTeX rendering logic for formulas in the
build_latex_for_formulasfunction. The main update is to ensure that formulas are wrapped in$... math mode delimiters when rendered inside the\adjustbox` command, which improves LaTeX formatting and compatibility.LaTeX rendering improvement:
build_latex_for_formulas(inlatex_utils.py) to wrap formulas with$... inside\adjustbox`, ensuring proper math mode handling.