使用多个计数器以十进制和十六进制显示表格行数

使用多个计数器以十进制和十六进制显示表格行数

总结我的代码用于自动对带有两个十进制和零填充十六进制运行计数器的表格进行编号(使用fmtcountarray包),但不起作用。


我正在尝试自动对包含多个十进制和十六进制运行计数器的表格进行编号,其中十六进制数字用零填充,以便它们有两位数。它看起来应该像这样:

计数器 A,十六进制 柜台A, 十二月 计数器 B,十六进制 柜台B, 十二月 描述
00 0 00 0 表格第一行
00 0 01 1 表格第二行
00 0 02 2 表格第三行
00 0 03 3 表格第四行
00 0 04 4 表格第五行
00 0 05 5 表格第六行
00 0 06 6 表格第七行
00 0 07 7 表格第八行
00 0 08 8 表格第九行
00 0 09 9 表格第十行
00 0 0A 10 表格第十一行
00 0 0B 11 表格第十二行
00 0 0摄氏度 12 表格第十三行
00 0 0D 十三 表格第十四行
00 0 0E 14 表格第十五行
00 0 0F 15 表格第十六行
00 0 10 16 表格第十七行
00 0 11 17 表格第十八行
01 1 00 0 表格第十九行
01 1 01 1 表格第二十行
01 1 02 2 表格第二十一行

为了实现这一点,我定义了几个宏(第三个计数器,autoCounterC以防我在不同的表中需要第三个运行计数器)并编写了我的文档,如下所示:

因此,我的文档如下所示:

\documentclass{article}

\usepackage[landscape,letterpaper]{geometry}

\usepackage{xparse,fmtcount,array,longtable}
\usepackage[table]{xcolor}

\newcounter{autoCounterA}
\newcounter{autoCounterB}[autoCounterA]
\newcounter{autoCounterC}[autoCounterB]

\NewDocumentCommand{\autoCounterAHex}{O{1}}{}
\NewDocumentCommand{\autoCounterADec}{O{1}}{}
\newcommand{\stepAutoCounterA}{}
\NewDocumentCommand{\autoCounterBHex}{O{1}}{}
\NewDocumentCommand{\autoCounterBDec}{O{1}}{}
\newcommand{\stepAutoCounterB}{}
\NewDocumentCommand{\autoCounterCHex}{O{1}}{}
\NewDocumentCommand{\autoCounterCDec}{O{1}}{}
\newcommand{\stepAutoCounterC}{}

\providecommand{\resetAutoCounters}{\setcounter{autoCounterA}{0} \setcounter{autoCounterB}{0} \setcounter{autoCounterC}{0}}

\providecommand{\enableAutoCounterCommands}{
    % \RenewDocumentCommand{\autoCounterAHex}{O{1}}{\padzeroes[##1]{\hexadecimal{autoCounterA}}}
    \RenewDocumentCommand{\autoCounterAHex}{O{1}}{\theautoCounterA}
    \RenewDocumentCommand{\autoCounterADec}{O{1}}{\padzeroes[##1]{\decimal{autoCounterA}}}
    \renewcommand{\stepAutoCounterA}{\stepcounter{autoCounterA}}
    \RenewDocumentCommand{\autoCounterBHex}{O{1}}{\padzeroes[##1]{\hexadecimal{autoCounterB}}}
    \RenewDocumentCommand{\autoCounterBDec}{O{1}}{\padzeroes[##1]{\decimal{autoCounterB}}}
    \renewcommand{\stepAutoCounterB}{\stepcounter{autoCounterB}}
    \RenewDocumentCommand{\autoCounterCHex}{O{1}}{\padzeroes[##1]{\hexadecimal{autoCounterC}}}
    \RenewDocumentCommand{\autoCounterCDec}{O{1}}{\padzeroes[##1]{\decimal{autoCounterC}}}
    \renewcommand{\stepAutoCounterC}{\stepcounter{autoCounterC}}
}
\providecommand{\disableAutoCounterCommands}{
    \RenewDocumentCommand{\autoCounterAHex}{O{1}}{}
    \RenewDocumentCommand{\autoCounterADec}{O{1}}{}
    \renewcommand{\stepAutoCounterA}{}
    \RenewDocumentCommand{\autoCounterBHex}{O{1}}{}
    \RenewDocumentCommand{\autoCounterBDec}{O{1}}{}
    \renewcommand{\stepAutoCounterB}{}
    \RenewDocumentCommand{\autoCounterCHex}{O{1}}{}
    \RenewDocumentCommand{\autoCounterCDec}{O{1}}{}
    \renewcommand{\stepAutoCounterC}{}
}

\begin{document}
\resetAutoCounters \disableAutoCounterCommands
\rowcolors{2}{gray!15}{white}
\begin{longtable}{c<{\autoCounterAHex[2]}|c<{\autoCounterADec}|c<{\autoCounterBHex[2]}|c<{\autoCounterBDec}|p{120pt}<{\stepAutoCounterB}}
    \textbf{Counter A, Hex} & \textbf{Counter A, Dec} & \textbf{Counter B, Hex} & \textbf{Counter B, Dec} & Description \\ \hline \enableAutoCounterCommands \endhead
    &&&& First row of the table \\
    &&&& Second row of the table \\
    &&&& Third row of the table \\
    &&&& Fourth row of the table \\
    &&&& Fifth row of the table \\
    &&&& Sixth row of the table \\
    &&&& Seventh row of the table \\
    &&&& Eighth row of the table \\
    &&&& Ninth row of the table \\
    &&&& Tenth row of the table \\
    &&&& Eleventh row of the table \\
    &&&& Twelfth row of the table \\
    &&&& Thirteenth row of the table \\
    &&&& Fourteenth row of the table \\
    &&&& Fifteenth row of the table \\
    &&&& Sixteenth row of the table \\
    &&&& Seventeenth row of the table \\
    &&&& Eighteenth row of the table \\ \stepAutoCounterA
    &&&& Nineteenth row of the table \\
    &&&& Twentieth row of the table \\
    &&&& Twenty-first row of the table \\
\end{longtable}
\end{document}

文档编译时xelatex没有任何错误,但计数器似乎根本不起作用。此外,表中还有一个额外的行,仅包含0第一列中的字符(其余列之间没有分隔线):
输出中的计数器根本没有出现,并且表中有一个额外的行,其中仅包含第一列中的 0 字符(其余列之间没有分隔线)。

我该如何修正现有的代码以生成上面正确的示例输出中所示的表格,以及我当前的版本有什么问题?

答案1

您的问题与您的方法中需要用全局范围定义的命令有关。

在这个概念证明中我只是移动\enableAutoCounterCommands到了 longtable 之前所以我不需要学习新的 LaTeX 语法以及将\global.

\documentclass{article}

\usepackage[landscape,letterpaper]{geometry}

\usepackage{xparse,fmtcount,array,longtable}
\usepackage[table]{xcolor}

\newcounter{autoCounterA}
\newcounter{autoCounterB}[autoCounterA]
\newcounter{autoCounterC}[autoCounterB]

\NewDocumentCommand{\autoCounterAHex}{O{1}}{}
\NewDocumentCommand{\autoCounterADec}{O{1}}{}
\newcommand{\stepAutoCounterA}{}
\NewDocumentCommand{\autoCounterBHex}{O{1}}{}
\NewDocumentCommand{\autoCounterBDec}{O{1}}{}
\newcommand{\stepAutoCounterB}{}
\NewDocumentCommand{\autoCounterCHex}{O{1}}{}
\NewDocumentCommand{\autoCounterCDec}{O{1}}{}
\newcommand{\stepAutoCounterC}{}

\providecommand{\resetAutoCounters}{%
   \setcounter{autoCounterA}{0}%
   \setcounter{autoCounterB}{0}%
   \setcounter{autoCounterC}{0}%
}

\providecommand{\enableAutoCounterCommands}{
    \RenewDocumentCommand{\autoCounterAHex}{O{1}}{\padzeroes[##1]{\hexadecimal{autoCounterA}}}
    \RenewDocumentCommand{\autoCounterADec}{O{1}}{\padzeroes[##1]{\decimal{autoCounterA}}}
    \renewcommand{\stepAutoCounterA}{\stepcounter{autoCounterA}}
    \RenewDocumentCommand{\autoCounterBHex}{O{1}}{\padzeroes[##1]{\hexadecimal{autoCounterB}}}
    \RenewDocumentCommand{\autoCounterBDec}{O{1}}{\padzeroes[##1]{\decimal{autoCounterB}}}
    \renewcommand{\stepAutoCounterB}{\stepcounter{autoCounterB}}
    \RenewDocumentCommand{\autoCounterCHex}{O{1}}{\padzeroes[##1]{\hexadecimal{autoCounterC}}}
    \RenewDocumentCommand{\autoCounterCDec}{O{1}}{\padzeroes[##1]{\decimal{autoCounterC}}}
    \renewcommand{\stepAutoCounterC}{\stepcounter{autoCounterC}}
}
\providecommand{\disableAutoCounterCommands}{
    \RenewDocumentCommand{\autoCounterAHex}{O{1}}{}
    \RenewDocumentCommand{\autoCounterADec}{O{1}}{}
    \renewcommand{\stepAutoCounterA}{}
    \RenewDocumentCommand{\autoCounterBHex}{O{1}}{}
    \RenewDocumentCommand{\autoCounterBDec}{O{1}}{}
    \renewcommand{\stepAutoCounterB}{}
    \RenewDocumentCommand{\autoCounterCHex}{O{1}}{}
    \RenewDocumentCommand{\autoCounterCDec}{O{1}}{}
    \renewcommand{\stepAutoCounterC}{}
}

\begin{document}
\resetAutoCounters \disableAutoCounterCommands
\rowcolors{2}{gray!15}{white}
\enableAutoCounterCommands 
\begin{longtable}{c<{\autoCounterAHex[2]}|c<{\autoCounterADec}|c<{\autoCounterBHex[2]}|c<{\autoCounterBDec}|p{120pt}<{\stepAutoCounterB}}
    \multicolumn{1}{c}{\textbf{Counter A, Hex}} &
    \multicolumn{1}{c}{\textbf{Counter A, Dec}} &
    \multicolumn{1}{c}{\textbf{Counter B, Hex}} & 
    \multicolumn{1}{c}{\textbf{Counter B, Dec}} & 
    \multicolumn{1}{p{120pt}}{Description }\\ \hline
\endhead
    &&&& First row of the table \\
    &&&& Second row of the table \\
    &&&& Third row of the table \\
    &&&& Fourth row of the table \\
    &&&& Fifth row of the table \\
    &&&& Sixth row of the table \\
    &&&& Seventh row of the table \\
    &&&& Eighth row of the table \\
    &&&& Ninth row of the table \\
    &&&& Tenth row of the table \\
    &&&& Eleventh row of the table \\
    &&&& Twelfth row of the table \\
    &&&& Thirteenth row of the table \\
    &&&& Fourteenth row of the table \\
    &&&& Fifteenth row of the table \\
    &&&& Sixteenth row of the table \\
    &&&& Seventeenth row of the table \\
    &&&& Eighteenth row of the table \\ \stepAutoCounterA
    &&&& Nineteenth row of the table \\
    &&&& Twentieth row of the table \\
    &&&& Twenty-first row of the table \\
\end{longtable}
\end{document}

在此处输入图片描述

相关内容