这邮政提供了一种生成 Latex 代码的便捷方法,我正在尝试重现它。
这是一段 Python 代码,
>>> df = pd.DataFrame({'name': ['Raphael', 'Donatello'],
... 'mask': ['red', 'purple'],
... 'weapon': ['sai', 'bo staff']})
>>> print(df.to_latex(index=False)) # doctest: +NORMALIZE_WHITESPACE
生成了以下 Latex 代码
\begin{tabular}{lll}
\toprule
name & mask & weapon \\
\midrule
Raphael & red & sai \\
Donatello & purple & bo staff \\
\bottomrule
\end{tabular}
我编译了这个代码并得到了这个表
和这个错误
Undefined control sequence
l.15 \toprule
The control sequence at the end of the top line
of your error message was never \def'ed. If you have
misspelled it (e.g., `\hobx'), type `I' and the correct
spelling (e.g., `I\hbox'). Otherwise just continue,
and I'll forget about whatever was undefined.
如何修复?
答案1
\toprule
(和\midrule
或\bottomrule
)定义在书签包。它必须在使用前必须包含。
该软件包通过提供不同宽度的规则并修复一些行距问题,大大改善了表格的视觉外观。标准 latex 用于\hline
在行之间绘制规则。
\documentclass{article}
\usepackage{booktabs}
\begin{document}
\begin{tabular}{lll}
\toprule
name & mask & weapon \\
\midrule
Raphael & red & sai \\
Donatello & purple & bo staff \\
\bottomrule
\end{tabular}
\end{document}