我正在使用宏动态地构建表格,但无法SetCell
使用计数器动态地为单元格着色。
颜色定义如下color@1
,,color@2
...我使用计数器循环遍历值来构建表格内容并为单元格着色。这是我正在处理的整个项目的精简版本,所以我无法更改颜色的语法。
以下是非 NWE:
\documentclass[]{article}
\usepackage{babel}
\usepackage{xcolor}
\usepackage{graphicx} % Enhanced Picture
\usepackage{chngcntr} % Change counters
\usepackage{etoolbox} % Class manipulation
\usepackage{tabularray} % New table style
\UseTblrLibrary{functional} % Storing table data in macro
\UseTblrLibrary{counter} % BEamer splitting tables
\definecolor{BXcolor}{rgb}{0.5, 0.0, 0.13}
\colorlet{BXcolorFG}{white}
% Color for the gradations
\definecolor{color@1}{RGB}{255, 221, 0}
\colorlet{color@2}{orange}
\definecolor{color@3}{RGB}{255, 0, 0}
\definecolor{color@4}{RGB}{35, 35, 35}
\colorlet{colorEmpty}{black!10}
% Colors for the text above the gradations
\colorlet{colorFG@}{cyan}
\colorlet{colorFG@1}{black}
\colorlet{colorFG@2}{black}
\colorlet{colorFG@3}{black}
\colorlet{colorFG@4}{white}
\newcount\tableLines
\newcounter{tableLine}
\newcommand\storeTableData[1]{
\setcounter{tableLine}{0}%\the\numexpr4-#1}
\def\tabledata{} \tableLines=4
\loop
\addto\tabledata{
\stepcounter{tableLine}
\def\tablelinecolor{\arabic{tableline}}
test & \SetCell{color@\arabic{tableline}} \textcolor{color@\arabic{tableLine}}{Test} & Test \\
}
\advance \tableLines -1
\ifnum \tableLines>0\relax
\repeat
}%
\IgnoreSpacesOn
\prgNewFunction \printTableData {} {
% Simple function to return the content of the macro inside the table
\prgReturn {\tlUse \tabledata}
}
\IgnoreSpacesOff
\newcommand{\displayTable}[1]{%
% Build the table data
\storeTableData{#1}
% Draw the table
\begin{tblr}[evaluate=\printTableData]{Q[c,m]Q[c,m]X[l]}
Test & Test & TEst \\
\printTableData
\end{tblr}
}
\begin{document}
\displayTable{test}
\end{document}
我收到此错误:
! Package xcolor Error: Undefined color `color@\arabic {tableline}'.
这让我觉得宏\arabic{tableline}
没有扩展到其内容。我该如何解决这个问题?
答案1
rownum
使用由 提供的计数器(值)tabularray
代替 计数器tableLine
:
\addto\tabledata{%
test &
\SetCell{
bg=color@\inteval{\arabic{rownum}-1},
fg=colorFG@\inteval{\arabic{rownum}-1}
}
Test & Test \\
}
看起来包counter
中的库tabularray
没有想象中那么强大。
完整示例
\documentclass[]{article}
\usepackage{babel}
\usepackage{xcolor}
\usepackage{graphicx} % Enhanced Picture
\usepackage{chngcntr} % Change counters
\usepackage{etoolbox} % Class manipulation
\usepackage{tabularray} % New table style
\UseTblrLibrary{functional} % Storing table data in macro
\UseTblrLibrary{counter} % BEamer splitting tables
\definecolor{BXcolor}{rgb}{0.5, 0.0, 0.13}
\colorlet{BXcolorFG}{white}
% Color for the gradations
\definecolor{color@1}{RGB}{255, 221, 0}
\colorlet{color@2}{orange}
\definecolor{color@3}{RGB}{255, 0, 0}
\definecolor{color@4}{RGB}{35, 35, 35}
\colorlet{colorEmpty}{black!10}
% Colors for the text above the gradations
\colorlet{colorFG@}{cyan}
\colorlet{colorFG@1}{black}
\colorlet{colorFG@2}{black}
\colorlet{colorFG@3}{black}
\colorlet{colorFG@4}{white}
\newcount\tableLines
\newcounter{tableLine}
\newcommand\storeTableData[1]{
\setcounter{tableLine}{0}%\the\numexpr4-#1}
\def\tabledata{} \tableLines=4
\loop
\addto\tabledata{%
test &
\SetCell{
bg=color@\inteval{\arabic{rownum}-1},
fg=colorFG@\inteval{\arabic{rownum}-1}
}
Test & Test \\
}
\advance \tableLines -1\relax
\ifnum \tableLines>0\relax
\repeat
}%
\IgnoreSpacesOn
\prgNewFunction \printTableData {} {
% Simple function to return the content of the macro inside the table
\prgReturn {\tlUse \tabledata}
}
\IgnoreSpacesOff
\newcommand{\displayTable}[1]{%
% Build the table data
\storeTableData{#1}
% Draw the table
\begin{tblr}[evaluate=\printTableData]{Q[c,m]Q[c,m]X[l]}
Test & Test & TEst \\
\printTableData
\end{tblr}
}
\begin{document}
\displayTable{test}
\end{document}
更新
\SetCell
在每行的第一个单元格中
\addto\tabledata{%
\SetCell{
bg=color@\inteval{\arabic{rownum}-1},
fg=colorFG@\inteval{\arabic{rownum}-1}
}
test &
Test & Test \\
}