Skip to content

BUG: DataFrame.style.bar.to_latex does not output valid LaTeX #54082

@olofahlen

Description

@olofahlen

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
print(pd.DataFrame([[123]]).style.bar().to_latex())

Issue Description

The output of the code above is

\begin{tabular}{lr}
 & 0 \\
0 & \width10em \backgroundlinear-gradient(90deg, #d65f5f 100.0%, transparent 100.0%) 123 \\
\end{tabular}

Expected Behavior

This does not look like valid LaTeX at all.

  1. There is probably no command called width10em.
  2. LaTeX commands cannot contain -. Here it looks like \backgroundlinear-gradient is supposed to correspond to something but ends up calling a LaTeX command called \backgroundlinear.
  3. Arguments to commands are not passed with parentheses, so (90deg, #d65f5f 100.0%, transparent 100.0%) looks incorrect.
  4. LaTeX comments are created by using %, so the line above becomes 0 & \width10em \backgroundlinear-gradient(90deg, #d65f5f 100.0.
  5. The output does not compile even when including all packages referenced on https://pandas.pydata.org/docs/reference/api/pandas.io.formats.style.Styler.to_latex.html. The compiler fails with Undefined control sequence.

Installed Versions

INSTALLED VERSIONS

commit : 2e218d1
python : 3.9.15.final.0
python-bits : 64
OS : Linux
OS-release : 3.10.0-1160.62.1.el7.x86_64
Version : #1 SMP Tue Apr 5 13:02:25 PDT 2022
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.5.3
numpy : 1.24.1
pytz : 2022.7.1
dateutil : 2.8.2
setuptools : 66.1.1
pip : 23.0
Cython : None
pytest : 7.2.2
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.9.2
html5lib : None
pymysql : 1.0.3
psycopg2 : None
jinja2 : 3.1.2
IPython : 8.9.0
pandas_datareader: None
bs4 : 4.11.2
bottleneck : None
brotli :
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : 3.6.3
numba : None
numexpr : None
odfpy : None
openpyxl : 3.1.0
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.10.0
snappy : None
sqlalchemy : 2.0.16
tables : None
tabulate : 0.9.0
xarray : None
xlrd : 2.0.1
xlwt : None
zstandard : 0.19.0
tzdata : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    Closing CandidateMay be closeable, needs more eyeballsIO LaTeXto_latexStylerconditional formatting using DataFrame.style

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions