固定列宽的LaTeX表格设置最后一列居中报错如何解决
问题解决方案
错误原因
你遇到的报错是两个问题共同导致的:
- 为最后一列p类型列添加
>{\centering}时没有配套使用\arraybackslash命令,\centering会重定义\\的功能,导致LaTeX无法识别行尾的换行符,进而触发\hline位置错误、无行可结束的报错。 - 原代码中分类标题行(
\textbf{Gender}、\textbf{Educational level}、\textbf{Age})的对齐分隔符&数量过多,6列表格只需要5个&做列分隔,原代码写了6个,导致列数不匹配,触发多余制表符的报错。
修正后代码
\begin{table}[ht] \centering \setlength{\tabcolsep}{2pt} \renewcommand{\arraystretch}{1.5} % 最后一列添加\centering\arraybackslash实现居中,同时恢复行尾\\的换行功能 \begin{tabular}{|p{0.15\textwidth}>{\centering}p{0.15\textwidth}>{\centering}p{0.15\textwidth}>{\centering}p{0.15\textwidth}>{\centering}p{0.15\textwidth}>{\centering\arraybackslash}p{0.15\textwidth}|} \hline & \textbf{Virtual Reality Experience} & \textbf{First Person Experience} & \textbf{Multimedia Presentation Experience} & \multicolumn{2}{c|}{\textbf{Full sample}} \\ \hline & \textit{n} & \textit{n} & \textit{n} & \textit{n} & \textit{\%} \\ \hline \textbf{Gender} &&&&& \\ Female & 12 & 10 & 18 & 40 & 52.6 \\ Male & 10 & 13 & 13 & 36 & 47.4 \\ \hline \textbf{Educational level} &&&&& \\ High School & 8 & 5 & 6 & 19 & 25.0 \\ Bachelor's Degree & 13 & 9 & 19 & 41 & 53.9 \\ Master's Degree & 1 & 8 & 6 & 15 & 19.7 \\ PhD & 0 & 1 & 0 & 1 & 1.3 \\ \hline \hline \textbf{Age} &&&&& \\ Mean & 23.6 & 24.6 & 24 & \multicolumn{2}{c|}{24.1} \\ Median & 23.6 & 24.6 & 24 & \multicolumn{2}{c|}{24.1} \\ \hline \end{tabular} \caption{Participants demographic information} \label{tab:demographic} \end{table}
修改说明
- 列定义部分将最后一列的格式改为
>{\centering\arraybackslash}p{0.15\textwidth}|,既实现了内容居中,又恢复了行尾\\的换行功能 - 删除了三个分类标题行多余的1个
&,匹配6列的表格结构
内容的提问来源于stack exchange,提问作者alirek
相关产品推荐
相关产品推荐

