A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/pandas-dev/pandas/issues/50040 below:

add math mode to formatter `escape="latex"` · Issue #50040 · pandas-dev/pandas · GitHub

Feature Type Problem Description

Have a DataFrame that contains both content in latex mathmode (eg. $x=3$), but also content which need to be escaped (eg. something_with_underscore).

With the current escape mode of the style.format() Function it is only possible to either escape both or none of them, instead of being agnostic of what is needed.

import pandas as pd
df = pd.DataFrame([[1,"$x=3$"],[3,"something_with_underscore"]], columns=["A", "B"])
print(df.style.format(escape="latex").to_latex())

# Prints:
# \begin{tabular}{lrl}
#  & A & B \\
# 0 & 1 & \$x=3\$ \\
# 1 & 3 & something\_with\_underscore\\
# \end{tabular}

# or 
print(df.style.to_latex()) # no call to format function

# Prints:
# \begin{tabular}{lrl}
#  & A & B \\
# 0 & 1 & $x=3$ \\
# 1 & 3 & something_with_underscore\\
# \end{tabular}
# ... which is invalid LaTeX
Feature Description
  1. The easy way is to add an escape="latex-math" option and exclude the $ escaping if this string is supplied.

  2. Because there might be other people needing different escapings one could also refactor the escape argument to take both predefined strings (e.g. "html" and "latex") but also a map with characters to escape.

    Could be called like this:

    df.style.format(escape="latex;math").to_latex()
    # or
    df.style.format(escape={
       "%", "\\%")
    }).to_latex()
  3. Both

Alternative Solutions

Replace the \\$ character with $ afterwards.

As mentioned by @attack68, if the content to be escaped is only in the header you can use format_index:

import pandas as pd
df = pd.DataFrame([[1,"$x=3$"],[3,"Text"]], columns=["A", "under_score"])
print(df.style.format_index(axis=1, escape="latex").to_latex())

\begin{tabular}{lrl}
 & A & under\_score \\
0 & 1 & $x=3$ \\
1 & 3 & Text \\
\end{tabular}
Additional Context

Seems like other people have the same problem:
https://stackoverflow.com/questions/48516653/how-to-prevent-pandas-dataframe-to-latex-from-adding-escape-characters-before
https://stackoverflow.com/questions/44260547/pandas-to-latex-escapes-mathmode


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4