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

8
  • Why all those \multicolumns?
    – Bernard
    Feb 11, 2020 at 20:07
  • What should happen to "work and steal"? This is wider than 2cm.
    – leandriis
    Feb 11, 2020 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, 2020 at 20:09
  • 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, 2020 at 20:11
  • 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} Feb 11, 2020 at 20:18

2 Answers 2

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

4
  • +1: You seem to systematically address old questions using the nice new package tabularray :) - are you the package author? Aug 22, 2021 at 1:20
  • 1
    @Dr.ManuelKuehner Yes, I am the author.
    – L.J.R.
    Aug 22, 2021 at 1:21
  • Nice - good work! I gave you some upvotes as a "thank you". Aug 22, 2021 at 1:23
  • 1
    Thank you for your appreciation of my package.
    – L.J.R.
    Aug 22, 2021 at 1:30
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

You must log in to answer this question.

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