
Ich möchte jede Zahl im Array mithilfe von in ein Raster einfügen 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}
Aber ich habe so etwas bekommen. Das Ergebnis ist ähnlich, wenn ich \multido
durch ersetze \foreach
. Die mathematische Operation scheint im Index des Arrays nicht unterstützt zu werden. Wie kann ich das bitte beheben?
Antwort1
Sie sollten verwenden arrayjobx
, aber es unterstützt keine Arithmetik für seine Argumente.
\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}
Einfacher geht es, wenn Sie einen Befehl definieren, der diese Aufgabe erledigt:
\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}
Mit moderneren Werkzeugen:
\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}
Alle drei Codeteile führen zum selben Ergebnis.
Antwort2
Code
\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}