2

I want to generate a table with fixed column width even though every line of it has \multicolumn.

I have seen this solution but it's not suitable for my needs since I need to be able to control each cell's border.

MWE:

\documentclass{article}
\usepackage{multirow}
\begin{document}
\begin{tabular}{*{2}{p{1cm}}*{2}{p{2cm}}}
&
&\multicolumn{2}{c}{strong}\\
&
&\multicolumn{1}{c}{work and steal}
&\multicolumn{1}{c}{work}\\
\cline{3-4}
\multirow{2}*{weak} 
&\multicolumn{1}{c|}{work}
&\multicolumn{1}{c|}{(x,y)} 
&\multicolumn{1}{c|}{(x,y)}\\
\cline{3-4}
&\multicolumn{1}{c|}{rest}
&\multicolumn{1}{c|}{(x,y)} 
&\multicolumn{1}{c|}{(x,y)}\\
\cline{3-4}
\end{tabular}
\end{document}

enter image description here

Morzen
  • 325
  • Why all those \multicolumns? – Bernard Feb 11 '20 at 20:07
  • What should happen to "work and steal"? This is wider than 2cm. – leandriis Feb 11 '20 at 20:08
  • Probably you are more satisfied with &work and steal &work\\ instead fo your current 2 multicolumns. If you insist on using the multicolumn commands, although I can't see any reason why, you can use &\multicolumn{1}{p{2cm}}{work and steal} &\multicolumn{1}{p{2cm}}{work}\\. – leandriis Feb 11 '20 at 20:09
  • All multicolumns to control borders. Cells can be as wide as necessary but preferably minimal. – Morzen Feb 11 '20 at 20:10
  • 1
    Not all of the multicolumn commands are needed to expicitly control the borders of the table. Especially not the ones in the second row. – leandriis Feb 11 '20 at 20:11
  • You're right and this solves some of the problem, but I also use multicolumn since I want to control the alignment (because when I use p{2cm} I can't center the text) – Morzen Feb 11 '20 at 20:14
  • 2
    you are completely removing the p specification and replacing it by c so the column width is not specified. if you need multicolumn (which seems unlikely) use p again not c. a p column entry is a parbox so you can use \centering to center, or for a fixed width c column use array package and wc{2cm} – David Carlisle Feb 11 '20 at 20:18
  • Cheers! that was the solution. – Morzen Feb 11 '20 at 20:23

2 Answers2

1

An alternative solution with tblr environment of tabularray package:

\documentclass{article}

\usepackage{tabularray}

\begin{document}

\begin{tblr}{ colspec = {{2}{t{1cm}}{2}{t{2.3cm}}}, hline{3-5} = {3-4}{solid}, vline{3-5} = {3-4}{solid}, cells = {c}, cell{1}{3} = {c=2}{}, % multicolumn cell{3}{1} = {r=2}{}, % multirow } & & strong & \ & & work and steal & work \ weak & work & (x,y) & (x,y) \ & rest & (x,y) & (x,y) \ \end{tblr}

\end{document}

enter image description here

L.J.R.
  • 10,932
0

Here is a solution with {NiceTabular} of nicematrix.

\documentclass{article}
\usepackage{nicematrix}

\begin{document} \begin{NiceTabular}{ccw{c}{23mm}w{c}{23mm}} & & \Block[C]{1-2}{strong} & \ & & work and steal & work \ \Block{2-1}{weak} & work & \Block[hvlines]{2-2}{} $(x,y)$ & $(x,y)$ \ & rest & $(x,y)$ & $(x,y)$ \ \end{NiceTabular} \end{document}

Output of the above code

F. Pantigny
  • 40,250