I'm trying to generate a latex file with a table generated from a Pandas dataframe without success.
I'm using pylatex library to generate the entire file
The example code is as follows:
geometry_options = {"tmargin": "1cm", "lmargin": "10cm"}
doc = Document(geometry_options=geometry_options)
with doc.create(Section("Top 10 canales")) as target:
target.append(df.to_latex())
doc.generate_pdf('full', clean_tex=False)
The output of print(df.to_latex())
is:
\begin{tabular}{lr}
\toprule
{} & ATCCO \\
\midrule
All channels & 16395.0 \\
OLATV\_El Trece & 4421.0 \\
OLATV\_TN & 2801.0 \\
OLATV\_Telefe & 2113.0 \\
OLATV\_ESPN 2 & 1632.0 \\
OLATV\_Atcco Canal 2 & 1361.0 \\
OLATV\_America & 925.0 \\
TV Publica & 838.0 \\
OLATV\_Fox Sports 2 & 536.0 \\
OLATV\_ESPN & 497.0 \\
\bottomrule
\end{tabular}
If i change the Section()
statement for the Tabular()
statement it returns an error at the time of compiling into pdf
The final tex file is as follows:
\documentclass{article}%
\usepackage[T1]{fontenc}%
\usepackage[utf8]{inputenc}%
\usepackage{lmodern}%
\usepackage{textcomp}%
\usepackage{lastpage}%
\usepackage{geometry}%
\geometry{tmargin=1cm,lmargin=10cm}%
%
%
%
\begin{document}%
\normalsize%
\section{Top 10 canales}%
\label{sec:Top10canales}%
\textbackslash{}begin\{tabular\}\{lr\}\newline%
\textbackslash{}toprule\newline%
\{\} \& ATCCO \textbackslash{}\textbackslash{}\newline%
\textbackslash{}midrule\newline%
All channels \& 16395.0 \textbackslash{}\textbackslash{}\newline%
OLATV\textbackslash{}\_El Trece \& 4421.0 \textbackslash{}\textbackslash{}\newline%
OLATV\textbackslash{}\_TN \& 2801.0 \textbackslash{}\textbackslash{}\newline%
OLATV\textbackslash{}\_Telefe \& 2113.0 \textbackslash{}\textbackslash{}\newline%
OLATV\textbackslash{}\_ESPN 2 \& 1632.0 \textbackslash{}\textbackslash{}\newline%
OLATV\textbackslash{}\_Atcco Canal 2 \& 1361.0 \textbackslash{}\textbackslash{}\newline%
OLATV\textbackslash{}\_America \& 925.0 \textbackslash{}\textbackslash{}\newline%
TV Publica \& 838.0 \textbackslash{}\textbackslash{}\newline%
OLATV\textbackslash{}\_Fox Sports 2 \& 536.0 \textbackslash{}\textbackslash{}\newline%
OLATV\textbackslash{}\_ESPN \& 497.0 \textbackslash{}\textbackslash{}\newline%
\textbackslash{}bottomrule\newline%
\textbackslash{}end\{tabular\}\newline%
%
\end{document}
Without the render:
tabular
output is set verbatim-like in the TeX code. You may have to play around with the switchescape=False
(saw it here: Python: Pandas to latex - Issues with the backslash)\textbackslash{}begin\{tabular\}\{lr\}
makes the text\begin{tabular}{lr}
in the typeset output, you want\begin{tabular}{lr}
in the source.Tabular()
! LaTeX Error: Illegal character in array arg. See the LaTeX manual or LaTeX Companion for explanation. Type H <return> for immediate help. ... l.14 \begin{tabular}{Top 10 canales} % Overfull \hbox (1421.54367pt too wide) in paragraph at lines 14--33 [][]
escape = False
and still get the error