表格单元格中的十六进制数字字符串未正确对齐

表格单元格中的十六进制数字字符串未正确对齐

下面的代码

\begin{tabular}{ |c|c|c| }
    \hline
        $C_2$ & B5 6E 2C D4 F3 12 57 3E 8C 8C 1A 77 A3 EE C3 41 \\ 
        $P'_3 \xor P_3$ & 00 00 00 00 00 0F 00 03 00 00 00 00 00 00 00 00 \\
        C'_2$ & B5 6E 2C D4 F3 1D 57 3D 8C 8C 1A 77 A3 EE C3 41\\
    \hline
\end{tabular}

产生的暴行是这样的: 第二行的 0 看起来不正确

我尝试了不同的对齐方式、字体和字体大小。请帮忙。

答案1

如果您的编辑器支持替换选择中的全部内容,那么就不会太难。

\documentclass{article}

\def\xor{\oplus}

\begin{document}

\noindent\begin{tabular}{ |c| r*{15}{@{ }r}| }
    \hline
        $C_2$ & B5 & 6E & 2C &  D4 & F3 & 12 & 57 & 3E & 8C & 8C & 1A & 77 & A3 & EE & C3 & 41 \\ 
        $P'_3 \xor P_3$ & 00 & 00 & 00 & 00 & 00 & 0F & 00 & 03 & 00 & 00 & 00 & 00 & 00 & 00 & 00 & 00 \\
        $C'_2$ & B5 & 6E & 2C & D4 & F3 & 1D & 57 & 3D & 8C & 8C & 1A & 77 & A3 & EE & C3 & 41\\
    \hline
\end{tabular}

\end{document}

演示

答案2

只是为了好玩,如何避免改变这么多的输入&

\documentclass{article}
%\usepackage{xparse} % not necessary with LaTeX 2020-10-01
\usepackage{array,booktabs}

\newcommand{\xor}{\oplus}

\ExplSyntaxOn
\NewDocumentCommand{\bytetable}{O{\textwidth}m}
 {
  \dutt_bytetable:nn { #1 } { #2 }
 }

\int_new:N \l__dutt_bytetable_bytes_int
\seq_new:N \l__dutt_bytetable_rows_seq
\seq_new:N \l__dutt_bytetable_row_seq
\seq_new:N \l__dutt_bytetable_bytes_seq
\tl_new:N \l__dutt_bytetable_temp_tl
\tl_new:N \l__dutt_bytetable_body_tl

\cs_generate_variant:Nn \seq_set_split:Nnn { Nne }

\cs_new_protected:Nn \dutt_bytetable:nn
 {
  \seq_set_split:Nnn \l__dutt_bytetable_rows_seq { \\ } { #2 }
  % examine the part after the last \\
  \seq_pop_right:NN \l__dutt_bytetable_rows_seq \l__dutt_bytetable_temp_tl
  \tl_if_empty:NF \l__dutt_bytetable_temp_tl
   {% not empty, reinsert
    \seq_put_right:NV \l__dutt_bytetable_rows_seq \l__dutt_bytetable_temp_tl
   }
  % let's build the body of the table
  \tl_clear:N \l__dutt_bytetable_body_tl
  \int_zero:N \l__dutt_bytetable_bytes_int % this counts the number of bytes
  % examine each row
  \seq_map_inline:Nn \l__dutt_bytetable_rows_seq
   {
    % separate the parts before and after &
    \seq_set_split:Nnn \l__dutt_bytetable_row_seq { & } { ##1 }
    % in the second part we need to split at spaces
    \seq_set_split:Nne \l__dutt_bytetable_bytes_seq { ~ }
     {
      \seq_item:Nn \l__dutt_bytetable_row_seq { 2 }
     }
    % count the number of parts in the bytes section
    \int_set:Nn \l__dutt_bytetable_bytes_int
     {
      \int_max:nn { \l__dutt_bytetable_bytes_int } { \seq_count:N \l__dutt_bytetable_bytes_seq }
     }
    % add the row to the table
    \tl_put_right:Nx \l__dutt_bytetable_body_tl
     {
      % first the part before &
      \seq_item:Nn \l__dutt_bytetable_row_seq { 1 } &
      % then the rest, with & in between each item
      \seq_use:Nn \l__dutt_bytetable_bytes_seq { & }
      % the endline marker
      \exp_not:N \\
     }
   }
  % output the table
  \group_begin:
  \setlength{\tabcolsep}{0pt}
  \begin{tabular*}{#1}
   {
    @{\extracolsep{\fill}}
    >{$}c<{$}
    *{\l__dutt_bytetable_bytes_int}{c}
   }
  \toprule
  \l__dutt_bytetable_body_tl
  \bottomrule
  \end{tabular*}
  \group_end:
 }

\ExplSyntaxOff

\begin{document}

\[
\bytetable{
  C_2           & B5 6E 2C D4 F3 12 57 3E 8C 8C 1A 77 A3 EE C3 41 \\ 
  P'_3 \xor P_3 & 00 00 00 00 00 0F 00 03 00 00 00 00 00 00 00 00 \\
  C'_2          & B5 6E 2C D4 F3 1D 57 3D 8C 8C 1A 77 A3 EE C3 41 \\
}
\]

\end{document}

\bytetable命令有一个可选参数,用于指定最终表格的宽度,默认值为\textwidth

如果在某些情况下字节数较少,则无需人工干预即可让表格保持其自然大小。当然,如果字节数很大,则会发生冲突,具体取决于整体文本宽度。

在此处输入图片描述

答案3

根据您的使用情况,也许您应该对十六进制数使用固定宽度的字体,例如\texttt。在表格上下文中,您可以使用标准包中的功能array为相应的列添加格式:

示例输出

\documentclass{article}

\usepackage{array}

\begin{document}

\begin{tabular}{c>{\ttfamily}c}
    \hline
        $C_2$ & B5 6E 2C D4 F3 12 57 3E 8C 8C 1A 77 A3 EE C3 41 \\
        $P'_3 \oplus P_3$ & 00 00 00 00 00 0F 00 03 00 00 00 00 00 00 00 00 \\
        $C'_2$ & B5 6E 2C D4 F3 1D 57 3D 8C 8C 1A 77 A3 EE C3 41\\
    \hline
\end{tabular}

\end{document}

[我改成\xor只是\oplus为了我自己的方便。]

相关内容