表格中的文字遍布整个列?

表格中的文字遍布整个列?

在下面的代码中,第一行的条目被拉伸到各自的框中。我知道没有足够的空间让每行完全适合整个框,但我希望它不要分散开来。我只想让它与左侧齐平。

\begin{center}
    \small
    \begin{table}[h]
    \centering
    \begin{tabular}{ | p{2.4cm} | p{2.6cm} | p{2.6cm} | p{2.6cm} |   p{2.6cm} | }
    \hline
     & Volume per Longboard ($\mathrm{in}^{3}$) & Mass per Longboard (lb.) & %
                                Volume for 5,000 units & Cost for 5,000 units (\$)\\ \hline
    Polyurethane & 2 & 2 & 2 & 2\\ \hline
    Bamboo & 3 & 3 & 3 & 3\\ \hline
    Aluminum & 4 & 4 & 4 &4\\ \hline
    Steel Alloy & 5 & 5 & 5 & 5\\
    \hline
    \end{tabular}
        \caption{A summary of materials in the longboard with cost calculations.}
    \end{table}
\end{center}

答案1

您定义了p具有固定宽度的 aragraph 列(为什么?),这会增加比通常的article类(例如)提供的更多的水平空间\textwidth。 (特别是第一p列是有问题的,我只会l在这里使用该列。)

这里有一些关于如何改进表格的想法(没有 改变单元格或头部的内容)。

代码

\documentclass{article}
%\usepackage{showframe}
\usepackage{graphicx}
\usepackage{booktabs}
\usepackage{tabularx}
\newcolumntype{R}{>{\centering\arraybackslash}X}
\newcommand*{\leftHead}[1]{\multicolumn{1}{>{\raggedright\arraybackslash}X}{#1}}
\newcommand*{\leftHeadV}[1]{\multicolumn{1}{>{\raggedright\arraybackslash}X|}{#1}}
\newcommand{\rotHead}[1]{\rlap{\hspace{-.7em}\rotatebox{45}{#1}}}
\begin{document}
\small
\begin{table}
\centering
\begin{tabular}{lrrrr}
                 & \rotHead{Volume per Longboard ($\mathrm{in}^{3}$)} & \rotHead{Mass per Longboard (lb.)} & \rotHead{Volume for 5,000 units} & \rotHead{Cost for 5,000 units (\$)} \\ \toprule
    Polyurethane &                                                  2 &                                  2 &                                2 &                                   2 \\
    Bamboo       &                                                  3 &                                  3 &                                3 &                                   3 \\
    Aluminum     &                                                  4 &                                  4 &                                4 &                                   4 \\
    Steel Alloy  &                                                  5 &                                  5 &                                5 &                                   5 \\ \bottomrule
\end{tabular}
    \caption{A summary of materials in the longboard with cost calculations.}
\end{table}

\small
\begin{table}
\centering
\begin{tabularx}{\linewidth}{|l|R|R|R|R|}
    \hline
                 & \leftHeadV{Volume per Longboard ($\mathrm{in}^{3}$)} & \leftHeadV{Mass per Longboard (lb.)} & \leftHeadV{Volume for 5,000 units} & \leftHeadV{Cost for 5,000 units (\$)} \\ \hline
    Polyurethane & 2                                                    & 2                                    & 2                                  & 2                                     \\ \hline
    Bamboo       & 3                                                    & 3                                    & 3                                  & 3                                     \\ \hline
    Aluminum     & 4                                                    & 4                                    & 4                                  & 4                                     \\ \hline
    Steel Alloy  & 5                                                    & 5                                    & 5                                  & 5                                     \\ \hline
\end{tabularx}
    \caption{A summary of materials in the longboard with cost calculations.}
\end{table}


\begin{table}
\centering
\begin{tabularx}{\linewidth}{lRRRR}
    \toprule
                 & \leftHead{Volume per Longboard ($\mathrm{in}^{3}$)} & \leftHead{Mass per Longboard (lb.)} & \leftHead{Volume for 5,000 units} & \leftHead{Cost for 5,000 units (\$)} \\ \midrule
    Polyurethane & 2                                                   & 2                                   & 2                                 & 2                                    \\
    Bamboo       & 3                                                   & 3                                   & 3                                 & 3                                    \\
    Aluminum     & 4                                                   & 4                                   & 4                                 & 4                                    \\
    Steel Alloy  & 5                                                   & 5                                   & 5                                 & 5                                    \\ \bottomrule
\end{tabularx}
    \caption{A summary of materials in the longboard with cost calculations.}
\end{table}
\end{document}

输出

在此处输入图片描述

答案2

正如 Mico 所提到的,尝试使用tabularx,但对于单元格中左对齐的内容,您需要定义新类型的列:

\documentclass{article}
    \usepackage{tabularx}
    \newcolumntype{L}{>{\raggedright\arraybackslash}X}
    \begin{document} 
\begin{table}[h]\small
    \centering
\caption{A summary of materials in the longboard with cost calculations.}
    \begin{tabularx}{133mm}{|l*{4}{|L}| }
    \hline
     & Volume per Longboard ($\mathrm{in}^{3}$) & Mass per Longboard (lb.) & Volume for 5,000 units & Cost for 5,000 units (\$)\\ \hline
    Polyurethane    & 2 & 2 & 2 & 2\\ \hline
    Bamboo          & 3 & 3 & 3 & 3\\ \hline
    Aluminum        & 4 & 4 & 4 & 4\\ \hline
    Steel Alloy     & 5 & 5 & 5 & 5\\
    \hline
    \end{tabularx}    
\end{table}
    \end{document}

相关内容