Skip to content

BUG: Underscores aren't escaped in LaTeX outputs #61296

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
3 tasks done
gtkacz opened this issue Apr 15, 2025 · 1 comment
Closed
3 tasks done

BUG: Underscores aren't escaped in LaTeX outputs #61296

gtkacz opened this issue Apr 15, 2025 · 1 comment
Labels
Bug Needs Triage Issue that has not been reviewed by a pandas team member

Comments

@gtkacz
Copy link

gtkacz commented Apr 15, 2025

Pandas version checks

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

import pandas as pd

df = pd.DataFrame([{"header_1": 1, "header_2": 2}, {"header_1": 3, "header_2": 4}])
print(df.to_latex(index=False))

Issue Description

Underlines in strings ( _ ) should be escaped in LaTeX outputs given than underscores in LaTeX represent subscripts.

Expected Behavior

The expected output of the provided code example should be:

\begin{tabular}{rr}
\toprule
header\_1 & header\_2 \\
\midrule
1 & 2 \\
3 & 4 \\
\bottomrule
\end{tabular}

But instead is:

\begin{tabular}{rr}
\toprule
header_1 & header_2 \\
\midrule
1 & 2 \\
3 & 4 \\
\bottomrule
\end{tabular}

Installed Versions

INSTALLED VERSIONS

commit : 0691c5c
python : 3.12.7
python-bits : 64
OS : Linux
OS-release : 5.15.167.4-microsoft-standard-WSL2
Version : #1 SMP Tue Nov 5 00:21:55 UTC 2024
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : C.UTF-8
LOCALE : C.UTF-8

pandas : 2.2.3
numpy : 2.2.4
pytz : 2025.2
dateutil : 2.9.0.post0
pip : None
Cython : None
sphinx : None
IPython : 9.1.0
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : 4.13.4
blosc : None
bottleneck : None
dataframe-api-compat : None
fastparquet : None
fsspec : 2024.12.0
html5lib : None
hypothesis : None
gcsfs : None
jinja2 : 3.1.6
lxml.etree : None
matplotlib : 3.10.1
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
psycopg2 : None
pymysql : None
pyarrow : 19.0.1
pyreadstat : None
pytest : None
python-calamine : None
pyxlsb : None
s3fs : None
scipy : 1.15.2
sqlalchemy : None
tables : None
tabulate : 0.9.0
xarray : None
xlrd : None
xlsxwriter : None
zstandard : None
tzdata : 2025.2
qtpy : None
pyqt5 : None

@gtkacz gtkacz added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Apr 15, 2025
@gtkacz
Copy link
Author

gtkacz commented Apr 15, 2025

I found the escape bool keyword on the to_latex method, which fixes this. This should be enabled by default though.

@gtkacz gtkacz closed this as completed Apr 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Needs Triage Issue that has not been reviewed by a pandas team member
Projects
None yet
Development

No branches or pull requests

1 participant