Skip to content

BUG: Escape LaTeX special characters in the extra text of summaries - #791

Merged
bashtage merged 2 commits into
bashtage:mainfrom
omadson:fix-latex-summary-extra-text
Oct 12, 2026
Merged

bashtage merged 2 commits into
bashtage:mainfrom
omadson:fix-latex-summary-extra-text

Conversation

@omadson

@omadson omadson commented Oct 12, 2026

Copy link
Copy Markdown
Contributor

Hi, @bashtage. Thanks for the great project.

This is the PR I mentioned in #560. I reproduced the problem on current main by exporting the summary of an IV2SLS with underscores in the variable names and compiling the result with pdflatex, which fails. A default LinearFactorModel summary fails the same way, with no user variable names involved.

Summary.as_latex() escapes the table cells (my\_x) but appends the extra text below the tables unchanged. Underscores in the "Endogenous" and "Instruments" lines, and in the fixed text "See full_summary for complete results" of the asset pricing summaries, make the LaTeX invalid. pdflatex stops with ! Missing $ inserted., the failure reported in #560.

On main, an IV2SLS with variables my_x, my_z1, my_z2 ends with

Endogenous: my_x \newline
 Instruments: my_z1, my_z2 \newline

and with this change

Endogenous: my\_x \newline
 Instruments: my\_z1, my\_z2 \newline

statsmodels' Summary.as_latex (same in 0.13.1 to 0.15.0) joins extra_txt without escaping it. The Summary in linearmodels.compat.statsmodels already overrides as_html, so this adds as_latex. It escapes \ & % $ # _ { } ~ ^ in the extra text in one str.translate pass. Tables, as_text(), as_html() and extra_txt are unchanged, and the output equals statsmodels' when there is nothing to escape.

#560 also says the extra text cannot be edited. This does not change that; s = res.summary; s.extra_txt = None already works.

Tests are in linearmodels/tests/shared/test_base.py. The escaping and end-to-end tests fail on main. Linters are clean and the affected test directories pass. I compiled the as_latex() output of an IV2SLS and a LinearFactorModel summary with pdflatex: it fails on main and compiles with this change.

Fixes #560.

@codecov

codecov Bot commented Oct 12, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.60%. Comparing base (6983567) to head (9fa3a90).
⚠️ Report is 52 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff            @@
##             main     #791     +/-   ##
=========================================
  Coverage   99.60%   99.60%             
=========================================
  Files         131      136      +5     
  Lines       24664    26057   +1393     
  Branches     1823     1873     +50     
=========================================
+ Hits        24566    25954   +1388     
+ Misses         39       38      -1     
- Partials       59       65      +6     
Flag Coverage Δ
adder 99.59% <100.00%> (+<0.01%) ⬆️
subtractor 99.59% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@bashtage
bashtage merged commit 80fcf90 into bashtage:main Oct 12, 2026
24 checks passed
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.

Sanitize extra text from summary

2 participants