2

I would like share with something. I have created a custom counter to cross-reference sub figures.

For some reason, the counter is wrong if I use it inside tblr environment (the one defined in tabularray package). I tested similar approach with tabular and the latter works as expected.

I am still on TexLive 2021 with the most recent updates:

Version 1.13.2 (TeX Live 2021)  (format=lualatex 2022.3.25)  25 MAR 2022 13:25
Package: tabularray 2022-03-01 v2022A Typeset tabulars and arrays with LaTeX3
Package: cleveref 2018/03/27 v0.21.4 Intelligent cross-referencing

The MWE:

\documentclass[12pt]{article}
\usepackage{graphicx}
\usepackage{tabularray}
    \UseTblrLibrary{booktabs}
\usepackage{cleveref}

\newcounter{mycustomcounter}
\renewcommand\themycustomcounter{(\alph{mycustomcounter})}
\crefname{mycustomcounter}{subfigure}{subfigures}
\Crefname{mycustomcounter}{Subfigure}{Subfigures}
\crefformat{mycustomcounter}{#2subfigure~#1#3}
\Crefformat{mycustomcounter}{#2Subfigure~#1#3}


\begin{document}

\begin{figure}[htbp]
    \setcounter{mycustomcounter}{0}
    \centering
    \begin{tblr}{
        width=0.9\textwidth,
        colspec={X[c] X[c]},
    }
        \includegraphics[width=0.4\textwidth]{example-image-a}
        &
        \includegraphics[width=0.4\textwidth]{example-image-b}
        \\
        \refstepcounter{mycustomcounter}
        \label{fig:example1}
        \themycustomcounter
        &
        \refstepcounter{mycustomcounter}
        \label{fig:example2}
        \themycustomcounter
    \end{tblr}
\end{figure}

\begin{figure}[htbp]
    \setcounter{mycustomcounter}{0}
    \centering
    \begin{tabular}{cc}
        \includegraphics[width=0.4\textwidth]{example-image-a}
        &
        \includegraphics[width=0.4\textwidth]{example-image-b}
        \\
        \refstepcounter{mycustomcounter}
        \label{fig:example3}
        \themycustomcounter
        &
        \refstepcounter{mycustomcounter}
        \label{fig:example4}
        \themycustomcounter
    \end{tabular}
\end{figure}

References to subfigures: \labelcref{fig:example1}, \labelcref{fig:example2}, \labelcref{fig:example3}, \labelcref{fig:example4}.

\end{document}

enter image description here

1
  • I can reproduce, but I can't understand...
    – gusbrs
    Mar 25, 2022 at 13:57

1 Answer 1

2

I don't understand why, but this is documented behavior. The manual says "You need to load counter library with \UseTblrLibrary{counter}, if you want to modify some LaTeX counters inside tabularray tables." And indeed, with it, it works as expected.

\documentclass[12pt]{article}
\usepackage{graphicx}
\usepackage{tabularray}
\UseTblrLibrary{booktabs}
\UseTblrLibrary{counter}
\usepackage{cleveref}

\newcounter{mycustomcounter}
\renewcommand\themycustomcounter{(\alph{mycustomcounter})}
\crefname{mycustomcounter}{subfigure}{subfigures}
\Crefname{mycustomcounter}{Subfigure}{Subfigures}
\crefformat{mycustomcounter}{#2subfigure~#1#3}
\Crefformat{mycustomcounter}{#2Subfigure~#1#3}


\begin{document}

\begin{table}[htbp]
    \setcounter{mycustomcounter}{0}
    \centering
    \begin{tblr}{
        width=0.9\textwidth,
        colspec={X[c] X[c]},
    }
        \includegraphics[width=0.4\textwidth]{example-image-a}
        &
        \includegraphics[width=0.4\textwidth]{example-image-b}
        \\
        \refstepcounter{mycustomcounter}
        \label{fig:example1}
        \themycustomcounter
        &
        \refstepcounter{mycustomcounter}
        \label{fig:example2}
        \themycustomcounter
    \end{tblr}
\end{table}

\begin{table}[htbp]
    \setcounter{mycustomcounter}{0}
    \centering
    \begin{tabular}{cc}
        \includegraphics[width=0.4\textwidth]{example-image-a}
        &
        \includegraphics[width=0.4\textwidth]{example-image-b}
        \\
        \refstepcounter{mycustomcounter}
        \label{fig:example3}
        \themycustomcounter
        &
        \refstepcounter{mycustomcounter}
        \label{fig:example4}
        \themycustomcounter
    \end{tabular}
\end{table}


References to subfigures: \labelcref{fig:example1}, \labelcref{fig:example2}, \labelcref{fig:example3}, \labelcref{fig:example4}.

\end{document}

enter image description here

4
  • Hi, Thanks for the answer. I am glad it can work and sorry I missed that in the first place. I was reading through the manual at some point but never sport such thing.
    – Celdor
    Mar 25, 2022 at 14:27
  • 1
    @Celdor Oh, I didn't mean to be critical, I didn't know it either and have just found out about it. It's just that it's all I know... ;-)
    – gusbrs
    Mar 25, 2022 at 14:30
  • 1
    tabularray processes a tabular more than once, and so counter need special treatment, e.g. github.com/lvjr/tabularray/issues/89 Mar 25, 2022 at 15:37
  • @UlrikeFischer Thanks, that explains it! I'm yet to get acquainted with tabularray, it is in my "to do someday" list. :)
    – gusbrs
    Mar 25, 2022 at 15:39

You must log in to answer this question.

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