![如何在由包 arrayjob 创建的数组索引中使用数学运算](https://linux22.com/image/449768/%E5%A6%82%E4%BD%95%E5%9C%A8%E7%94%B1%E5%8C%85%20arrayjob%20%E5%88%9B%E5%BB%BA%E7%9A%84%E6%95%B0%E7%BB%84%E7%B4%A2%E5%BC%95%E4%B8%AD%E4%BD%BF%E7%94%A8%E6%95%B0%E5%AD%A6%E8%BF%90%E7%AE%97.png)
我想使用 将数组中的每个数字放在网格上tikz
。
\documentclass[tikz]{standalone}
\usepackage{pgffor}
\usepackage{multido}
\usetikzlibrary{calc}
\usepackage{arrayjob}
\newarray\Values
\readarray{Values}{I&F&I&G&L&O&C&K&E&D&A&W&A&Y&.}
\begin{document}
\begin{tikzpicture}
\draw (0,0) grid(5,3);
% \foreach \x in {1,2,3,4,5}
% \foreach \y in {1,2,3}
% {
% \node at (\x-0.5,\y-0.5) {\Values(\x+\y*5)};
% }
\multido{\dx=0cm+1cm,\iColumn=1+1}{5}{%
\multido{\dy=0cm+1cm,\iRow=1+2}{3}{%
\node at (\dx+0.5cm,\dy+0.5cm) {\Values(\iColumn+\iRow*5)};}}
\end{tikzpicture}
\end{document}
\multido
但我得到了类似的东西。如果我用替换,结果是相似的\foreach
。数组的索引似乎不支持数学运算。请问我该如何解决这个问题?
答案1
您应该使用arrayjobx
,但它不支持对其参数进行算术运算。
\documentclass[tikz]{standalone}
\usepackage{pgffor}
\usepackage{multido}
\usetikzlibrary{calc}
\usepackage{arrayjobx}
\newarray\Values
\readarray{Values}{I&F&I&G&L&O&C&K&E&D&A&W&A&Y&.}
\begin{document}
\begin{tikzpicture}
\draw (0,0) grid(5,3);
\foreach \x in {1,2,3,4,5}
{
\foreach \y in {1,2,3}
{
\node at (\x-0.5,\y-0.5) {\expandafter\Values\expandafter(\the\numexpr\x+\y*5)};
}
}
\end{tikzpicture}
\end{document}
更简单的是,定义一个执行该作业的命令:
\documentclass[tikz]{standalone}
\usepackage{pgffor}
\usepackage{multido}
\usetikzlibrary{calc}
\usepackage{arrayjobx}
\newarray\Values
\readarray{Values}{I&F&I&G&L&O&C&K&E&D&A&W&A&Y&.}
\newcommand{\Get}[2]{%
\expandafter#1\expandafter(\the\numexpr#2\relax)%
}
\begin{document}
\begin{tikzpicture}
\draw (0,0) grid(5,3);
\foreach \x in {1,2,3,4,5}
{
\foreach \y in {1,2,3}
{
\node at (\x-0.5,\y-0.5) {\Get\Values{\x+\y*5}};
}
}
\end{tikzpicture}
\end{document}
使用更现代化的工具:
\documentclass[tikz]{standalone}
\usepackage{pgffor}
\usetikzlibrary{calc}
\ExplSyntaxOn
\NewDocumentCommand{\definearray}{mm}
{
\seq_clear_new:c { l_jasmine_array_#1_seq }
\seq_set_split:cnn { l_jasmine_array_#1_seq } { & } { #2 }
}
\cs_generate_variant:Nn \seq_set_split:Nnn { c }
\NewExpandableDocumentCommand{\getarrayitem}{mm}
{
\seq_item:cn { l_jasmine_array_#1_seq } { #2 }
}
\ExplSyntaxOff
\definearray{Values}{I&F&I&G&L&O&C&K&E&D&A&W&A&Y&.}
\begin{document}
\begin{tikzpicture}
\draw (0,0) grid(5,3);
\foreach \x in {1,2,3,4,5}
{
\foreach \y in {1,2,3}
{
\node at (\x-0.5,\y-0.5) {\getarrayitem{Values}{\x+\y*5}};
}
}
\end{tikzpicture}
\end{document}
这三段代码均产生相同的结果。
答案2
代码
\documentclass[tikz]{standalone}
\usetikzlibrary{graphs}
\begin{document}
\tikz
\graph[
grid placement, wrap after=5, branch up,
fresh nodes, nodes={
draw, minimum size=+1cm, text height=+\ht\strutbox, text depth=+0pt},
]{I, F, I, G, L, O, C, K, E, D, A, W, A, Y, .};
\tikz
\draw (0,0) grid (5,3) [shift={(.5,.5)}]
graph[
grid placement, wrap after=5, branch up,
fresh nodes, nodes={text height=+\ht\strutbox, text depth=+0pt},
]{I, F, I, G, L, O, C, K, E, D, A, W, A, Y, .};
\tikz
\node foreach[count=\i from 0] \t in {I, F, I, G, L, O, C, K, E, D, A, W, A, Y, .}
[draw, minimum size=+1cm, text height=+\ht\strutbox, text depth=+0pt]
at ({mod(\i,5)},{int(\i/5)}) {\t};
\end{document}