tikz 标签坐标使用来自 foreach 变量的数学表达式

tikz 标签坐标使用来自 foreach 变量的数学表达式
\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}

上述示例中有一个错误,其中名称范围从到two1未知two25。我该如何重写此行:

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

这样名字就从two1two25。我想根据以下条件计算名字

\ai + (\bi * 5)

答案1

使用\pgfmathtruncatemacro计算值并截断它 ( 1.01)。

代码

\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}

输出

在此处输入图片描述

相关内容