# Table with fixed width columns with \multicolumn in every line

I want my two tables to look like in the example below, except I would like to get rid of the first row(s) (1 to 8). Both tables should have the same width and all fields should span the given number of columns, e.g. Version should be 1/8th of the total table. Without the first row, the width of all fields seems to be random, e.g. Options to Paddings is roughly 2:6 and not 6:2.

\documentclass{article}

\usepackage{multirow}

\begin{document}
\begin{figure}
\centering
\begin{tabular}{|p{0.0925\textwidth}|p{0.0925\textwidth}|p{0.0925\textwidth}|p{0.0925\textwidth}|p{0.0925\textwidth}|p{0.0925\textwidth}|p{0.0925\textwidth}|p{0.0925\textwidth}|}
\hline
\centering{1} & \centering{2} & \centering{3} & \centering{4} & \centering{5} & \centering{6} & \centering{7} & \centering{8}\tabularnewline
\hline
Version & \multicolumn{2}{c|}{Traffic Class} & \multicolumn{5}{c|}{Flow Label}\tabularnewline
\hline
\hline
\multicolumn{8}{|c|}{}\tabularnewline
\multicolumn{8}{|c|}{}\tabularnewline
\multicolumn{8}{|c|}{}\tabularnewline
\hline
\multicolumn{8}{|c|}{}\tabularnewline
\multicolumn{8}{|c|}{}\tabularnewline
\multicolumn{8}{|c|}{}\tabularnewline
\hline
\end{tabular}
\end{figure}

\begin{figure}
\centering{}
\begin{tabular}{|p{0.0925\textwidth}|p{0.0925\textwidth}|p{0.0925\textwidth}|p{0.0925\textwidth}|p{0.0925\textwidth}|p{0.0925\textwidth}|p{0.0925\textwidth}|p{0.0925\textwidth}|}
\hline
\centering{1} & \centering{2} & \centering{3} & \centering{4} & \centering{5} & \centering{6} & \centering{7} & \centering{8}\tabularnewline
\hline
Version & \centering{}IHL & \multicolumn{2}{c|}{Type of Service} & \multicolumn{4}{c|}{Total Length}\tabularnewline
\hline
\multicolumn{4}{|c|}{Identification} & Flags & \multicolumn{3}{c|}{Fragment Offset}\tabularnewline
\hline
\multicolumn{2}{|c|}{Time to Live} & \multicolumn{2}{c|}{Protocol} & \multicolumn{4}{c|}{Header Checksum}\tabularnewline
\hline
\hline
\hline
\hline
\end{tabular}
\end{figure}
\end{document}


Update:

I switched to tabularx and resizebox so it's easier to control the size of the table. Issue remains though, as soon as I remove the first row(s), the fields don't have the correct width anymore.

\documentclass[draft]{article}

\usepackage{graphicx}
\usepackage{multirow}
\usepackage{tabularx}

\begin{document}
\begin{figure}
\centering{}
\resizebox{0.8\textwidth}{!}{%
\begin{tabularx}{\textwidth}{|*{8}{X|}}
\hline
\centering{1} & \centering{2} & \centering{3} & \centering{4} & \centering{5} & \centering{6} & \centering{7} & \centering{8}\tabularnewline
\hline
\centering{Version} & \centering{IHL} & \multicolumn{2}{c|}{Type of Service} & \multicolumn{4}{c|}{Total Length}\tabularnewline
\hline
\multicolumn{4}{|c|}{Identification} & Flags & \multicolumn{3}{c|}{Fragment Offset}\tabularnewline
\hline
\multicolumn{2}{|c|}{Time to Live} & \multicolumn{2}{c|}{Protocol} & \multicolumn{4}{c|}{Header Checksum}\tabularnewline
\hline
\hline
\hline
\hline
\end{tabularx}}
\end{figure}

\begin{figure}
\centering{}
\resizebox{0.8\textwidth}{!}{%
\begin{tabularx}{\textwidth}{|*{8}{X|}}
\hline
\centering{1} & \centering{2} & \centering{3} & \centering{4} & \centering{5} & \centering{6} & \centering{7} & \centering{8}\tabularnewline
\hline
\centering{Version} & \multicolumn{2}{c|}{Traffic Class} & \multicolumn{5}{c|}{Flow Label}\tabularnewline
\hline
\hline
\multicolumn{8}{|c|}{}\tabularnewline
\multicolumn{8}{|c|}{}\tabularnewline
\multicolumn{8}{|c|}{}\tabularnewline
\hline
\multicolumn{8}{|c|}{}\tabularnewline
\multicolumn{8}{|c|}{}\tabularnewline
\multicolumn{8}{|c|}{}\tabularnewline
\hline
\end{tabularx}}
\end{figure}
\end{document}

• You like to have this table without the first row? – Zarko Jul 5 '16 at 7:40
• Yes, both tables without the first row (1 to 8), but both tables should still have the same width. – Martin Jul 5 '16 at 7:43
• The second table is too wide anyway! – user31729 Jul 5 '16 at 7:47
• than just erase both rows with lengths of field (numbers), and instead tabular use tabularx: \begin{tabularx}{\textwidth}{|*{8}{X|}}. Of course, in preamble you should add \usepackage{tabularx} ... – Zarko Jul 5 '16 at 7:53
• @ChristianHupfer: Both are, actually. But that's a problem of the simplified example I have given and no issue in my actual document (different font size, margins etc.) – Martin Jul 5 '16 at 7:59

If you look, for example, at your first table, you can notice that the cells in the second column are always under the scope of \multicolumn. Thus TeX has no way of using the column specification.

You probably want to use the bytefield package; the translation is rather easy.

In the first diagram I used a \bitheader line, comment it out if you don't want it.

\documentclass{article}

\usepackage{bytefield}

\begin{document}

\begin{figure}[htp]
\centering

\begin{bytefield}[bitwidth=0.125\textwidth]{8}
\bitbox{1}{Version} & \bitbox{2}{Traffic Class} & \bitbox{5}{Flow Label} \\
\end{bytefield}

\end{figure}

\begin{figure}[htp]
\centering

\begin{bytefield}[bitwidth=0.125\textwidth]{8}
\bitbox{1}{Version} & \bitbox{1}{IHL} & \bitbox{2}{Type of Service} & \bitbox{4}{Total Length} \\
\bitbox{4}{Identification} & \bitbox{1}{Flags} & \bitbox{3}{Fragment Offset} \\
\bitbox{2}{Time to Live} & \bitbox{2}{Protocol} & \bitbox{4}{Header Checksum} \\
\end{bytefield}


• This is great! Only issue: the baseline isn't aligned, but that can be fixed though: tex.stackexchange.com/questions/302388/… I'm not sure if I should accept this answer, because it doesn't answer why the width of fields gets randomized when using tabluar(x). – Martin Jul 5 '16 at 11:40
• @mgansler When you use \multicolumn in every cell of a column, that column is set to the minumum necessary width. – egreg Jul 5 '16 at 12:26