我想根据存储在数组中的值设置 arowcolor
和 a 的单元格内容( )。下一个 MWEtabular
arrayjob
\documentclass[10pt]{article}
\usepackage{arrayjob}
%\usepackage{etoolbox}
\usepackage{ifthen}
\usepackage{multido}
\newarray\numbers
\newcounter{cptr}
\newcommand{\updatearray}[1]{
\stepcounter{cptr}%
\numbers(\thecptr)=#1%
Inside updatearray, numbers(\thecptr) = \numbers(\thecptr)\\
}
\begin{document}
% populating array
\noindent\updatearray{1}\updatearray{2}\updatearray{3}\updatearray{4}
% array content
\noindent Array content\\
\multido{\i=1+1}{\thecptr}{numbers(\i) = \numbers(\i)\\}
% array into table
\noindent This is the table\\
\begin{tabular}{|p{3cm}|c|}
\hline
\multicolumn{1}{|c}{\textbf{Numbers}} &
\multicolumn{1}{|c|}{\textbf{Output}} \\ \hline
\let\ListNumbers\empty
\begingroup
\let\numbers\relax
\let\\\relax
\let\hline\relax
\multido{\iNumber=1+1}{\thecptr}{
\xdef\ListNumbers{\ListNumbers
\numbers(\iNumber) & ifthenelese(numbers(\iNumber)$<3$)(lower+color1)(greater+color2)\\ \hline}}
\endgroup
\ListNumbers
\end{tabular}
\delarray\numbers
\end{document}
给出输出
期望的输出类似于
请注意,必须if condition
依赖于数组值。
答案1
我可以为您提供一个expl3
接口:
\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\NewArray}{mm}
{
\seq_new:c { g_aloui_array_#1_seq }
\seq_set_from_clist:cn { g_aloui_array_#1_seq } { #2 }
}
\NewDocumentCommand{\PrintArray}{mmmm}
{% #1 = array name, #2 = treshold, #3 = lower color, #4 = higher color
\aloui_array_print:nnnn { #1 } { #2 } { #3 } { #4 }
}
\tl_new:N \l_aloui_array_tablebody_tl
\cs_new_protected:Nn \aloui_array_print:nnnn
{
% start the table preamble
\tl_set:Nn \l_aloui_array_tablebody_tl
{
\begin{tabular}{|p{3cm}|c|}
\hline
\multicolumn{1}{|c|}{\textbf{Numbers}} & \textbf{Output} \\
\hline
}
% add rows
\seq_map_inline:cn { g_aloui_array_#1_seq }
{
\int_compare:nTF { ##1 < #2 }
{% the item is below the treshold
\tl_put_right:Nn \l_aloui_array_tablebody_tl
{
\rowcolor{#3} ##1 & lower \\ \hline
}
}
{% the item is above the treshold
\tl_put_right:Nn \l_aloui_array_tablebody_tl
{
\rowcolor{#4} ##1 & higher \\ \hline
}
}
}
% finish up the table
\tl_put_right:Nn \l_aloui_array_tablebody_tl { \end{tabular} }
% print the table
\tl_use:N \l_aloui_array_tablebody_tl
}
\ExplSyntaxOff
\begin{document}
\NewArray{aloui}{1,2,3,4}
\PrintArray{aloui}{3}{red!60}{red!20}
\end{document}
适应浮点值会很容易。