7
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}

    \foreach \x [count=\xi from 1] in {-3.0,-2.8,...,1.8}
        \draw[thin] (0,0) ++(\x,2) rectangle ++(0.2, 0.2) coordinate [pos=.5] (one\xi);

    \foreach \x [count=\ai from 1] in {1,0.8,...,0.2}
        \foreach \y [count=\bi from 0] in {1,0.8,...,0.2}
            \draw[thin] (0,0) ++(-\x, -\y) rectangle ++(0.2, 0.2) coordinate [pos=.5] (two);
            %label should be \ai + (\bi * 5)
    \foreach \x  in {1,...,25}      
            \draw[->] (one\x) -- (two\x);
\end{tikzpicture}

\end{document}

In the above stated example is an error where the name range from two1 to two25 is unknown. How can i rewrite this line:

\draw[thin] (0,0) ++(-\x, -\y) rectangle ++(0.2, 0.2) coordinate [pos=.5] (two);

so that names are set from two1 to two25. I would like to calculate the names based on

\ai + (\bi * 5)
11

Use \pgfmathtruncatemacro to calculate the value and truncate it (1.01).

Code

\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}

    \foreach \x [count=\xi from 1] in {-3.0,-2.8,...,1.8}
        \draw[thin] (0,0) ++(\x,2) rectangle ++(0.2, 0.2) coordinate [pos=.5] (one\xi);

    \foreach \x [count=\ai from 1] in {1,0.8,...,0.2}
        \foreach \y [count=\bi from 0] in {1,0.8,...,0.2}
            \pgfmathtruncatemacro\yi{\ai + 5*\bi} % <-- here we go! ------ and there: --- vvv
            \draw[thin] (0,0) ++(-\x, -\y) rectangle ++(0.2, 0.2) coordinate [pos=.5] (two\yi);
    \foreach \x  in {1,...,25}      
            \draw[->] (one\x) -- (two\x);
\end{tikzpicture}
\end{document}

Output

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.