[Tex/LaTex] Table with fixed width columns with \multicolumn in every line

multicolumntableswidth

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
\multicolumn{4}{|c|}{Payload Length} & \multicolumn{2}{c|}{Next Header} & \multicolumn{2}{c|}{Hop Limit} \tabularnewline
\hline
\multicolumn{8}{|c|}{\multirow{4}{*}{Source Address}}\tabularnewline
\multicolumn{8}{|c|}{}\tabularnewline
\multicolumn{8}{|c|}{}\tabularnewline
\multicolumn{8}{|c|}{}\tabularnewline
\hline
\multicolumn{8}{|c|}{\multirow{4}{*}{Destination Address}}\tabularnewline
\multicolumn{8}{|c|}{}\tabularnewline
\multicolumn{8}{|c|}{}\tabularnewline
\multicolumn{8}{|c|}{}\tabularnewline
\hline
\end{tabular}
\caption{IPv6: Header Format}
\label{fig:ipv6_header}
\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
\multicolumn{8}{|c|}{Source Address}\tabularnewline
\hline
\multicolumn{8}{|c|}{Destination Address}\tabularnewline
\hline
\multicolumn{6}{|c|}{Options} & \multicolumn{2}{c|}{Padding}\tabularnewline
\hline
\end{tabular}
\caption{IPv4: Header Format}
\label{fig:ipv4_header}
\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
    \multicolumn{8}{|c|}{Source Address}\tabularnewline
    \hline
    \multicolumn{8}{|c|}{Destination Address}\tabularnewline
    \hline
    \multicolumn{6}{|c|}{Options} & \multicolumn{2}{c|}{Padding}\tabularnewline
    \hline
\end{tabularx}}
\caption{IPv4: Header Format}
\label{fig:ipv4_header}
\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
    \multicolumn{4}{|c|}{Payload Length} & \multicolumn{2}{c|}{Next Header} & \multicolumn{2}{c|}{Hop Limit} \tabularnewline
    \hline
    \multicolumn{8}{|c|}{\multirow{4}{*}{Source Address}}\tabularnewline
    \multicolumn{8}{|c|}{}\tabularnewline
    \multicolumn{8}{|c|}{}\tabularnewline
    \multicolumn{8}{|c|}{}\tabularnewline
    \hline
    \multicolumn{8}{|c|}{\multirow{4}{*}{Destination Address}}\tabularnewline
    \multicolumn{8}{|c|}{}\tabularnewline
    \multicolumn{8}{|c|}{}\tabularnewline
    \multicolumn{8}{|c|}{}\tabularnewline
    \hline
\end{tabularx}}
\caption{IPv6: Header Format}
\label{fig:ipv6_header}
\end{figure}
\end{document}

Best Answer

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}
\bitheader{0-7} \\ 
\bitbox{1}{Version} & \bitbox{2}{Traffic Class} & \bitbox{5}{Flow Label} \\
\bitbox{4}{Payload Length} & \bitbox{2}{Next Header} & \bitbox{2}{Hop Limit} \\
\wordbox{3}{Source Address} \\
\wordbox{3}{Destination Address}
\end{bytefield}

\caption{IPv6: Header Format}
\label{fig:ipv6_header}
\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} \\
\wordbox{1}{Source Address} \\
\wordbox{1}{Destination Address} \\
\bitbox{6}{Options} & \bitbox{2}{Padding} \\
\end{bytefield}

\caption{IPv4: Header Format}
\label{fig:ipv4_header}
\end{figure}

\end{document}

enter image description here

Related Question