I'm trying to achieve a table like this:

Table layout I need

Here is the code:

% \usepackage{multirow}
    \multicolumn{1}{|l|}{\multirow{2}{*}{channels}} & \multicolumn{12}{c|}{Encoding format} \\ \cline{2-13} 
    \multicolumn{1}{|l|}{} & \multicolumn{12}{c|}{pixel} \\ \hline
    1 & \multicolumn{12}{c|}{grey} \\ \hline
    2 & \multicolumn{6}{c|}{grey} & \multicolumn{6}{c|}{alpha} \\ \hline
    3 & \multicolumn{4}{c|}{R} & \multicolumn{4}{c|}{G} & \multicolumn{4}{c|}{B} \\ \hline
    4 & \multicolumn{3}{c|}{R} & \multicolumn{3}{c|}{G} & \multicolumn{3}{c|}{B} & \multicolumn{3}{c|}{A} \\ \hline

But the table that I got from the code above looks like this:

enter image description here

What am I missing?


I recently came across a new package nicematrix that seems to solve most of the common problems that a normal user encounters when trying to setup a simple table. From the manual:

In the environments of nicematrix, it’s possible to use the command \Block in order to place an element in the center of a rectangle of merged cells of the array.

The command \Block don’t create space by itself. The command \Block must be used in the upper leftmost cell of the array with two arguments.

The first argument is the size of the block with the syntax i-j where i is the number of rows of the block and j its number of columns. The second argument is the content of the block.

In NiceTabular the content of the block is composed in text mode. In the other environments, it is composed in math mode

The "[hvlines]" keyword further simplifies typing.

The resulting code is quite compact, without the need for new commands or added packages.


\NiceMatrixOptions{cell-space-top-limit = 3pt,cell-space-bottom-limit = 2pt}

\Block{2-1}{channels}&  \Block{1-12}{Encoding format} \\    
                     &  \Block{1-12}{pixel} \\
1                    &  \Block{1-12}{grey} \\   
2                    &  \Block{1-6}{grey} &&&&&&    \Block{1-6}{alpha}&&&&&\\
3                    &  \Block{1-4}{R} &&&&  \Block{1-4}{G} &&&&  \Block{1-4}{B} &&&\\
4                    &  \Block{1-3}{R}&&&\Block{1-3}{G} &&& \Block{1-3}{B} &&& \Block{1-3}{A}&&\\


This is the first time I have tried it. I used one of the many examples included in the document as a guide.



I set blocks equal to the width of “channels” with the usual padding on either side but divided by three. We need three blocks for the first column and 12 for the other columns.

Some computations are needed for taking care of the vertical rules that take some small, but not negligible, space.

The idea is not so difficult: in every row (the first column is out of these considerations), we need a total of eleven rules, but we have to divide the space evenly among the cells; so the top cells have to occupy 12 blocks and span 11 rules; the next two cells have to occupy 6 blocks and span 5 rules each and so on.





\begin{tabular}{ | >{\vphantom{$\Big|$}}w{c}{3\blockwidth} | *{12}{ w{c}{\blockwidth}| } }
channels & \mc{12}{11}{Encoding format} \\
         & \mc{12}{11}{pixel} \\
1        & \mc{12}{11}{grey} \\
2        & \mc{6}{5}{grey} & \mc{6}{5}{alpha} \\
3        & \mc{4}{3}{R} & \mc{4}{3}{G} & \mc{4}{3}{B} \\
4        & \mc{3}{2}{R} & \mc{3}{2}{G} & \mc{3}{2}{B} & \mc{3}{2}{A} \\


enter image description here

To be honest, the table explains very little.


You can add an empty line with all cells and a phantom letter:




    \ec & \ec & \ec &\ec & \ec & \ec &\ec & \ec & \ec &\ec & \ec & \ec & \ec \\[-2ex]
       \multirow{2}{*}{channels}& \mc{12}{Encoding format} \\ \cline{2-13}
        & \mc{12}{pixel} \\ \hline
        1 & \multicolumn{12}{c|}{grey} \\ \hline
        2 & \mc{6}{grey} & \mc{6}{alpha} \\ \hline
        3 & \mc{4}{R} & \mc{4}{G} & \mc{4}{B} \\ \hline
        4 & \mc{3}{R} & \mc{3}{G} & \mc{3}{B} & \mc{3}{A}\\ \hline


enter image description here

  • I'm always amazed how unnecessarily complicated are simple things like tables in LaTeX. Your solution looks more like a hack and workaround for this weird behaviour. Isn't my code an example of a bug in LaTeX library? – Kyriet Nov 27 '20 at 12:11
  • The problem is simply that you defined 12 centred columns, which by definition have to fit their contents, but these columns contain no cell. – Bernard Nov 27 '20 at 12:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.