admin管理员组

文章数量:1313807

How come that this piece of code does not compile right in Texmaker on an iMac?

\documentclass{article}
\usepackage{tabularray}
\newcounter{linecounter}
\begin{document}
% Initialize the line counter
\setcounter{linecounter}{1}
\begin{tblr}{
  colspec={@{}ll@{}}}
\hline
\textbf{Line Number} & \textbf{Text} \\
\hline
\thelinecounter & First line of text. \\
\addtocounter{linecounter}{1}
\thelinecounter & Second line of text. \\
\addtocounter{linecounter}{1}
\thelinecounter & Third line of text. \\
%\addtocounter{linecounter}{1}
\hline
\end{tblr}
\end{document}

gives

Line Number  Text
5          First line of text.
7          Second line of text.
9          Third line of text.

How come that this piece of code does not compile right in Texmaker on an iMac?

\documentclass{article}
\usepackage{tabularray}
\newcounter{linecounter}
\begin{document}
% Initialize the line counter
\setcounter{linecounter}{1}
\begin{tblr}{
  colspec={@{}ll@{}}}
\hline
\textbf{Line Number} & \textbf{Text} \\
\hline
\thelinecounter & First line of text. \\
\addtocounter{linecounter}{1}
\thelinecounter & Second line of text. \\
\addtocounter{linecounter}{1}
\thelinecounter & Third line of text. \\
%\addtocounter{linecounter}{1}
\hline
\end{tblr}
\end{document}

gives

Line Number  Text
5          First line of text.
7          Second line of text.
9          Third line of text.
Share Improve this question edited Jan 31 at 22:02 Robert 8,69354 gold badges129 silver badges170 bronze badges asked Jan 31 at 14:31 algonalgon 111 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 0

tabularray processes the content of a table multiple times to determine the widths of columns etc.

You can use the counter library to get the correct numbering:

\documentclass{article}
\usepackage{tabularray}
\UseTblrLibrary{counter}
\newcounter{linecounter}
\begin{document}
% Initialize the line counter
\setcounter{linecounter}{1}
\begin{tblr}{
  colspec={@{}ll@{}}}
\hline
\textbf{Line Number} & \textbf{Text} \\
\hline
\thelinecounter & First line of text. \\
\addtocounter{linecounter}{1}\thelinecounter & Second line of text. \\
\addtocounter{linecounter}{1}\thelinecounter & Third line of text. \\
%\addtocounter{linecounter}{1}
\hline
\end{tblr}
\end{document}

Or you could use the existing counters tabularray already provides:

\documentclass{article}
\usepackage{tabularray}
\UseTblrLibrary{counter}
\newcounter{linecounter}
\begin{document}

\begin{tblr}{
  colspec={@{}ll@{}},
}
\hline
\textbf{Line Number} & \textbf{Text} \\
\hline
\the\numexpr\arabic{rownum}-1 & First line of text. \\
\the\numexpr\arabic{rownum}-1 & Second line of text. \\
\the\numexpr\arabic{rownum}-1 & Third line of text. \\
\hline
\end{tblr}
\end{document}

本文标签: latexgenerating count inside tabularray environmentStack Overflow