我正在尝试在 foreach 语句中改变颜色。这里描述了一种根据 foreach 变量执行此操作的方法。但是,我想使用 HTML 颜色代码,因此我决定使用 arrayjob。似乎无法\definecolor
解析\Color(\the\value{i})
。我收到错误:
! Incomplete \iffalse; all text was ignored after line 30.
<inserted text>
\fi
最简示例:
\documentclass[12pt]{article}
\usepackage[a4paper, landscape, bottom=17.9mm, left=9.6mm, top=17.9mm, right=9.6mm, nohead, nofoot]{geometry}
\usepackage{arrayjob}
\usepackage{xcolor}
\usepackage{tikz}
\begin{document}
\newarray{\Color}
\readarray{Color}
{
006400&%Dunkelgrün
008000&%Grün
228B22&%Waldgrün
2E8B57&%Seegrün
}
\newcounter{i}
\setcounter{i}{1}
\pagestyle{empty}
%%% Info %%%
\begin{tikzpicture}[x=1cm,y=1cm]
\foreach \x in {0,9,18}
{
\foreach \y in {0,6,12}
{
\definecolor{currentcolor}{HTML}{\Color(\the\value{i})}%006400
\draw[step=1mm,line width=0.1pt,color=currentcolor] (\x-4.255,\y-2.705) grid (\x+4.255,\y+2.705);
}
}
\end{tikzpicture}
\newpage
\end{document}
答案1
该arrayjob
包不应该与 LaTeX 一起使用,因为它重新定义了\array
;有替代品arrayjobx
,但它对这个应用程序没用,因为构造\Color(\the\value{i})
是一组指令来打印006400,而不是字符串本身。
您可以使用 PGF 内置的数组功能。
\documentclass[12pt]{article}
\usepackage[a4paper, landscape, bottom=17.9mm, left=9.6mm, top=17.9mm, right=9.6mm, nohead, nofoot]{geometry}
\usepackage{xcolor}
\usepackage{tikz}
\begin{document}
\newcommand{\Colors}{{%
"000000",%Schwarz
"8FBC8F",%Dunkles Schiefergrau
"708090",%Schiefergrau
"778899",%Helles Schiefergrau
"B0C4DE",%Helles Stahlblau
"696969",%Mattes Grau
"808080",%Grau
"A9A9A9",%Dunkelgrau
"C0C0C0"%Silber
}}
\newcounter{i}
\setcounter{i}{0}
\pagestyle{empty}
%%% Info %%%
\begin{tikzpicture}[x=1cm,y=1cm]
\foreach \x in {0,9,18}
{
\foreach \y in {0,6,12}
{
\pgfmathsetmacro{\thecurrentcolor}{\Colors[\value{i}]}
\definecolor{currentcolor}{HTML}{\thecurrentcolor}%006400
\draw[step=1mm,line width=0.1pt,color=currentcolor] (\x-4.255,\y-2.705) grid (\x+4.255,\y+2.705);
\stepcounter{i}
}
}
\end{tikzpicture}
\end{document}
一种更接近工作原理的不同实现arrayjobx
是使用expl3
和xparse
。
\documentclass[12pt]{article}
\usepackage[a4paper, landscape, bottom=17.9mm, left=9.6mm, top=17.9mm, right=9.6mm, nohead, nofoot]{geometry}
\usepackage{xcolor}
\usepackage{tikz}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\definearray}{mm}
{ % #1 is the array name, #2 the comma separated values
\seq_clear_new:c { l_tomson_array_#1_seq }
\seq_set_split:cnn { l_tomson_array_#1_seq } { , } { #2 }
\cs_if_exist:cF { #1 }
{
\cs_new:cpn { #1 } { \tomson_array_use:nn { #1 } }
}
}
\cs_generate_variant:Nn \seq_set_split:Nnn { c }
\cs_new:Nn \tomson_array_use:nn
{ % #1 is the array name, #2 is an integer
\seq_item:cn { l_tomson_array_#1_seq } { #2 }
}
\ExplSyntaxOff
\definearray{Colors}{
000000, % Schwarz
8FBC8F, % Dunkles Schiefergrau
708090, % Schiefergrau
778899, % Helles Schiefergrau
B0C4DE, % Helles Stahlblau
696969, % Mattes Grau
808080, % Grau
A9A9A9, % Dunkelgrau
C0C0C0 % Silber
}
\newcounter{i}
\setcounter{i}{1}
\begin{document}
\pagestyle{empty}
%%% Info %%%
\begin{tikzpicture}[x=1cm,y=1cm]
\foreach \x in {0,9,18}
{
\foreach \y in {0,6,12}
{
\definecolor{currentcolor}{HTML}{\Colors{\value{i}}}% 006400
\draw[step=1mm,line width=0.1pt,color=currentcolor] (\x-4.255,\y-2.705) grid (\x+4.255,\y+2.705);
\stepcounter{i}
}
}
\end{tikzpicture}
\end{document}
当您这样做时,\definearray{Colors}{...}
您还定义了一个宏\Colors
,该宏以整数作为参数并从数组中提取相应的项。