KateMac
KateMac

Reputation: 33

Convert .tex LaTeX file to a PDF

I am using Python 3.10.1 and the VSC Editor. My code below creates a LaTeX file then converts it to a pdf.

The users.tex file successfully creates, but the pdf does not.

How can I resolve this issue?

import pandas as pd
import os  
from pdflatex import PDFLaTeX

cols = ('Name', 'Username', 'Fav Color'); 
df = pd.DataFrame ((('Peter',  '1998_pete2', (139, 0, 139)),
                    ('Leon',  'Gar_man',    (143, 188, 143)),
                    ('Isla',  'Isla2021',   (173, 216, 230)),
                    ('Blake', 'kirbster',   (147, 112, 219))), columns=cols)

print(df)
df.to_latex('users.tex', index=False, caption='User Details', position='center')

pdf = os.system('pdflatex users.tex')

Upvotes: 0

Views: 3581

Answers (1)

sekthor
sekthor

Reputation: 514

I believe that the df.to_latex() only produces the \begin{tabular} environment. If so, your .tex file is missing the preamble needed in a LaTeX document. Thus the build of that document would fail.

You would have to insert the output into the \begin{document} part a properly formatted latex file.

Edit: I can confirm that the output is as follows:

\begin{table}[center]
\centering
\caption{User Details}
\begin{tabular}{lll}
\toprule
 Name &   Username &       Fav Color \\
\midrule
Peter & 1998\_pete2 &   (139, 0, 139) \\
 Leon &    Gar\_man & (143, 188, 143) \\
 Isla &   Isla2021 & (173, 216, 230) \\
Blake &   kirbster & (147, 112, 219) \\
\bottomrule
\end{tabular}
\end{table}

This means you need the LaTeX boiler plate document wrapped around it like such:

\documentclass{article}
\usepackage{booktabs}
\begin{document}

\begin{table}
\centering
\caption{User Details}
\begin{tabular}{lll}
\toprule
 Name &   Username &       Fav Color \\
\midrule
Peter & 1998\_pete2 &   (139, 0, 139) \\
 Leon &    Gar\_man & (143, 188, 143) \\
 Isla &   Isla2021 & (173, 216, 230) \\
Blake &   kirbster & (147, 112, 219) \\
\bottomrule
\end{tabular}
\end{table}

\end{document}

Edit 2: In order for the compilation to work you will also need to get rid of the position='center' argument, as mentioned by jjramsey.

Upvotes: 1

Related Questions