表格仅占据页面的一部分吗?

表格仅占据页面的一部分吗?

我目前正在尝试创建一个具有特定宽度的表格(表格和内容本身)。经过多次尝试,我找到了一个可行的解决方案,但我的问题是:这是可行的方法吗?还是有更好的方法?

\begin{tabular}{llp{3in}}
 1 & 2 & 3 this is really long \\
 4 & 5 & 6 this is really long this is really long\\
 7 & 8 & 9 this is really long this is really long this is really long\\
 10&11 & 12 this is really long this is really long this is really long this is really long\\
\end{tabular}

表格的宽度仅相当于页面的一半

答案1

您可以使用tabular*tabularx环境来创建具有预定宽度的表格,例如0.5\textwidth

如下面的截图所示,即使整体宽度相同(通过构造!),tabular*和环境的外观也有很大不同。通过扩大列间空白来工作,而通过扩大列的宽度来工作(同时保持列间空白为)。tabularxtabular*tabularx2\tabcolsep

在此处输入图片描述

\documentclass{article} 
\usepackage{tabularx}
\begin{document} 
\hrule  %% just to demonstate the overall width of the text block
\medskip
\centering
\begin{tabular}{lll}
\hline
 1 & 2 & 3 \\
 4 & 5 & 6 \\
 7 & 8 & 9 \\
 10&11 & 12\\
\hline
\end{tabular}

\medskip
\begin{tabular*}{0.5\textwidth}{l@{\extracolsep{\fill}}ll} %% note "@{\extracolsep{\fill}}"
\hline
 1 & 2 & 3 \\
 4 & 5 & 6 \\
 7 & 8 & 9 \\
 10&11 & 12\\
\hline
\end{tabular*}

\medskip
\begin{tabularx}{0.5\textwidth}{XXX}  % note: "X" instead of "l"
\hline
 1 & 2 & 3 \\
 4 & 5 & 6 \\
 7 & 8 & 9 \\
 10&11 & 12\\
\hline
\end{tabularx}
\end{document}

附录:我刚刚注意到您更新了表格代码,更新后的表格的第三列现在包含需要扩展到多行的文本。为了让更新后的表格占据宽度0.5\textwidth,我建议您使用tabularx环境并使用该包的列类型的修改形式X而不是当前列p{3in}。我建议您使用修改后的X列类型,因为通常最好在右对齐模式下排版窄列文本,而不是在完全对齐模式下排版。

在此处输入图片描述

\documentclass{article} 
\usepackage{tabularx}  % for tabularx environment
\usepackage{ragged2e}  % for \RaggedRight macro
\newcolumntype{Y}{>{\RaggedRight\arraybackslash}X} % set contents of "Y" ragged-right
\begin{document}
\hrule  %% just to demonstrate width of text block
\medskip
\centering
\begin{tabularx}{0.5\textwidth}{llY}
\hline
 1 & 2 & 3 this is really long \\
 4 & 5 & 6 this is really long this is really long\\
 7 & 8 & 9 this is really long this is really long this is really long\\
 10&11 & 12 this is really long this is really long this is really long this is really long\\
\hline
\end{tabularx}
\end{document}

相关内容