I am creating a table by passing DataFrame
to to_latex()
, which is generating a table without a border. Is there any way to add it?
Asked
Active
Viewed 2,673 times
2

onlyphantom
- 8,606
- 4
- 44
- 58

P.Natu
- 131
- 1
- 3
- 12
-
Seems not. I just `df.to_latex(...).replace('{lllllllllllllll}', '...', 1)`. – user26742873 Jan 22 '21 at 06:18
2 Answers
1
import re # Use regular expression to search for column declaration section of LaTeX table
tex_content = df.to_latex(index=True, escape=False) #Or whatever options you want/need
print(tex_content)
re_borders = re.compile(r"begin\{tabular\}\{([^\}]+)\}")
borders = re_borders.findall(tex_content)[0]
borders = '|'.join(list(borders))
tex_content = re_borders.sub("begin{tabular}{" + borders + "}", tex_content)
print(tex_content)
# Or save tex_content to a ".tex" file

Cesar
- 63
- 4
-2
Refer this, https://www.overleaf.com/learn/latex/Tables
\begin{center}
\begin{tabular}{ |c|c|c| }
\hline
cell1 & cell2 & cell3 \\
cell4 & cell5 & cell6 \\
cell7 & cell8 & cell9 \\
\hline
\end{tabular}
\end{center}

MattAllegro
- 6,455
- 5
- 45
- 52

Deep Sutariya
- 1
- 1
-
I'm pretty sure the original poster is asking for a programmatic way to do that. – Dominik Stańczak Mar 18 '19 at 09:17
-
yup. exactly. Please let me know if there is any programmatic way to add border. I have large number of tables which are getting printed using to_latex(). Can't add border manually for all these tables(as size vary for each table). – P.Natu Mar 18 '19 at 09:58