我有一张带有长标题名称的表格。我想修复列的宽度。当我这样做时,文本会从列中溢出。有什么修复方法吗?
以下是示例
\begin{table*}[ht]
\begin{tabular}{|l|r|r|r|r|r|}
\hline
Image & Score & here is a lot of text & agian a lot &a lot and a lot& and the same here\\ \hline
a \(left\) & 1 & 1 & 1 & 1 & 1 \\ \hline
b \(right\)& 1 &1 & 1 & 1 & 1 \\
\end{tabular}
\end{table*}
答案1
p{}
下面是使用列类型或将内容包装在中的示例\parbox
:
对于您的具体示例,由于您希望数据列按照表标题对齐,因此您可以使用类似以下内容:
\newcommand*{\TitleParbox}[1]{\parbox[c]{1.75cm}{\raggedright #1}}%
\begin{tabular}{|l|r|r|r|r|r|}
\hline
Image & Score & \parbox[c]{1.5cm}{\raggedright here is a lot of text} & \TitleParbox{again a lot} &\TitleParbox{a lot and a lot}& \TitleParbox{and the same here}\\ \hline
a \(left\) & 1 & 1 & 1 & 1 & 1 \\ \hline
b \(right\)& 1 &1 & 1 & 1 & 1 \\ \hline
\end{tabular}
得出的结果是:
笔记:
- 该
showframe
包用于显示页面边距。
代码:
\documentclass{article}
\usepackage{showframe}
\newcommand*{\Title}{A very long table heading}%
\newcommand*{\TitleInParbox}{\parbox[c]{0.3\linewidth}{\Title}}%
\begin{document}
\section{Tabular with left alignment}
\noindent
\begin{tabular}{lll}
\Title & \Title & \Title
\end{tabular}
\section{Using the p\{\} column type:}
\noindent
\begin{tabular}{p{0.3\linewidth} p{0.3\linewidth} p{0.3\linewidth}}
\Title & \Title & \Title
\end{tabular}
\section{Using a parbox:}
\noindent
\begin{tabular}{lll}
\TitleInParbox & \TitleInParbox & \TitleInParbox
\end{tabular}
\end{document}
答案2
除了考虑@PeterGrill 的回答,您还应该考虑使用tabularx
环境。它提供了一种称为的列类型X
,它(实际上)是一p
列——即,它允许换行——但如果您想让表格适合给定的宽度,例如,\textwidth
...
您可能还想考虑让表格看起来更“开放”。在下面的屏幕截图中,第一个表格遵循代码中提供的布局(除了tabularx
使用环境,修改了X
第 3 至第 6 列的列类型),而第二个表格通过删除所有垂直规则并使用较少但间距适当的水平规则来实现“开放”外观。
\documentclass{article}
\usepackage{tabularx,ragged2e,booktabs}
\newcolumntype{L}{>{\RaggedRight\arraybackslash}X} % ragged-right version of "X"
\begin{document}
\begin{table}[t]
\caption{Lots of vertical and horizontal rules\strut}
\begin{tabularx}{\textwidth}{|l|r|L|L|L|L|}
\hline
Image & Score & here is a lot of text & again a lot & a lot and a lot & and the same here \\ \hline
a \emph{left} & 1 & 1 & 1 & 1 & 1 \\ \hline
b \emph{right} & 1 &1 & 1 & 1 & 1 \\ \hline
\end{tabularx}
\end{table}
\begin{table}[h]
\caption{No vertical rules; fewer but well-spaced horizontal rules}
\begin{tabularx}{\textwidth}{@{} lrLLLL @{}}
\toprule
Image & Score & here is a lot of text & again a lot & a lot and a lot & and the same here \\ \midrule
a \emph{left} & 1 & 1 & 1 & 1 & 1 \\
b \emph{right} & 1 &1 & 1 & 1 & 1 \\ \bottomrule
\end{tabularx}
\end{table}
\end{document}