1

This is a followup question to a similar problem:

I like to create a table environment and to have the option creating a label/caption.

This is my MWE:

\documentclass{scrbook}

\usepackage{xcolor,xparse}
\usepackage{tabularray}

\NewTblrTheme{exc-main}{
  \SetTblrStyle{head}{%
    bg=blue!10,
    fg=blue
  }
  \SetTblrInner{rowsep=0pt,hspan=minimal}
  \SetTblrInner{%
    row{odd} = {bg=black!10},
    row{1}={%
      bg=blue!10,
      fg=blue,
      font=\bfseries,
      halign={c},
      valign={m},
    },
    width = \linewidth,
    hline{1,Z} = {2pt,fg=red}, % toprule and bottomrule
    hline{2} = {1pt,fg=red}, % midrule
    }
  }

  \NewDocumentEnvironment{excTableEnv}{ o m o }{%
    \IfValueT{#1}{\SetTblrOuter{#1}}%
    \IfValueT{#3}{\SetTblrInner{#3}}%
  \begin{tblr}[%
    theme=exc-main,
      entry=none,
      label=none,
      long,
      ]{%
        colspec ={#2},
        row{2-Z}={font=\sffamily},
        rowhead = 1, %<- first row is head
      }
    }
    {\end{tblr}}

  \begin{document}
  \chapter{test}
\begin{excTableEnv}{XX}
  Top & Top \\
  tba & tba \\
\end{excTableEnv}

  \ref{tab:1}
\begin{excTableEnv}[label={tab:1},caption={something}]{XX}
  Head &  Head \\
  tba & tba \\
\end{excTableEnv}

\begin{excTableEnv}{XX}
  Top & Top \\
  tba & tba \\
\end{excTableEnv}
\end{document}

enter image description here

It works fine for the environment without any optional argument. But label={tab:1} is not considered or does not overwrite the default setting.

3
  • Try to remove label=none... Commented Jan 22 at 12:32
  • Then it works, but as default I do not want to have the table labelled or have a number for it. Just occsionnally there should be a caption, label, number to it. Commented Jan 22 at 12:40
  • Remove label=none and use \IfValueTF{#1}{\SetTblrOuter{label=none,#1}}{\SetTblrOuter{label=none}}. Commented Jan 22 at 15:19

1 Answer 1

1

Local keys take precedence over keys provided by \SetTblrOuter.

Here is a solution:

\documentclass{scrbook}

\usepackage{xcolor,xparse}
\usepackage{tabularray}

\NewTblrTheme{exc-main}{
  \SetTblrStyle{head}{%
    bg=blue!10,
    fg=blue
  }
  \SetTblrInner{rowsep=0pt,hspan=minimal}
  \SetTblrInner{%
    row{odd} = {bg=black!10},
    row{1}={%
      bg=blue!10,
      fg=blue,
      font=\bfseries,
      halign={c},
      valign={m},
    },
    width = \linewidth,
    hline{1,Z} = {2pt,fg=red}, % toprule and bottomrule
    hline{2} = {1pt,fg=red}, % midrule
  }
}

\NewDocumentEnvironment{excTableEnv}{o m o}{%
  \IfValueTF{#1}{\SetTblrOuter{label=none,#1}}{\SetTblrOuter{label=none}}%
  %\IfValueT{#1}{\SetTblrOuter{#1}}%
  \IfValueT{#3}{\SetTblrInner{#3}}%
  \begin{tblr}[%
      theme=exc-main,
      entry=none,
      %label=none,
      long,
    ]{%
      colspec ={#2},
      row{2-Z}={font=\sffamily},
      rowhead = 1, %<- first row is head
    }
  }{
  \end{tblr}
}

\begin{document}
\chapter{test}

\begin{excTableEnv}{XX}
  Top & Top \\
  tba & tba \\
\end{excTableEnv}

\ref{tab:1}

\begin{excTableEnv}[caption={something},label={tab:1}]{XX}
  Head &  Head \\
  tba & tba \\
\end{excTableEnv}

\begin{excTableEnv}{XX}
  Top & Top \\
  tba & tba \\
\end{excTableEnv}
\end{document}

You must log in to answer this question.

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