对于自定义 tabularray 环境,标签和标题是可选的

对于自定义 tabularray 环境,标签和标题是可选的

这是一个后续问题类似问题

我喜欢创建一个表格环境并可以选择创建标签/标题。

这是我的 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}

在此处输入图片描述

它在没有任何可选参数的情况下可以很好地适用于环境。但是label={tab:1}不考虑或不会覆盖默认设置。

答案1

本地密钥优先于 提供的密钥\SetTblrOuter

这是一个解决方案:

\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}

相关内容