Skip to content

[FR] add option to format \hline in to_latex() #28291

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
stevenlis opened this issue Sep 5, 2019 · 6 comments · Fixed by #40422
Closed

[FR] add option to format \hline in to_latex() #28291

stevenlis opened this issue Sep 5, 2019 · 6 comments · Fixed by #40422
Milestone

Comments

@stevenlis
Copy link

Please considering add a parameter to allow users to add \hline to the latex output so that each two rows will have a horizontal line between them.

For example:

import pandas as pd

df = pd.DataFrame(data={'col_1': [1, 2, 4],
                        'col_2': [4, 3, 2]})
print(df.to_latex(index=False))

It will give me:

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

I wish the output could be

\begin{tabular}{rr}
\toprule
\hline
 col\_1 &  col\_2 \\
\hline
\midrule
     1 &      4 \\ \hline
     2 &      3 \\ \hline
     4 &      2 \\ \hline
\bottomrule
\end{tabular}
@WillAyd WillAyd added the IO LaTeX to_latex label Sep 5, 2019
@ghost
Copy link

ghost commented Jun 2, 2020

As a temporal solution, you can use "replace" function for strings in Python, to change those " \\ " at the end of your table rows by "\\ \hline" to have the horizontal lines.

You need before to save your lateX code in a variable as below :

latex_code = df.to_latex(index=False)

and then you perform the replace :

latex_code = latex_code.replace("\\\n", "\\ \hline\n")
print(latex_code)

Best.

@Ademord
Copy link

Ademord commented Jan 30, 2022

@MoMoussabbih how does the merge help with this? i cant seem to undestand how to do it today without the replace you recommend.

@riklopfer
Copy link

@jreback how can we use the code from that pr to solve this issue? I don't see it either.

@DanielHabenicht
Copy link

DanielHabenicht commented Dec 5, 2022

@attack68 maybe you can shed light on how one can add \hline to each row for latex?

I guess it could be done by using something along the lines of (Not working!):

.set_table_styles([
        {'selector': 'toprule', 'props': ':toprule;'},
        {'selector': 'bottomrule', 'props': ':hline;'},
        {'selector': 'td', 'props': ':hline;'},
    ], overwrite=False)

But I can't seem to get the right selector. Or is it simply not possible? (how is it done for vertical lines? should I create an extra issue?)

@attack68
Copy link
Contributor

attack68 commented Dec 5, 2022

No that doesnt work. The selctors are very specific, as documented. You cannot add HTML selectors and expect it to work - its not coded. (also td is a cell not a row, tr is a row and that wouldnt work either)

If clines does not work for you as required, you could maybe try to be clever and use the format_index to add an hline at the beginning of each row. This is obviously not the intended or expected route but it does work:

This will a bit chaotic with adding styles and css conversion though.

image

@DanielHabenicht
Copy link

Thanks, I think I got it now. For a fully lined table one would execute:

df = pd.DataFrame([["a"], ["b"]])
print(df.style.set_table_styles([
    {'selector': 'toprule', 'props': ':hline;'},
    {'selector': 'midrule', 'props': ':hline;'},
    {'selector': 'bottomrule', 'props': ':hline;'},
], overwrite=False).to_latex(clines="all;data",  column_format="|l|l|"))

Resulting in:

\begin{tabular}{|l|l|}
\hline
 & 0 \\
\hline
0 & a \\
\cline{1-2}
1 & b \\
\cline{1-2}
\hline
\end{tabular}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants