2

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}{>{\centering\arraybackslash}X}
\setlength\tabcolsep{.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?

1
  • Welcome to TeX.SE.
    – Mico
    Jun 2, 2021 at 0:46

1 Answer 1

2

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}

\setlength\tabcolsep{2pt} % '0.5pt' seems rather extreme

\begin{document}

\noindent % <-- important
\begin{tabularx}{\textwidth}{|*{16}{C|}}
\hline
\multicolumn{4}{|>{\hsize=\dimexpr4\hsize+6\tabcolsep+3\arrayrulewidth\relax}R|}{4}  & 
\multicolumn{4}{ >{\hsize=\dimexpr4\hsize+6\tabcolsep+3\arrayrulewidth\relax}R|}{8}  &
\multicolumn{4}{ >{\hsize=\dimexpr4\hsize+6\tabcolsep+3\arrayrulewidth\relax}R|}{12} &
\multicolumn{4}{ >{\hsize=\dimexpr4\hsize+6\tabcolsep+3\arrayrulewidth\relax}R|}{16} \\
\hline
B/R & 
\multicolumn{5}{ >{\hsize=\dimexpr5\hsize+8\tabcolsep+4\arrayrulewidth\relax}C|}{Msg-Type}  & \multicolumn{5}{ >{\hsize=\dimexpr5\hsize+8\tabcolsep+4\arrayrulewidth\relax}C|}{Src-Addr}  &
\multicolumn{5}{ >{\hsize=\dimexpr5\hsize+8\tabcolsep+4\arrayrulewidth\relax}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}
2
  • Okay, thanks. Would this also work when I have no 'Z' because for example I expand the first cell to span 2 columns and shrink the second? Does this \dimexpr 'contain' 1/16 * the size I specified?
    – Kaskorian
    Jun 2, 2021 at 20:58
  • @Kaskorian - I'm not sure I understand the gist of your follow-up questions. If you're asking whether the specific approach I took in the answer shown above is guaranteed to work in circumstances that could be arbitrarily different from the setup you provided in your query, the only possible answer is a resounding "no". One has to, in essence, solve a constrained optimization problem in order to obtain the values of the widths of the cells that consist of combinations of 4 or 5 "basic" cells. How this constrained optimization problem gets solved in practice depends on the constraints.
    – Mico
    Jun 2, 2021 at 21:40

You must log in to answer this question.

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