TeX - LaTeX Asked by Kyriet on August 15, 2021
I’m trying to achieve a table like this:
Here is the code:
% usepackage{multirow}
begin{table}[!h]
begin{tabular}{|r|c|c|c|c|c|c|c|c|c|c|c|c|}
hline
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
end{tabular}
end{table}
But the table that I got from the code above looks like this:
What am I missing?
You can add an empty line with all cells and a phantom letter:
documentclass{article}
usepackage{multirow}
newcommand{mc}[2]{multicolumn{#1}{c|}{#2}}
newcommand{ec}{multicolumn{1}{c}{phantom{W}}}
usepackage{lipsum}
begin{document}
lipsum[47]
begin{table}[!h]
centering
begin{tabular}{|r|*{12}{c}}
ec & ec & ec &ec & ec & ec &ec & ec & ec &ec & ec & ec & ec [-2ex]
hline
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
end{tabular}
end{table}
end{document}
Answered by Bernard on August 15, 2021
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 commandBlock
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.
documentclass{article}
usepackage{nicematrix}
begin{document}
NiceMatrixOptions{cell-space-top-limit = 3pt,cell-space-bottom-limit = 2pt}
begin{NiceTabular}{r*{12}{c}}[hvlines]
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}&&
end{NiceTabular}
end{document}
This is the first time I have tried it. I used one of the many examples included in the document as a guide.
Answered by Simon Dispa on August 15, 2021
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.
documentclass{article}
usepackage{array}
newlength{blockwidth}
begin{document}
begin{center}
settowidth{blockwidth}{hspace{tabcolsep}channelshspace{tabcolsep}}
setlength{blockwidth}{0.3333blockwidth}
setlength{tabcolsep}{0pt}
newcommand{mc}[3]{%
multicolumn{#1}{
w{c}{dimexpr#1blockwidth-#2arrayrulewidth}
|
}{#3}%
}
begin{tabular}{ | >{vphantom{$Big|$}}w{c}{3blockwidth} | *{12}{ w{c}{blockwidth}| } }
hline
channels & mc{12}{11}{Encoding format}
cline{2-13}
& mc{12}{11}{pixel}
hline
1 & mc{12}{11}{grey}
hline
2 & mc{6}{5}{grey} & mc{6}{5}{alpha}
hline
3 & mc{4}{3}{R} & mc{4}{3}{G} & mc{4}{3}{B}
hline
4 & mc{3}{2}{R} & mc{3}{2}{G} & mc{3}{2}{B} & mc{3}{2}{A}
hline
end{tabular}
end{center}
end{document}
To be honest, the table explains very little.
Answered by egreg on August 15, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP