1

I am trying to start with an example from Reference table row in LaTeX to create a label for a row, but I have not had any luck getting that to work in a longtblr environment. The code I have so far is:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{array}
\usepackage{float}
\usepackage{makecell}
\usepackage{nameref}
\usepackage{setspace}
\usepackage{tabularray}
\usepackage[absolute,overlay]{textpos}
\usepackage{xcolor}

\newcounter{rowcntr}[table] \renewcommand{\therowcntr}{\thetable.\arabic{rowcntr}} \NewColumnType{N}[1][]{>{\refstepcounter{rowcntr}\therowcntr}c} \AtBeginEnvironment{tblr}{\setcounter{rowcntr}{0}}

\begin{document}

\begin{longtblr}[ caption = {Long Table 1}, label = {tblr:long-table-1}, ]{ colspec = {N[3]|X[5]}, width = \linewidth, rowhead = 1, rowfoot = 1, row{even} = {azure9}, row{1,Z} = {c,font=\sffamily\bfseries,bg=azure5,fg=white} } Col 1 & Col 2 \ \label{row:long-table-1-a} & Ipsum \ \label{row:long-table-1-b} & Ipsum \ Col 1 & Col 2 \ \end{longtblr}

\begin{longtblr}[ caption = {Long Table 2}, label = {tblr:long-table-2}, ]{ colspec = {N[3]|X[5]}, width = \linewidth, rowhead = 1, rowfoot = 1, row{even} = {azure9}, row{1,Z} = {c,font=\sffamily\bfseries,bg=azure5,fg=white} } Col 1 & Col 2 \ \label{row:long-table-2-a} & Ipsum \ \label{row:long-table-2-b} & Ipsum \ Col 1 & Col 2 \ \end{longtblr}

\end{document}

repro

There are several problems, the numbering is off, the index is included in the headers

  • The (wrong) numbering is included in the header and footer.
  • The row indexing is off, it starts at 2 and jumps by 2.
  • The header in the first table has the wrong height.
  • The width of the tables is inconsistent.

What I really want is to omit the number/index column and use a \nameref against the value of the second column, so that each time I reference the label, I pull in the corresponding cell value. With this approach, I can instrument any bad references (i.e., when someone updates the table and breaks the references).

I found reference to a label linked with a longtable row as a possible alternative approach, but this requires duplicating the actual cell value everywhere in the document.

I would be grateful for any guidance.

Ritmo2k
  • 129

1 Answers1

0

The bad numbering is due to the fact that counters in tblrs don't work properly without loading the tblr library counter (tabularray manual, §5.3). For me this also fixed the overall table widths and the extra height in the first row of the first table.

\UseTblrLibrary{counter}

The inconsistent width is because you've based the N column type off of c, which doesn't support width coefficients like X does. Disregarding the other issues for the moment, you could fix this by changing your column definition to:

\NewColumnType{N}[1][]{>{\refstepcounter{rowcntr}\therowcntr}X[#1]}

Here I've replaced c with X[#1]. #1 gets filled in with the optional argument to N. Without it, you effectively get X[1] no matter what argument you give to N.

The remaining, more difficult issue is the fact that your header and footer are numbered (and still wrongly at this point). I think what's going on here is a confusion between the older style column types from array and the new syntax introduced by tabularray. When you do row{1,Z} = {c,...}, c is not a column type that will replace the column type you've already assigned with colspec above; it's a value for key halign (see tabularray manual §2.4--5). Furthermore, even adding preto={}, the tabularray equivalent of >{} to this option set won't work, because you're modifying the definition of the row and not the column.

My recommendation would be to drop the NewColumnType and instead specify the numbering as a property of only the cell range you want numbered. Your table specification would then look like this:

colspec = {X[3]|X[5]},
width = \linewidth,
rowhead = 1,
rowfoot = 1,
row{even} = {azure9},
row{1,Z} = {c,font=\sffamily\bfseries,bg=azure5,fg=white},
cell{2-Y}{1} = {preto={\refstepcounter{rowcntr}\therowcntr}},

If you want to avoid having to copy the same definition for multiple tables, you can use \NewTblrEnviron to make a new table type:

\NewTblrEnviron{mytblr}
\SetTblrOuter[mytblr]{long}
\SetTblrInner[mytblr]{
  colspec = {X[3]|X[5]},
  width = \linewidth,
  rowhead = 1,
  rowfoot = 1,
  row{even} = {azure9},
  row{1,Z} = {c,font=\sffamily\bfseries,bg=azure5,fg=white},
  cell{2-Y}{1} = {preto={\refstepcounter{rowcntr}\therowcntr}},
}

Lastly, you don't need \AtBeginEnvironment{tblr}{\setcounter{rowcntr}{0}} because [table] in \newcounter{rowcntr}[table] already does that. You also don't need array or makecell because you're using tabularray.

Without further ado, here's my version of your MWE:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
%\usepackage{float} &not relevant for longtblr
\usepackage{nameref}
%\usepackage{setspace} %not used here
\usepackage{tabularray}
  \UseTblrLibrary{counter}
%\usepackage[absolute,overlay]{textpos} %not used here
\usepackage{xcolor}

\newcounter{rowcntr}[table] \renewcommand{\therowcntr}{\thetable.\arabic{rowcntr}}

\NewTblrEnviron{mytblr} \SetTblrOuter[mytblr]{long} \SetTblrInner[mytblr]{ colspec = {X[3]|X[5]}, width = \linewidth, rowhead = 1, rowfoot = 1, row{even} = {azure9}, row{1,Z} = {c,font=\sffamily\bfseries,bg=azure5,fg=white}, cell{2-Y}{1} = {preto={\refstepcounter{rowcntr}\therowcntr}}, }

\begin{document}

\begin{mytblr}[ caption = {Long Table 1}, label = {tblr:long-table-1}, ]{} Col 1 & Col 2 \ \label{row:long-table-1-a} & Ipsum \ \label{row:long-table-1-b} & Ipsum \ Col 1 & Col 2 \ \end{mytblr}

\begin{mytblr}[ caption = {Long Table 2}, label = {tblr:long-table-2}, ]{} Col 1 & Col 2 \ \label{row:long-table-2-a} & Ipsum \ \label{row:long-table-2-b} & Ipsum \ Col 1 & Col 2 \ \end{mytblr}

\end{document}


EDIT in response to your comment:

It's odd that the row numbering wouldn't work without defining a new tabularray environment. This code works as intended for me:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{nameref}
\usepackage{tabularray}
  \UseTblrLibrary{counter}
\usepackage{xcolor}

\newcounter{rowcntr}[table] \renewcommand{\therowcntr}{\thetable.\arabic{rowcntr}}

\begin{document}

\begin{longtblr}[ caption = {Long Table 1}, label = {tblr:long-table-1}, ]{ colspec = {X[3]|X[5]}, width = \linewidth, rowhead = 1, rowfoot = 1, row{even} = {azure9}, row{1,Z} = {c,font=\sffamily\bfseries,bg=azure5,fg=white}, cell{2-Y}{1} = {preto={\refstepcounter{rowcntr}\therowcntr}}, } Col 1 & Col 2 \ \label{row:long-table-1-a} & Ipsum \ \label{row:long-table-1-b} & Ipsum \ Col 1 & Col 2 \ \end{longtblr}

\begin{longtblr}[ caption = {Long Table 2}, label = {tblr:long-table-2}, ]{ colspec = {X[3]|X[5]}, width = \linewidth, rowhead = 1, rowfoot = 1, row{even} = {azure9}, row{1,Z} = {c,font=\sffamily\bfseries,bg=azure5,fg=white}, cell{2-Y}{1} = {preto={\refstepcounter{rowcntr}\therowcntr}}, } Col 1 & Col 2 \ \label{row:long-table-2-a} & Ipsum \ \label{row:long-table-2-b} & Ipsum \ Col 1 & Col 2 \ \end{longtblr}

\end{document}

  • Thank you so much for the detailed explanation. I notice that without using the NewTblrEnviron, the counter does not reset between tables. For insight, how can that be fixed? – Ritmo2k Mar 09 '24 at 16:59
  • 1
    @Ritmo2k That's odd. I've added another example to my answer; see if it works for you? – vjeffcott Mar 09 '24 at 17:28