TransWikia.com

Table equal column width when only multicolumn cells

TeX - LaTeX Asked by Kaskorian on July 24, 2021

I have to create a table containing some package header information. I created a HTML reference layout, I now want to translate it to LaTeX.

HTML:
enter image description here

LaTeX:

documentclass[11pt,a4paper,titlepage,openbib]{article}

usepackage[utf8]{inputenc}
usepackage[german]{babel}
usepackage[T1]{}
usepackage[left=2cm,right=2cm,top=2cm,bottom=2cm]{geometry}
usepackage[hidelinks]{hyperref}
usepackage{enumitem}
usepackage{array}
usepackage{tabularx}

newcolumntype{C}{>{centeringarraybackslash}X}
setlengthtabcolsep{.5pt}

begin{document}
begin{tabularx}{textwidth}{|*{16}{C|}}
hline
  multicolumn{4}{|r|}{4} 
& multicolumn{4}{r|}{8} 
& multicolumn{4}{r|}{12}
& multicolumn{4}{r|}{16}
hline
  multicolumn{1}{|c|}{B/R}
& multicolumn{5}{c|}{Msg-Type}
& multicolumn{5}{c|}{Src-Addr}
& multicolumn{5}{c|}{Dest-Addr}
hline
  multicolumn{16}{|c|}{Fletcher-16 Checksum}
hline
  multicolumn{8}{|c|}{Payload-Length in Bits}
& multicolumn{8}{c|}{Payload max. 28 Byte}
hline
end{tabularx}
end{document}

Result:
enter image description here

What am I missing?

One Answer

LaTeX needs a bit more information in order to determine the cell widths correctly. In the following, this is achieved by providing explicit calculations for the usable widths of 7 of 8 cells in rows 1 and 2. The eighth cell -- the one that contains "B/R" -- is given its natural cell attribute, not multicolumn{1}{|c|}{B/R}.

The calculations are based on the fact that if the usable width of a cell defined by

newcolumntype{C}{>{Centering}X|}

is, say, Z, then the usable width of a combination of 4 of these cells is given by 4Z + (4-1)*2*tabcolsep + (4-1)*arrayrulewidth.

enter image description here

documentclass[11pt,a4paper]{article}
usepackage[ngerman]{babel}
usepackage[utf8]{inputenc}
usepackage[T1]{fontenc}
usepackage[margin=2cm]{geometry}

usepackage{tabularx,ragged2e}
newcolumntype{C}{>{Centering}X}
newcolumntype{R}{>{RaggedLeft}X}

setlengthtabcolsep{2pt} % '0.5pt' seems rather extreme

begin{document}

noindent % <-- important
begin{tabularx}{textwidth}{|*{16}{C|}}
hline
multicolumn{4}{|>{hsize=dimexpr4hsize+6tabcolsep+3arrayrulewidthrelax}R|}{4}  & 
multicolumn{4}{ >{hsize=dimexpr4hsize+6tabcolsep+3arrayrulewidthrelax}R|}{8}  &
multicolumn{4}{ >{hsize=dimexpr4hsize+6tabcolsep+3arrayrulewidthrelax}R|}{12} &
multicolumn{4}{ >{hsize=dimexpr4hsize+6tabcolsep+3arrayrulewidthrelax}R|}{16} 
hline
B/R & 
multicolumn{5}{ >{hsize=dimexpr5hsize+8tabcolsep+4arrayrulewidthrelax}C|}{Msg-Type}  & multicolumn{5}{ >{hsize=dimexpr5hsize+8tabcolsep+4arrayrulewidthrelax}C|}{Src-Addr}  &
multicolumn{5}{ >{hsize=dimexpr5hsize+8tabcolsep+4arrayrulewidthrelax}C|}{Dest-Addr} 
hline
multicolumn{16}{|c|}{Fletcher-16 Checksum} 
hline
multicolumn{8}{|c|}{Payload-Length in Bits} & 
multicolumn{8}{ c|}{Payload max. 28 Byte} 
hline
end{tabularx}

end{document}

Answered by Mico on July 24, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP