![对于自定义 tabularray 环境,标签和标题是可选的](https://linux22.com/image/473016/%E5%AF%B9%E4%BA%8E%E8%87%AA%E5%AE%9A%E4%B9%89%20tabularray%20%E7%8E%AF%E5%A2%83%EF%BC%8C%E6%A0%87%E7%AD%BE%E5%92%8C%E6%A0%87%E9%A2%98%E6%98%AF%E5%8F%AF%E9%80%89%E7%9A%84.png)
这是一个后续问题类似问题:
我喜欢创建一个表格环境并可以选择创建标签/标题。
这是我的 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}