tabularx 表格左侧的水平线被移动到右侧

tabularx 表格左侧的水平线被移动到右侧

这个问题很难解释,所以我将粘贴屏幕截图:

在此处输入图片描述

您可以清楚地看到此表存在一些问题:分隔行的水平线不知何故超出了表的范围。此外,位于第二行 (SourceNodeId) 的字符串未居中,而是稍微向左移动。

\begin{table}[h!]
    \centering
    \newcommand{\tabularxmulticolumnleft}[3] 
        {\multicolumn{#1}
                     {>{\raggedright\hsize=\dimexpr#1\hsize+#1\tabcolsep+\arrayrulewidth\relax}#2}
                     {#3}}
    \newcommand{\tabularxmulticolumncentered}[3] 
        {\multicolumn{#1}
                     {>{\centering\hsize=\dimexpr#1\hsize+#1\tabcolsep+\arrayrulewidth\relax}#2}
                     {#3}}
    \begin{tabularx}{\textwidth}{ *{16}{X}  }
        \hline
        \tabularxmulticolumnleft{4}{|X}{0} & \tabularxmulticolumnleft{4}{|X}{3} & \tabularxmulticolumnleft{4}{|X}{7} &\tabularxmulticolumnleft{4}{|X|}{11} \\
        \hline
        \tabularxmulticolumncentered{16}{|X|}{SourceNodeId} \\
        \hline
        \tabularxmulticolumncentered{4}{|X}{MessageType} & \tabularxmulticolumncentered{4}{|X}{RetransmissionId} & \tabularxmulticolumncentered{1}{|X}{ExpectAck} &
        \tabularxmulticolumncentered{7}{|X|}{IV}\\
        \hline
        \tabularxmulticolumncentered{9}{|X}{IV} & \tabularxmulticolumncentered{4}{|X}{PayloadSize} & \tabularxmulticolumncentered{3}{|X|}{Payload}\\
        \hline
        \tabularxmulticolumncentered{16}{|X|}{Payload}\\
        \hline
    \end{tabularx}
    \label{table:dataframe_header}
\end{table}

你知道这里可能存在什么问题吗?

PS:表格的结构是故意设计的。我希望列的大小固定,所有列的宽度都相等。表格表示字节数组,旨在显示每个片段中的信息。以下是此类表格的示例:关联

答案1

像这样吗?(请注意,我已经替换ExpectAck为,I因为ExpectAck根本无法放入狭窄的列中。)

在此处输入图片描述

\documentclass{article}
\usepackage{tabularx}
\newcolumntype{L}{>{\raggedright\arraybackslash}X}
\newcolumntype{C}{>{\centering\arraybackslash}X}
% idea for following code comes from https://tex.stackexchange.com/a/566577/5001
\newcommand\newhsize[1]{\hsize=\dimexpr #1\hsize
           +#1\tabcolsep+#1\tabcolsep-2\tabcolsep
           +#1\arrayrulewidth-\arrayrulewidth \relax}
\newcommand{\mcL}[2]{ \multicolumn{#1}{ >{\newhsize{#1}}L|}{#2}}
\newcommand{\mcC}[2]{ \multicolumn{#1}{ >{\newhsize{#1}}C|}{#2}}
\newcommand{\mcLL}[2]{\multicolumn{#1}{|>{\newhsize{#1}}L|}{#2}} % "|" at LHS
\newcommand{\mcCC}[2]{\multicolumn{#1}{|>{\newhsize{#1}}C|}{#2}} % "|" at LHS
%% (LHS: left-hand side)

\begin{document}
\begin{table}[h!]
\setlength\extrarowheight{2pt} % for a slightly more open "look"
    \begin{tabularx}{\textwidth}{| *{16}{X} |}
        \hline
        \mcLL{4}{0} & \mcL{4}{3} & \mcL{4}{7} & \mcL{4}{11} \\
        \hline
        \mcCC{16}{SourceNodeId} \\
        \hline
        \mcCC{4}{MessageType} & \mcC{4}{RetransmissionId} & \mcC{1}{I} & \mcC{7}{IV}\\
        \hline
        \mcCC{9}{IV} & \mcC{4}{PayloadSize} & \mcC{3}{Payload}\\
        \hline
        \mcCC{16}{Payload}\\
        \hline
    \end{tabularx}
\end{table}
\end{document}

相关内容