我正在尝试使用 pandas to_latex()
函数来创建包含我的表格的 PDF,但每次尝试时我都会得到一个
Overfull \hbox (914.42409pt too wide) in paragraph at lines 1--43
或类似的错误。我知道这是因为我的表格对于页面来说太大了,我的问题是,如何让我的表格变小? to_latex
函数的文档似乎不是很广泛,所以我对解决方案的搜索并不是很有成果。
我创建表格和 .tex 文件的代码是这样的:
r = r.to_latex(na_rep = '0', float_format = fp(2), formatters=format_dict)
message = "\documentclass[12pt]{article}" \
"\\"+"begin{document} " \
"\section{20 Day Trading Stats}"
footer = "\end{document}"
message = message + r + footer
afile=open("outputfile.tex", "wb")
afile.write(message)
其中 fp(2)
和 format_dict
只是参数的格式规范。
如果重要的话,我正在使用 MikTeX 和 LEd 构建 PDF,并且我的 PC 运行的是 Windows 7。
最佳答案
一个解决方案可能是 matrix2latex .此模块允许您将表格置于您喜欢的任何类型的环境中。通常,我会选择:
from matrix2latex import matrix2latex
import numpy
data = numpy.array(r)
matrix2latex(data, 'outputfile.tex', 'table', 'tabular', 'small')
这将导致:
\begin{table}[ht]
\begin{tabular}{cc...cc}
\toprule
\begin{small}
item1 & item2 &..... \\
...
\end{small}
\end{tabular}
\end{table}
显然,您可以将“小”更改为您喜欢的任何内容,但请记住参数的顺序很重要。
希望对您有所帮助!
关于python - Pandas 数据帧 to_latex : how to fit tables on a page?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17694593/