Skip to content

BUG: to_latex does not escape % with percent formatter #61478

Open
@stertingen

Description

@stertingen

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

print(pd.DataFrame({"x": [0.1, 0.5, 1.0]}).to_latex(formatters={"x": "{:.0%}"}, escape=True))
print(pd.DataFrame({"x": [0.1, 0.5, 1.0]}).style.format("{:.0%}", escape="latex").to_latex())

Issue Description

When using "{:.0%}" to format floating point values as percentages, the percent signs are not correctly escaped even if explicitly specified. This applies to DataFrame.to_latex and Styler.to_latex.

Output:

\begin{tabular}{lr}
\toprule
 & x \\
\midrule
0 & 10% \\
1 & 50% \\
2 & 100% \\
\bottomrule
\end{tabular}

\begin{tabular}{lr}
 & x \\
0 & 10% \\
1 & 50% \\
2 & 100% \\
\end{tabular}

Expected Behavior

\begin{tabular}{lr}
\toprule
 & x \\
\midrule
0 & 10\% \\
1 & 50\% \\
2 & 100\% \\
\bottomrule
\end{tabular}

\begin{tabular}{lr}
 & x \\
0 & 10\% \\
1 & 50\% \\
2 & 100\% \\
\end{tabular}

Installed Versions

INSTALLED VERSIONS ------------------ commit : 0691c5c python : 3.12.10 python-bits : 64 OS : Windows OS-release : 11 Version : 10.0.26100 machine : AMD64 processor : Intel64 Family 6 Model 165 Stepping 2, GenuineIntel byteorder : little LC_ALL : None LANG : None LOCALE : de_DE.cp1252

pandas : 2.2.3
numpy : 2.0.2
pytz : 2024.2
dateutil : 2.9.0.post0
pip : 25.0.1
Cython : 3.0.11
sphinx : None
IPython : 8.30.0
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : 4.12.3
blosc : None
bottleneck : 1.4.2
dataframe-api-compat : None
fastparquet : None
fsspec : 2024.10.0
html5lib : None
hypothesis : None
gcsfs : None
jinja2 : 3.1.4
lxml.etree : None
matplotlib : 3.10.0
numba : 0.60.0
numexpr : 2.10.2
odfpy : None
openpyxl : 3.1.5
pandas_gbq : None
psycopg2 : 2.9.10
pymysql : None
pyarrow : 18.1.0
pyreadstat : None
pytest : None
python-calamine : None
pyxlsb : None
s3fs : None
scipy : 1.14.1
sqlalchemy : 2.0.36
tables : None
tabulate : 0.9.0
xarray : None
xlrd : None
xlsxwriter : None
zstandard : 0.23.0
tzdata : 2024.2
qtpy : None
pyqt5 : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugIO LaTeXto_latexNeeds DiscussionRequires discussion from core team before further action

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions