1

I am struggling to get the distribution for table with multiple columns per rows right. My LaTeX code is:

\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}

This coding results in this:

enter image description here

But I actually want this:

enter image description here

0

2 Answers 2

2

Here's a solution that uses a tabularx environment to (a) assure that the table fits inside the width of the text block, (b) assure that the 7 cells in the bottom row have equal widths, and (c) allow automatic line wrapping within the cells.

As in David Carlisle's answer, the tabularx env. is set up to have not 7 but 14 underlying columns. Each visible cell in the bottom row actually spans 2 underlying columns. The contents of the visible cells must be entered in \mc (or \md) wrappers, to indicate how many underlying columns they should span.

enter image description here

\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
  • 1
    Thank you so much. Live-saving :) I had a compiling error first (assuming the documentclass was struggling without table). So I put in \begin{table} and \end{table}
    – Abtc
    Oct 13, 2020 at 8:10
  • @Abtc - You're welcome. For sure, if you encase the tabularx environment in a table environment, you can omit the \noindent directive that I employed in my answer.
    – Mico
    Oct 13, 2020 at 10:00
1

In it's lowest level \halign table column assignment Tex does a "simplification" that if there is a cell boundary that is spanned on every row it is essentially removed from the internal structure.

So here you want to ensure that you really have 14 effective columns so you can have a final last row with every cell present and no spans, this would normally force extra visible vertical space and vertical rules so i used \omit primitive to prevent the addition of the rules and the \strut that latex normally adds to a row.

enter image description here

\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}

I left the table content as unrelated to the question but numbers should be in math mode, $-1$ is not -1, I would also use \setlength\extrarowheight{2pt} to keep the \hline away from the cell content.

1
  • +1 for the use of \omit. :-)
    – Mico
    Oct 13, 2020 at 6:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .