我正在尝试为经常重复使用的时间表创建自定义 tblr 环境。我想添加一个可选标签以供交叉引用。
我正在将 xparse 命令与 tabularray 一起使用,但是无法让 tabularray在外部规范中\NewDocumentEnvironment
扩展命令。\IfValueT{#1}{label={#1},}
如果我使用\IfValueTF
并复制,一切都会正常\begin{tblr}[
,但这似乎不太健全。
有没有办法让下面 MWE 中的“testa”选项起作用?
梅威瑟:
\documentclass{scrreprt}
\usepackage{tabularray}
\NewDocumentEnvironment{testa}{o m +b}{
\begin{tblr}[
\IfValueT{#1}{label={#1},}
caption={#2},
long,
]{
colspec = {ll},
}
Column 1 & Column 2 \\
#3
\end{tblr}
}{}
\NewDocumentEnvironment{testb}{o m +b}{
\IfValueTF{#1}{\begin{tblr}[label={#1},}{\begin{tblr}[}
caption={#2},
long,
]{
colspec = {ll},
}
Column 1 & Column 2 \\
#3
\end{tblr}
}{}
\begin{document}
\begin{testa}[labela]{captiona}
a & b
\end{testa}
\begin{testb}[labelb]{captionb}
a & b
\end{testb}
Tables \ref{labela} and \ref{labelb} should be the same, but Table \ref{labela} is an undefined reference.
\end{document}
答案1
我将在表格之外设置选项:
\documentclass{scrreprt}
\usepackage{tabularray}
\NewDocumentEnvironment{testa}{o m +b}{%
\IfValueT{#1}{\SetTblrOuter{label={#1}}}%
\begin{tblr}[
caption={#2},
long,
]{
colspec = {ll},
}
Column 1 & Column 2 \\
#3
\end{tblr}
}{}
\begin{document}
\begin{testa}[labela]{captiona}
a & b
\end{testa}
Tables \ref{labela}
\end{document}
答案2
你可以选择
\documentclass{scrreprt}
\usepackage{tabularray}
\NewDocumentEnvironment{testa}{o m +b}{%
\expanded{\noexpand\begin{tblr}[
\IfValueT{#1}{label={#1},}}
caption={#2},
long,
]{
colspec = {ll},
}
Column 1 & Column 2 \\
#3
\end{tblr}%
}{}
\begin{document}
\begin{testa}[labela]{captiona}
a & b
\end{testa}
\end{document}
更简洁的版本:
\documentclass{scrreprt}
\usepackage{tabularray}
\ExplSyntaxOn
\NewDocumentCommand{\testastart}{mm}
{
\jw_testastart:en { \tl_if_novalue:nF { #1 } { label={#1} } } { #2 }
}
\cs_new_protected:Nn \jw_testastart:nn
{
\begin{tblr}[#1, caption={#2},long]
}
\cs_generate_variant:Nn \jw_testastart:nn { e }
\ExplSyntaxOff
\NewDocumentEnvironment{testa}{o m +b}{%
\testastart{#1}{#2}{
colspec = {ll},
}
Column 1 & Column 2 \\
#3
\end{tblr}%
}{}
\begin{document}
\begin{testa}[labela]{captiona}
a & b
\end{testa}
\end{document}
文件.aux
\relax
\providecommand*\new@tpo@label[2]{}
\newlabel{labela}{{1}{1}{}{}{}}
\@writefile{lot}{\contentsline {table}{\numberline {1}{captiona}}{1}{}\protected@file@percent }
\gdef \@abspage@last{1}