1

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}
Martin
  • 113
  • 1
    You like to have this table without the first row? – Zarko Jul 05 '16 at 07:40
  • Yes, both tables without the first row (1 to 8), but both tables should still have the same width. – Martin Jul 05 '16 at 07:43
  • The second table is too wide anyway! –  Jul 05 '16 at 07: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 05 '16 at 07: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 05 '16 at 07:59
  • @Zarko: I tried that now and in the first table (ipv6) the version field is approximately 5 columns wide. And in the second table (ipv4), options is narrower than version/padding. Looks okay if I keep the first row though so I will switch to tabularx anyway, thanks so far. – Martin Jul 05 '16 at 08:04
  • Than be so kind an give us all relevant information! Don't leave us to rove in darkens. How can we help you? Similar figures people usually draw with some drawing package as are pstrick or tikz. – Zarko Jul 05 '16 at 09:26

1 Answers1

1

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

egreg
  • 1,121,712
  • This is great! Only issue: the baseline isn't aligned, but that can be fixed though: https://tex.stackexchange.com/questions/302388/bytefield-colors-and-baseline-align 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 05 '16 at 11:40
  • Also: why is it \wordbox{3} and not \wordbox{4} for ipv6 source/destination? – Martin Jul 05 '16 at 11:44
  • @mgansler The argument specifies the number of lines the field should span. – egreg Jul 05 '16 at 12:25
  • 1
    @mgansler When you use \multicolumn in every cell of a column, that column is set to the minumum necessary width. – egreg Jul 05 '16 at 12:26
  • apparently I could see straight anymore. 4 is the correct value, not 3. Thanks again! – Martin Jul 06 '16 at 06:31