Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions pandas/io/formats/style_render.py
Original file line number Diff line number Diff line change
Expand Up @@ -1564,7 +1564,12 @@ def relabel_index(
levels_ = refactor_levels(level, obj)

def alias_(x, value):
"""
Checks and returns formatted values based on specific conditions.
"""
if isinstance(value, str):
if value.startswith("$") and value.endswith("$"):
return value
return value.format(x)
return value

Expand Down
24 changes: 24 additions & 0 deletions pandas/tests/io/formats/test_to_latex.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,30 @@ def test_to_latex_number_of_items_in_header_missmatch_raises(
with pytest.raises(ValueError, match=msg):
df.to_latex(header=header)

def test_to_latex_with_latex_header(self):
df = DataFrame({"a": [1, 2]})
result = df.to_latex(header=[r"$\bar{y}$"], index=False, column_format="l")
expected = r"""
\begin{tabular}{l}
\toprule
$\bar{y}$ \\
\midrule
1 \\
2 \\
\bottomrule
\end{tabular}
"""

# Function to remove leading spaces from each line
def remove_leading_spaces(s):
return "\n".join(line.lstrip() for line in s.strip().split("\n"))

# Normalize both result and expected strings
result_clean = remove_leading_spaces(result)
expected_clean = remove_leading_spaces(expected)

assert result_clean == expected_clean

def test_to_latex_decimal(self):
# GH 12031
df = DataFrame({"a": [1.0, 2.1], "b": ["b1", "b2"]})
Expand Down
Loading