我已使用多列和 tabularx 来编写我打算在 LaTex 中编写的表格。但生成的表格超出了页面宽度。我还在 tabularx 中添加了文本宽度,但它没有按预期工作。当然,我犯了一些错误,但无法弄清楚。很高兴得到纠正。谢谢
\begin{table}
\centering
\caption{my table}
\label{mytable}
\begin{tabularx}{\textwidth}{|c|c|c|c|X|c|X|}
\hline
\multicolumn{1}{|l|}{Type} & \multicolumn{1}{l|}{Product name} & \multicolumn{1}{l|}{\begin{tabular}[c]{@{}l@{}}Diameter\\(mm)\end{tabular}} & \multicolumn{1}{l|}{\begin{tabular}[c]{@{}l@{}}Tensile strength\\(MPa)\end{tabular}} & \multicolumn{1}{l|}{\begin{tabular}[c]{@{}l@{}}Young's modulus\\(GPa)\end{tabular}} & \multicolumn{1}{l|}{\begin{tabular}[c]{@{}l@{}}Elongation\\(\%)\end{tabular}} & Application \\
\hline
\multirow{5}{*}{\begin{tabular}[c]{@{}c@{}}class A\\~\\~\\~\\ \end{tabular}} & abc123 & 0.014 & 1500 & 36 & 7 & four words text here \\
\cline{2-7} & abc123 & 0.040 & 1400 & 36 & 6.5 & four words text here\\
\cline{2-7} & abc123 & 0.20 & 975 & 27 & 9 & four words text here \\
\cline{2-7} & abc123 & 0.31 & 975 & 26 & 6 & four words text here \\
\cline{2-7} & abc123 & 0.67 & 900 & 23 & 9 & four words text here \\ \hline
\multicolumn{1}{|l|}{\multirow{3}{*}{class B}} & abc123 & 0.027 & 1560 & 39 & 6.5 & four words text here \\
\cline{2-7} \multicolumn{1}{|l|}{} & abc123 & 0.040 & 1600 & 41 & 6.5 & four words text here \\
\cline{2-7} \multicolumn{1}{|l|}{} & abc123 & 0.10 & 1200 & 28 & 12.5 & four words text here \\
\hline
\end{tabularx}
\end{table}
另外,我希望将第一列的合并列文本的垂直对齐方式设置为居中而不是顶部。
答案1
环境宽度超出文本块宽度的原因tabularx
是您不允许在标题单元格中换行。我建议您 (a) 彻底简化标题材料,主要是通过摆脱\multicolumn
“包装器”,以及 (b) 对所有六个数据列使用居中版本的列类型X
。或者,考虑在标题中使用单独的行来放置有关测量单位的信息。
\documentclass{article}
\usepackage{tabularx} % for "tabularx" environment and "X" column type
\usepackage{ragged2e} % for "\Centering" macro
\newcolumntype{C}{>{\Centering\arraybackslash\hspace{0pt}}X}
%% "\hspace{0pt}" allows for hyphenation of first word in header cells
\begin{document}
\begin{table}
%%\centering % not needed
\caption{My table}
\label{tab:mytable}
\setlength\extrarowheight{2pt} % for a slightly more open "look"
\setlength\tabcolsep{3pt} % default: 6pt
\begin{tabularx}{\textwidth}{|l|*{6}{C|}}
\hline
Type & Product name & Diameter & Tensile strength
& Young's modulus & Elongation & Application \\
& & (mm) & (MPa) & (GPa) & (\%) & \\ % place units of measurement on a separate row
\hline
Class A & abc123 & 0.014 & 1500 & 36 & 7 & four words text here \\ \cline{2-7}
& abc123 & 0.040 & 1400 & 36 & 6.5 & four words text here \\ \cline{2-7}
& abc123 & 0.20 & 975 & 27 & 9 & four words text here \\ \cline{2-7}
& abc123 & 0.31 & 975 & 26 & 6 & four words text here \\ \cline{2-7}
& abc123 & 0.67 & 900 & 23 & 9 & four words text here \\
\hline
Class B & abc123 & 0.027 & 1560 & 39 & 6.5 & four words text here \\ \cline{2-7}
& abc123 & 0.040 & 1600 & 41 & 6.5 & four words text here \\ \cline{2-7}
& abc123 & 0.10 & 1200 & 28 & 12.5& four words text here \\
\hline
\end{tabularx}
\end{table}
\end{document}
附录:如果这是我的表格,我会努力让它看起来更加开放和“吸引人”。我主要通过 (a) 删除所有垂直规则和 (b) 使用更少但间隔良好的水平规则来实现这一点。该booktabs
包及其宏\toprule
、\midrule
和\bottomrule
就是为这种用例而制作的。
\documentclass{article}
\usepackage{tabularx,ragged2e,siunitx,booktabs}
\newcolumntype{C}{>{\Centering\arraybackslash\hspace{0pt}}X}
\begin{document}
\begin{table}
\caption{Second try}
\label{tab:secondtry}
\setlength\tabcolsep{4pt} % default: 6pt
\begin{tabularx}{\textwidth}{@{} l *{6}{C} @{}}
\toprule
Type & Product name & Diameter & Tensile strength
& Young's modulus & Elongation & Application \\
& & (\si{\milli\meter}) & (\si{\mega\pascal}) & (\si{\giga\pascal}) & (\%) & \\
\midrule
Class A & abc123 & 0.014 & 1500 & 36 & 7 & four words text here \\ \addlinespace
& abc123 & 0.040 & 1400 & 36 & 6.5 & four words text here \\ \addlinespace
& abc123 & 0.20 & 975 & 27 & 9 & four words text here \\ \addlinespace
& abc123 & 0.31 & 975 & 26 & 6 & four words text here \\ \addlinespace
& abc123 & 0.67 & 900 & 23 & 9 & four words text here \\
\midrule
Class B & abc123 & 0.027 & 1560 & 39 & 6.5 & four words text here \\ \addlinespace
& abc123 & 0.040 & 1600 & 41 & 6.5 & four words text here \\ \addlinespace
& abc123 & 0.10 & 1200 & 28 & 12.5& four words text here \\
\bottomrule
\end{tabularx}
\end{table}
\end{document}