[英]Convert .tex LaTeX file to a PDF
I am using Python 3.10.1 and the VSC Editor.我正在使用 Python 3.10.1 和 VSC 编辑器。 My code below creates a LaTeX file then converts it to a pdf.
我下面的代码创建了一个 LaTeX 文件,然后将其转换为 pdf。
The users.tex file successfully creates, but the pdf does not. users.tex 文件成功创建,但 pdf 没有。
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')
I believe that the df.to_latex()
only produces the \begin{tabular}
environment.我相信
df.to_latex()
只会产生\begin{tabular}
环境。 If so, your .tex
file is missing the preamble needed in a LaTeX document.如果是这样,您的
.tex
文件缺少 LaTeX 文档中所需的序言。 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.您必须将 output 插入格式正确的 latex 文件的
\begin{document}
部分。
Edit : I can confirm that the output is as follows:编辑:我可以确认 output 如下:
\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:这意味着您需要将 LaTeX 样板文件包裹起来,如下所示:
\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.编辑 2 :为了使编译工作,您还需要删除
position='center'
参数,如 jjramsey 所述。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.