英文:
How do I automatically wrap text inside LaTeX tables produced by pandas.to_latex()?
问题
我正在使用 pandas.DataFrame.to_latex()
将文本填充的 pd.DataFrame
自动转换成 LaTeX
表格。一切看起来都没问题,但如果文本很长,它不会自动换行。使用以下设置也没有帮助:
longtable = True
这是我的设置:
df.to_latex(multicolumn=True, header=True, index_names=False, index=False, longtable=True)
英文:
I am using pandas.DataFrame.to_latex()
to automatically turn a text-filled pd.DataFrame
into a LaTeX
table. Everything seems fine but if the text is long, it is not broken. Using
<!-- language: lang-python-->
longtable = True
does not help. Here are my settings
<!-- language: lang-python -->
df.to_latex(multicolumn = True, header = True, index_names = False,
index = False, longtable = True)
答案1
得分: 1
在LaTeX表格中,您可以使用{table spec}
参数来控制表格的列格式,如下所示:
\begin{tabular}[pos]{table spec}
pandas.DataFrame.to_latex()
可以通过column_format
参数将格式字符串传递给此参数。如果您想要固定两列的宽度,可以使用以下格式:
column_format='p{3.5cm}|p{5cm}'
以下是一个简短的示例,说明如何利用这个方法来解决类似于您的问题:
import pandas as pd
import string
# 创建模拟数据
data_lower = string.ascii_lowercase
data_lower = ' '.join(data_lower[i:i+3] for i in range(0, len(data_lower), 3))
data_upper = string.ascii_uppercase
data_upper = ' '.join(data_upper[i:i+3] for i in range(0, len(data_upper), 3))
df = pd.DataFrame({'this is a long entry in the table in minuscules':
data_lower,
'THIS IS A LONG ENTRY IN THE TABLE IN MAJUSCULES':
data_upper}, index=[0])
df.to_latex(multicolumn=True, header=True, index_names=False,
index=False, column_format='p{3.5cm}|p{5cm}')
这将生成如下所示的输出,将表格的行在3.5cm和5cm处分隔:
\begin{tabular}{p{3.5cm}|p{5cm}}
\toprule
this is a long entry in the table in minuscules & THIS IS A LONG ENTRY IN THE TABLE IN MAJUSCULES \\
\midrule
abc def ghi jkl mno pqr stu vwx yz & ABC DEF GHI JKL MNO PQR STU VWX YZ \\
\bottomrule
\end{tabular}
如果您移除column_format='p{3.5cm}|p{5cm}'
参数,您可能会遇到LaTeX表格中单元格条目过长的问题,这我认为是您的问题所在。
英文:
In LaTeX tables you control the column formats of a table with the {table spec}
argument like this
\begin{tabular}[pos]{table spec}
The pandas.DataFrame.to_latex()
can pass a format string to this argument with the column_format
parameter. If you want to have fixed with of two columns, use e.g.
column_format='p{3.5cm}|p{5cm}'
Here is a short example illustrating how to utilize this to fix a problem comparable to yours:
import pandas as pd
import string
# Creating mock data
data_lower = string.ascii_lowercase
data_lower = ' '.join(data_lower[i:i+3] for i in range(0, len(data_lower), 3))
# >>> abc def ghi jkl mno pqr stu vwx yz
data_upper = string.ascii_uppercase
data_upper = ' '.join(data_upper[i:i+3] for i in range(0, len(data_upper), 3))
# >>> ABC DEF GHI JKL MNO PQR STU VWX YZ
df = pd.DataFrame({'this is a long entry in the table in minuscules':
data_lower,
'THIS IS A LONG ENTRY IN THE TABLE IN MAJUSCULES':
data_upper}, index=[0])
df.to_latex(multicolumn=True, header=True, index_names=False,
index=False, column_format='p{3.5cm}|p{5cm}')
This gives
<!-- language: lang-latex -->
\begin{tabular}{p{3.5cm}|p{5cm}}
\toprule
this is a long entry in the table in minuscules & THIS IS A LONG ENTRY IN THE TABLE IN MAJUSCULES \\
\midrule
abc def ghi jkl mno pqr stu vwx yz & ABC DEF GHI JKL MNO PQR STU VWX YZ \\
\bottomrule
\end{tabular}
and breaks the lines in the table at 3.5cm and 5cm respectively
if you remove the column_format='p{3.5cm}|p{5cm}'
parameter you'll end up in too long cell entries of the latex table, what I believe is your problem.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论