具有多个多列的表格

具有多个多列的表格

我正在努力让每行多列的表格分布正确。我的 LaTeX 代码是:

\documentclass{book}
\begin{document}

\begin{table}[]
\begin{tabular}{|c|c|c|c|c|c|c|c|c|c|c|c|c|c|}
\hline
\multicolumn{7}{|c|}{0} & \multicolumn{7}{c|}{1}\\ \hline
\multicolumn{7}{|c|}{Negative} & \multicolumn{7}{c|}{Positive}\\ \hline
\multicolumn{4}{|c|}{-1} & \multicolumn{6}{c|}{0} & \multicolumn{4}{c|}{1} \\ \hline
\multicolumn{4}{|c|}{Negative} & \multicolumn{6}{c|}{Neutral} & \multicolumn{4}{c|}{Positive} \\ \hline
\multicolumn{2}{|c|}{1} & \multicolumn{3}{c|}{2} & \multicolumn{4}{c|}{3} & \multicolumn{3}{c|}{4} & \multicolumn{2}{c|}{5} \\ \hline
\multicolumn{2}{|c|}{\begin{tabular}[c]{@{}c@{}}Strongly\\ Negative\end{tabular}} & \multicolumn{3}{c|}{\begin{tabular}[c]{@{}c@{}}Weakly\\ Negative\end{tabular}} & \multicolumn{4}{c|}{Neutral} & \multicolumn{3}{c|}{\begin{tabular}[c]{@{}c@{}}Weakly\\ Positive\end{tabular}} & \multicolumn{2}{c|}{\begin{tabular}[c]{@{}c@{}}Strongly\\ Positive\end{tabular}} \\ \hline
\begin{tabular}[c]{@{}c@{}}Very\\ Negative\end{tabular} & \multicolumn{2}{c|}{Negative} & \multicolumn{3}{c|}{\begin{tabular}[c]{@{}c@{}}Mostly\\ Negative\end{tabular}} & \multicolumn{2}{c|}{Neutral} & \multicolumn{3}{c|}{\begin{tabular}[c]{@{}c@{}}Mostly \\ Positive\end{tabular}} & \multicolumn{2}{c|}{Positive} & \begin{tabular}[c]{@{}c@{}}Strongly \\ Positive\end{tabular} \\ \hline

\end{tabular}
\end{table}

\end{document}

这种编码结果如下:

在此处输入图片描述

但我实际上想要的是这个:

在此处输入图片描述

答案1

这里有一个解决方案,它使用tabularx环境来(a)确保表格适合文本块的宽度,(b)确保底行中的 7 个单元格具有相同的宽度,以及(c)允许在单元格内自动换行。

就像大卫·卡莱尔的回答tabularx环境设置为具有 14 个底层列,而不是 7 个。底行中的每个可见单元格实际上跨越 2 个底层列。可见单元格的内容必须输入到\mc(或\md) 包装器中,以指示它们应跨越多少个底层列。

在此处输入图片描述

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{tabularx}
\renewcommand\tabularxcolumn[1]{m{#1}}
\newcolumntype{C}{>{\centering\arraybackslash}X}
\newcommand\newhsize[1]{\hsize= \dimexpr #1\hsize
                    +#1\tabcolsep+#1\tabcolsep-2\tabcolsep
                    +#1\arrayrulewidth-\arrayrulewidth  \relax}
\newcommand{\mc}[2]{\multicolumn{#1}{ >{\newhsize{#1}}C|}{#2}}
\newcommand{\md}[2]{\multicolumn{#1}{|>{\newhsize{#1}}C|}{#2}} % for first col. in row

\begin{document}   
\setlength\tabcolsep{3pt}      % default: 6pt
\setlength\extrarowheight{2pt} % for a more open "look"

\noindent
\begin{tabularx}{\textwidth}{|*{14}{C|}}
\hline
\md{7}{0} & \mc{7}{1} \\ \hline

\md{7}{Negative} & \mc{7}{Positive}\\ \hline

\md{5}{$-1$} & \mc{4}{0} & \mc{5}{1}\\ \hline

\md{5}{Negative} & \mc{4}{Neutral} & \mc{5}{Positive} \\ \hline

\md{3}{1} & \mc{2}{2} & \mc{4}{3} & \mc{2}{4} & \mc{3}{5}\\ \hline

\md{3}{Strongly Negative} & \mc{2}{Weakly Negative} & \mc{4}{Neutral} & 
\mc{2}{Weakly Positive} & \mc{3}{Strongly Positive}\\ \hline

\md{2}{Very Positive} & \mc{2}{Positive} & \mc{2}{Mostly Positive} & \mc{2}{Neutral} & 
\mc{2}{Mostly Negative} &  \mc{2}{Negative} & \mc{2}{Strongly Negative} \\ \hline
\end{tabularx}
\end{document}

答案2

在其最低级别的\halign表列分配中,Tex 进行了“简化”,如果每一行都跨越一个单元格边界,则它基本上会从内部结构中删除。

所以在这里你要确保你确实有 14 个有效列,这样你就可以有一个最后行,其中每个单元格都存在并且没有跨度,这通常会强制额外的可见垂直空间和垂直规则,所以我使用\omit原始来防止添加规则和\strut乳胶通常添加到一行中。

在此处输入图片描述

\documentclass{article}

\addtolength\textwidth{2pt}
\begin{document}

\begin{tabular}{|c|c|c|c|c|c|c|c|c|c|c|c|c|c|}
\hline
\multicolumn{7}{|c|}{0} & \multicolumn{7}{c|}{1} \\ \hline
\multicolumn{7}{|c|}{Negative} & \multicolumn{7}{c|}{Positive}\\ \hline
\multicolumn{4}{|c|}{-1} & \multicolumn{6}{c|}{0} & \multicolumn{4}{c|}{1}  \\\hline
\multicolumn{4}{|c|}{Negative}& \multicolumn{6}{c|}{Neutral}& \multicolumn{4}{c|}{Positive}\\ \hline
\multicolumn{2}{|c|}{1}& \multicolumn{3}{c|}{2}& \multicolumn{4}{c|}{3} & \multicolumn{3}{c|}{4} & \multicolumn{2}{c|}{5}\\ \hline
\multicolumn{2}{|c|}{\begin{tabular}[c]{@{}c@{}}Strongly\\ Negative\end{tabular}} & \multicolumn{3}{c|}{\begin{tabular}[c]{@{}c@{}}Weakly\\ Negative\end{tabular}}& \multicolumn{4}{c|}{Neutral}& \multicolumn{3}{c|}{\begin{tabular}[c]{@{}c@{}}Weakly\\ Positive\end{tabular}}& \multicolumn{2}{c|}{\begin{tabular}[c]{@{}c@{}}Strongly\\ positive\end{tabular}} \\ \hline
\begin{tabular}[c]{@{}c@{}}Very\\ Positive\end{tabular}& \multicolumn{2}{c|}{Positiv}& \multicolumn{3}{c|}{\begin{tabular}[c]{@{}c@{}}Mostly\\ Positive\end{tabular}} & \multicolumn{2}{c|}{Neutral} & \multicolumn{3}{c|}{\begin{tabular}[c]{@{}c@{}}Mostly \\ Negative\end{tabular}} & \multicolumn{2}{c|}{Negative}& \begin{tabular}[c]{@{}c@{}}Strongly \\ Negative\end{tabular}\\ \hline
\omit&\omit&\omit&\omit&\omit&\omit&\omit&\omit&\omit&\omit&\omit&\omit&\omit&\omit
\end{tabular}


\end{document}

我将表格内容保留为与问题无关,但数字应该处于数学模式,$-1$如果不是-1,我也会使用\setlength\extrarowheight{2pt}它来\hline远离单元格内容。

相关内容