\documentclass{article}
\usepackage{}
\usepackage{makecell, multirow, tabularx}
\begin{document}
\begin{table}[h!]
\setlength\extrarowheight{2pt} % for a ever so slightly more open "look"
\begin{tabularx}{\textwidth}{X |c |c |c |c |}
\cline{2-5}
& Mean & N & Std. Deviation & Std. Error Mean \\
\hline
Testing123 Testin123 Testing123 Testin123 & 23\% & 10\% & 279\% & 233\% \\
\hline
\end{tabularx}
\caption{Testing123 Testin123Testing123 Testin123}
\end{table}
我已经添加了多列
\begin{table}[h!]
\setlength\extrarowheight{2pt} % for a ever so slightly more open "look"
\begin{tabularx}{\textwidth}{X |c |c |c |c |}
\cline{2-5}
& Mean & N & Std. Deviation & Std. Error Mean \\
\hline
\multicolumn{1}{|c|}{Testing123 Testin123 Testing123 Testin123} & 23\% & 10\% & 279\% & 233\% \\
\hline
\end{tabularx}
\caption{Testing123 Testin123Testing123 Testin123}
\end{table}
结果:
我想将“Testing123 Testin123 Testing123 Testin123”分成两行。
答案1
如果您查看@Jennifer的代码,您会发现第一个标题单元格(平均值)中的左侧垂直线与下一行中的垂直线不对齐。这是因为垂直条(|
)放置不正确。您需要在第一行和第二行中将列|
右侧的设置为X
\multicolumn
消除来自第二个标题单元格的命令\multicolumn
。第一行应为:
\multicolumn{1}{X|}{} & Mean & N & Std. Deviation & Std. Error Mean \\
以下是完整的 MWE:
\documentclass{article}
\usepackage{tabularx, caption} % Load package caption for better captions
\usepackage{booktabs, ragged2e} % Use ragged2e to improve ragged text
\setlength\extrarowheight{1pt} % Both line necessary for decent spacing
\renewcommand{\arraystretch}{1.2}
\begin{document}
\begin{table}[!ht]
\caption{Testing123 Testin123Testing123 Testin123% % Caption above table
\label{tab:test123}% % Label inside caption
}
\begin{tabularx}{\textwidth}{|>{\RaggedRight\arraybackslash}X|c |c |c |c |}
\cline{2-5}
\multicolumn{1}{X|}{} % Right vertical rules in first header cell
& Mean % Remove `multicolumn` from second header cell
& N & Std. Deviation
& Std. Error Mean \\
\hline
Testing123 Testin123 Testing123 Testin123 & 23\% & 10\% & 279\% & 233\% \\
\hline
\end{tabularx}
\end{table}
不过,我建议您以不同的方式排版表格:
- 使用书签制定更好的规则
- 删除所有垂直线(参见booktabs 手册)
- 用于
@{}
消除左右侧轴承 - 通过定义新的列类型来设置四列等宽的数字
- 将图形列右对齐
- 将百分号移至标题
表格序言如下:
\begin{tabularx}{\textwidth}{@{}M*{4}{N}@{}}
意思是:
@{}
= 删除第一列左侧的侧边栏,即第一列开头的小空间M
= 设置一列类型(我们已经使用文档前言中的M
命令定义了它)。\newcolumntype
*{4}{N}
= 表示创建四列类型N
(我们已经使用文档序言中的命令进行了定义\newcolumntype
)。这与在此 MWE 中写入 four 相同NNNN
,但通常这是一种快捷方式。@{}
= 与上文最后一列第 1 项相同:将侧轴承移至正确的第五列,IE您在最后一列末尾看到的小空间。
希望现在能够更容易理解。
\documentclass{article}
\usepackage{tabularx, caption} % Load package caption for decent foramtting and spacing
\usepackage{booktabs, ragged2e} % Use booktabs rules and get rid of vertical rules,
% ragged2e to improved ragged right and left text
\setlength\extrarowheight{1pt} % Both line necessary for decent spacing, unless you are using booktabs rules.
\renewcommand{\arraystretch}{1.2}
\newcolumntype{Y}{>{\RaggedRight\arraybackslash\hsize=2\hsize\linewidth=\hsize}X}
\newcolumntype{W}{>{\RaggedLeft\arraybackslash\hsize=0.75\hsize\linewidth=\hsize}X}
\begin{document}
\begin{table}[!ht]
\caption{Testing123 Testin123Testing123 Testin123%
\label{tab:test123}%
}
\begin{tabularx}{\textwidth}{@{}Y*{4}{W}@{}}
\toprule
& \small Mean (\%)
& \small N (\%)& \small Std. Deviation~(\%)
& \small Std. Error Mean (\%)\\
\midrule
Testing123 Testin123 Testing123 Testin123 & 23 & 10 & 279 & 233 \\
\bottomrule
\end{tabularx}
\end{table}
\end{document}
答案2
我在标题行上附加了两次多列。我希望这是你正在寻找的东西
\documentclass{article}
\usepackage{}
\usepackage{makecell, multirow, tabularx}
\begin{document}
\begin{table}[h!]
\setlength\extrarowheight{2pt} % for a ever so slightly more open "look"
\begin{tabularx}{\textwidth}{|X|c |c |c |c |}
\cline{2-5}
\multicolumn{1}{l}{}
& \multicolumn{1}{|l|}{Mean} & N & Std. Deviation & Std. Error Mean \\
\hline
Testing123 Testin123 Testing123 Testin123 & 23\% & 10\% & 279\% & 233\% \\
\hline
\end{tabularx}
\caption{Testing123 Testin123Testing123 Testin123}
\end{table}
\end{document}