梅威瑟:
\documentclass{scrreprt}
\usepackage{tabularx}
\usepackage{booktabs}
\usepackage{threeparttable}
\usepackage{enumitem}
\begin{document}
\begin{threeparttable}[!htbp]
\begin{tabularx}{\linewidth}
{
>{\hsize=0.5\hsize}X
>{\hsize=1\hsize}X
>{\hsize=1\hsize}X
>{\hsize=1\hsize}X
>{\hsize=1.5\hsize}X
}
\toprule
Head 1 & Head 2 & Head 3 & Head 4 & Head 5\\
\midrule
A
&
Some pretty long text that runs over several lines in this table cell
&
\begin{itemize}[leftmargin=*]
\item Some pretty long text that runs over several lines
\item Some short text
\end{itemize}
&
\begin{itemize}[leftmargin=*]
\item Some short text
\item Some short text
\end{itemize}
&
\begin{itemize}[leftmargin=*]
\item Some even longer text that runs over several lines in this itemization within the last column of the table
\item Some very long text that runs over several lines in this itemization within the last column of the table. I'm using it to fill up this cell.
\end{itemize}
\\
\bottomrule
\end{tabularx}
\end{threeparttable}
\end{document}
为什么最后一列的文本没有使用可用宽度?当我将宽度调整为大于 1.5 时,边距会变得更大,就好像最后一列旁边有一个“不可见”的列。我做错了什么?
答案1
一些意见和建议:
在一个
tabularx
环境中,相对宽度类型的列X
必须等于列数类型X
。在您的表中,前者为 4.5,而后者为 5。此外,由于类型的某些列X
包含类似列表的环境,因此有必要编写>{\hsize=<x>\hsize\linewidth=\hsize}X
而不仅仅是
>{\hsize=<x>\hsize}X
在设置相对宽度时。该问题在软件包用户指南的第 4.3 节“列宽”中提到
tabularx
。鉴于第五列的内容比前四列中的任何一列都多得多,为最后一列分配更大的宽度可能是有意义的。在下面的例子中,前四列的相对宽度为 0.85,而最后一列的相对宽度为 1.6。请注意,4*0.85+1.6=3.4+1.6=5,正如所要求的。
此外,由于所有列都很窄,因此最好将其内容排版为右对齐而不是完全对齐。
环境
threeparttable
确实不是采用 形式的选项[!htbp]
。由于您实际上并没有使用环境机制threeparttable
(例如\tnote
宏和tablenotes
环境),因此我建议您改用table
环境。幸运的是,table
环境知道做什么用[!htbp]
。由于您正在加载
enumitem
包,我还建议您设置一个定制的 itemize 类环境,以紧密包装其内容。在下面的代码中,该环境称为myitemize
。(您显然可以自由地想出一个更时髦的名字。)
\documentclass{scrreprt}
\usepackage[english]{babel}
\usepackage{tabularx,ragged2e}
\newcolumntype{L}[1]{>{\RaggedRight\arraybackslash%
\hsize=#1\hsize\linewidth=\hsize}X}
\usepackage{booktabs,enumitem}
%% Create a bespoke itemize-like environment:
\newlist{myitemize}{itemize}{1}
\setlist[myitemize]{label=\textbullet,nosep,left=0pt,
before=\begin{minipage}[t]{\hsize},
after =\end{minipage}}
\begin{document}
\begin{table}[htbp]
\setlength\tabcolsep{4pt} % default: 6pt
\begin{tabularx}{\linewidth}
{@{} *{4}{L{0.85}} L{1.6} @{}} % 4*0.85+1.6=5
\toprule
Head 1 & Head 2 & Head 3 & Head 4 & Head 5\\
\midrule
Some fairly short text
&
Some pretty long text that runs over several lines in this table cell
&
\begin{myitemize}
\item Some pretty long text that runs over several lines
\item Some short text
\end{myitemize}
&
\begin{myitemize}
\item Some short text
\item Some short text
\end{myitemize}
&
\begin{myitemize}
\item Some even longer text that runs over several lines in this itemization within the last column of the table
\item Some very long text that runs over several lines in this itemization within the last column of the table. I'm using it to fill up this cell.
\end{myitemize}
\\
\bottomrule
\end{tabularx}
\end{table}
\end{document}