0

TL;DR My code to automatically number a table with two running counters in decimal and zero-padded hexadecimal (using the fmtcount and array packages) is not working.


I'm trying to automatically number a table with multiple running counters in decimal and hexadecimal, where the hexadecimal numbers are zero-padded so that they have two digits. It should look something like this:

Counter A, Hex Counter A, Dec Counter B, Hex Counter B, Dec Description
00 0 00 0 First row of the table
00 0 01 1 Second row of the table
00 0 02 2 Third row of the table
00 0 03 3 Fourth row of the table
00 0 04 4 Fifth row of the table
00 0 05 5 Sixth row of the table
00 0 06 6 Seventh row of the table
00 0 07 7 Eighth row of the table
00 0 08 8 Ninth row of the table
00 0 09 9 Tenth row of the table
00 0 0A 10 Eleventh row of the table
00 0 0B 11 Twelfth row of the table
00 0 0C 12 Thirteenth row of the table
00 0 0D 13 Fourteenth row of the table
00 0 0E 14 Fifteenth row of the table
00 0 0F 15 Sixteenth row of the table
00 0 10 16 Seventeenth row of the table
00 0 11 17 Eighteenth row of the table
01 1 00 0 Nineteenth row of the table
01 1 01 1 Twentieth row of the table
01 1 02 2 Twenty-first row of the table

In order to accomplish this, I've defined several macros (the third counter, autoCounterC, is in case I need a third running counter in a different table) and written my document like this:

So, my document looks like this:

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

The document compiles with xelatex without any errors, but it appears that the counters are not working at all. Furthermore, there is an extra row in the table that contains only the 0 character in the first column (there are no divider lines between the remaining columns):
The counters in the output do not appear at all, and there is an extra row in the table that contains only the 0 character in the first column (there are no divider lines between the remaining columns).

How can I correct my existing code to produce the table shown in the correct sample output above, and what is wrong with my current version?

1 Answer 1

0

Your problem is connected to commands in your approach needing to be defined with global scope.

In this proof of concept I simply moved \enableAutoCounterCommands to before the longtable so I did not have to learn the new LaTeX syntax and where to put the \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}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .