4

For a project I am working on, tabularray has been suggested in the answers to my previous questions (see here and here).

However, there will be other floats (figures, listings, etc.) with captions styled via \usepackage{caption}. The problem is that the caption provided by tabularray does not react to the settings made with \usepackage{caption}.

Since the design requirement is that all caption have to look the same, it is required to re-style the caption of longtblr.

Starting point

tabularray provides with the \DefTblrTemplate macro a possible way to re-style the macro manually.

\documentclass{scrreprt}

\usepackage{tabularray} % for table typesetting

% modifying the captions of tabularray \DefTblrTemplate{caption-tag}{default}{\small \bfseries Table\hspace{0.25em}\thetable} \DefTblrTemplate{caption-sep}{default}{\small \bfseries :\enskip} \DefTblrTemplate{caption-text}{default}{\small\InsertTblrText{caption}}

% setting the captions for all floats except tabularray \usepackage{caption} \captionsetup{format=plain,labelformat=simple,labelfont=bf,font=small,labelsep = colon}

\begin{document}

\begin{longtblr}[ caption = {The Caption of the {\ttfamily longtblr} environment.}, ]{ colspec = {rccl}, hline{Z} = {0.08em}, hline{2} = {0.05em}, row{1} = {font=\bfseries}, rowhead = 1, } date & time & time zone & event \ 2019/01/01 & 00:00 & CET & server installation finished\ 2019/01/01 & 00:05 & CET & server successfully booted\ 2019/01/01 & 00:06 & CET & starting xyz daemon\ \end{longtblr}

\begin{table}[h!] \centering \caption{Caption of a standard table styled with {\ttfamily captions}.}\vspace{0.25cm} \begin{tabular}{rccl} \textbf{date} & \textbf{time} & \textbf{time zone} & \textbf{event} \ \hline 2019/01/01 & 00:00 & CET & server installation finished\ 2019/01/01 & 00:05 & CET & server successfully booted\ 2019/01/01 & 00:06 & CET & starting xyz daemon\ \hline \end{tabular} \end{table}

\end{document}

This produces very similar caption styling; except for the space between "Table" and the number (which is not a big deal for me and could easily be fixed). However, a user would have to do the setup twice.

Question

  1. Is there an easy way to copy the caption settings of \usepackage{caption} to tabularray?
  2. Or is it easier to re-define the standard \caption macro manually instead of relying on the \usepackage{caption}?

I have tried to use an if-statement and helper macros, which of course lead to missing \csname or expansion errors. And honestly, the code was getting quite messy.

L.J.R.
  • 10,932
dsacre
  • 327
  • 1
  • 7
  • I have edited your question, removing unrelated text, to make the decription and the example minimal. – L.J.R. Jan 04 '22 at 02:48

1 Answers1

5

In fact, caption package provides \captionof command, which can be used by tabularray package:

\documentclass{scrreprt}

% setting the captions for all floats except tabularray \usepackage{caption} \captionsetup{format=plain,labelformat=simple,labelfont=bf,font=small,labelsep = colon}

\usepackage{tabularray}

\ExplSyntaxOn \prg_generate_conditional_variant:Nnn \tl_if_empty:n { e } { TF } \let \IfTokenListEmpty = \tl_if_empty:eTF \ExplSyntaxOff

% modifying the captions of tabularray \DefTblrTemplate{firsthead}{default}{% \addtocounter{table}{-1}% \IfTokenListEmpty{\InsertTblrText{entry}}{% \captionof{table}{\InsertTblrText{caption}}% }{% \captionof{table}[\InsertTblrText{entry}]{\InsertTblrText{caption}}% }% } \DefTblrTemplate{middlehead,lasthead}{default}{% \addtocounter{table}{-1}% \captionof{table}[]{\InsertTblrText{caption}(Continued)}% } \SetTblrTemplate{caption-lot}{empty}

\begin{document}

\listoftables

\begin{longtblr}[ caption = {The Caption of {\ttfamily longtblr} environment.}, ]{ colspec = {rccl}, hline{Z} = {0.08em}, hline{2} = {0.05em}, row{1} = {font=\bfseries}, rowhead = 1, } date & time & time zone & event \ 2019/01/01 & 00:00 & CET & server installation finished\ 2019/01/01 & 00:05 & CET & server successfully booted\ 2019/01/01 & 00:06 & CET & starting xyz daemon\ \end{longtblr}

\begin{longtblr}[ caption = {The Caption of {\ttfamily longtblr} environment.}, entry = {The Caption in LOT}, ]{ colspec = {rccl}, hline{Z} = {0.08em}, hline{2} = {0.05em}, row{1} = {font=\bfseries}, rowhead = 1, } date & time & time zone & event \ 2019/01/01 & 00:00 & CET & server installation finished\ 2019/01/01 & 00:05 & CET & server successfully booted\ 2019/01/01 & 00:06 & CET & starting xyz daemon\ \end{longtblr}

\begin{table}[h!] \centering \caption{Caption of a standard table styled with {\ttfamily captions}.}\vspace{0.25cm} \begin{tabular}{rccl} \textbf{date} & \textbf{time} & \textbf{time zone} & \textbf{event} \ \hline 2019/01/01 & 00:00 & CET & server installation finished\ 2019/01/01 & 00:05 & CET & server successfully booted\ 2019/01/01 & 00:06 & CET & starting xyz daemon\ \hline \end{tabular} \end{table}

\end{document}

enter image description here

L.J.R.
  • 10,932
  • 1
    I would suggest to add \InsertTblrText{entry} in the optional argument of \captionof and add \SetTblrTemplate{caption-lot}{empty}. Otherwise there will be duplicate entries in the list of tables. – marv Jan 18 '22 at 16:02
  • @marv Thanks. I have updated the answer. Note that I need to check whether \InsertTblrText{entry} is empty. – L.J.R. Jan 19 '22 at 02:52
  • 2
    That makes sense. Maybe this could be incorperated in a \NewTblrLibrary{caption} ? The template system of tabularray is very nice, but the caption package is very popular and setting styles with two different syntaxes is cumbersome. – marv Jan 19 '22 at 07:14
  • 3
    @marv Yes, it is a good idea to add a new library caption. – L.J.R. Jan 19 '22 at 09:51
  • 1
    @L.J.R. and @marv A \NewTblrLibrary{caption} would be great! – Dr. Manuel Kuehner Feb 21 '22 at 21:22
  • 2
    this doesn't work well if hyperref is used, you get duplicated destinations: destination with the same identifier (name{table.0.2}). – Ulrike Fischer Feb 02 '23 at 17:21
  • @UlrikeFischer I want to use this solution as a personal library (my own sty). The problem you mention does cause any weird stuff that should I be aware of? If I need to open a new question let me know. – Mane32 Jan 02 '24 at 17:28