Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.
\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)
share|improve this question

1 Answer 1

up vote 10 down vote accepted

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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