表格的列宽固定,每行都有 \multicolumn

表格的列宽固定,每行都有 \multicolumn

我希望我的两个表格看起来像下面的示例,但我想删除第一行(1 to 8)。两个表格的宽度应相同,所有字段应跨越给定的列数,例如Version应为整个表格的 1/8。如果没有第一行,所有字段的宽度似乎是随机的,例如Options大约Paddings是 2:6 而不是 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}

更新

我改用了tabularxresizebox这样更容易控制表格的大小。但问题仍然存在,只要我删除第一行,字段就不再具有正确的宽度。

\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}

答案1

例如,如果你看一下第一个表格,你会注意到第二列中的单元格始终在 的范围内\multicolumn。因此 TeX 无法使用列规范。

您可能想要使用该bytefield包;翻译相当容易。

在第一个图中我使用了一行\bitheader,如果您不想要它,请将其注释掉。

\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}

在此处输入图片描述

相关内容