Tabularray: correct row numbering in longtblr

tabularray

I'm trying to get numbered rows for a test plan. The expected result is "1", "2", etc. in the first column. However, when tabularray processes the table, it increments the counters multiple times before outputting anything, and multiple times between each row, or something like that it would seem. Example code:

\documentclass[10pt]{article}
\usepackage{setspace}
\usepackage{geometry}
\usepackage{tabularray}
\geometry{hmargin={1.5in, 1.5in}}
\setstretch{1.2}

\usepackage[sc]{mathpazo}
\linespread{1.05}
\usepackage[T1]{fontenc}

\begin{document}

\section{Test}%
{\small
\newcounter{teststep}\setcounter{teststep}{0}
\begin{longtblr}{
    hlines, vlines,
    colspec={cXcccX},
    column{2}={halign=l, valign=m, co=5},
    column{6}={halign=l, co=2},
    row{1}={halign=c, font=\bfseries},
    rowhead=1
}
Step    & Description       & Min   & Max   & Unit  & Measured  \\
    \refstepcounter{teststep}\arabic{teststep}
        & Do a step.
            & 1 & 100   & V & ~ \\
    \refstepcounter{teststep}\arabic{teststep}\label{repstep}
        & Do another step.
            & 1 & 100   & A & ~ \\
    \SetCell[c=6]{halign=j, valign=m, wd={\linewidth-18pt}} This line explains some things.  It's a lot of text and wraps to multiple lines, and isn't numbered, and can't interrupt the numbering of steps around it.  \\
    \refstepcounter{teststep}\arabic{teststep}
        & And like in Step~\ref{repstep}, another.
            & 1 & 100   & W & ~ \\
\end{longtblr}
}

\end{document}

(I could of course put the \refstepcounter stuff inside its own \newcommand{}, it's just written out here instead.)

Result:

Table screenshot

How to keep the numbers from incrementing?

Best Answer

tabularray runs the contents of the table multiple times so, if you need to change counters inside tabularray tables, you need the counter library to restore them, which can be enabled with \UseTblrLibrary{counter}.

\documentclass[10pt]{article}
\usepackage{setspace}
\usepackage{geometry}
\usepackage{tabularray}
\UseTblrLibrary{counter}
\geometry{hmargin={1.5in, 1.5in}}
\setstretch{1.2}

\usepackage[sc]{mathpazo}
\linespread{1.05}
\usepackage[T1]{fontenc}

\begin{document}

\section{Test}%
{\small
\newcounter{teststep}\setcounter{teststep}{0}
\begin{longtblr}{
    hlines, vlines,
    colspec={cXcccX},
    column{2}={halign=l, valign=m, co=5},
    column{6}={halign=l, co=2},
    row{1}={halign=c, font=\bfseries},
    rowhead=1
}
Step    & Description       & Min   & Max   & Unit  & Measured  \\
    \refstepcounter{teststep}\arabic{teststep}
        & Do a step.
            & 1 & 100   & V & ~ \\
    \refstepcounter{teststep}\arabic{teststep}\label{repstep}
        & Do another step.
            & 1 & 100   & A & ~ \\
    \SetCell[c=6]{halign=j, valign=m, wd={\linewidth-18pt}} This line explains some things.  It's a lot of text and wraps to multiple lines, and isn't numbered, and can't interrupt the numbering of steps around it.  \\
    \refstepcounter{teststep}\arabic{teststep}
        & And like in Step~\ref{repstep}, another.
            & 1 & 100   & W & ~ \\
\end{longtblr}
}

\end{document}

enter image description here