如何在由pandas.to_latex()生成的LaTeX表格中自动换行文本?

huangapple go评论90阅读模式
英文:

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=&#39;p{3.5cm}|p{5cm}&#39;

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 = &#39; &#39;.join(data_lower[i:i+3] for i in range(0, len(data_lower), 3))
# &gt;&gt;&gt; abc def ghi jkl mno pqr stu vwx yz
data_upper = string.ascii_uppercase
data_upper = &#39; &#39;.join(data_upper[i:i+3] for i in range(0, len(data_upper), 3))
# &gt;&gt;&gt; ABC DEF GHI JKL MNO PQR STU VWX YZ

df = pd.DataFrame({&#39;this is a long entry in the table in minuscules&#39;:
                    data_lower,
                   &#39;THIS IS A LONG ENTRY IN THE TABLE IN MAJUSCULES&#39;:
                    data_upper}, index=[0])

df.to_latex(multicolumn=True, header=True, index_names=False,
              index=False, column_format=&#39;p{3.5cm}|p{5cm}&#39;)

This gives

<!-- language: lang-latex -->

\begin{tabular}{p{3.5cm}|p{5cm}}
    \toprule
    this is a long entry in the table in minuscules &amp; THIS IS A LONG ENTRY IN THE TABLE IN MAJUSCULES \\
    \midrule
    abc def ghi jkl mno pqr stu vwx yz &amp; 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

如何在由pandas.to_latex()生成的LaTeX表格中自动换行文本?

if you remove the column_format=&#39;p{3.5cm}|p{5cm}&#39; parameter you'll end up in too long cell entries of the latex table, what I believe is your problem.

huangapple
  • 本文由 发表于 2020年1月3日 21:19:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/59579294.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定