使用 double foreach 时设置行起始位置不起作用

使用 double foreach 时设置行起始位置不起作用

我正在用 Tikz 绘制一个漂亮的图表。以下操作完美运行:

\foreach \m [
evaluate=\m as \nstart using ((\m - 1) * 4) + 1,
evaluate=\m as \nstep using ((\m - 1) * 4) + 2,
evaluate=\m as \nend using ((\m - 1)* 4) + 5] in {1,2,...,5}
\foreach \i in {\nstart,\nstep,...,\nend}

\draw (input-\i) -- (hidden-1-\m);

现在,我想让该行从顶部开始input-\i,因此我使用:

\draw (input-\i.north) -- (hidden-1-\m);

然而,这以 终止Package PGF Math Error: Unknown operator ``n' or ``no' (in '0.north'). \draw (input-\i.north) -- (hidden-1-\m);

我该如何修复这个错误或者解决它?

这是一个重现错误的最小示例:

\documentclass{beamer}
\usepackage{pgfplots}   
\tikzset{%
    tdnn neuron/.style={
        rectangle,
        draw,
        minimum height=0.5cm,
        minimum width=0.1cm 
    },
}
\begin{document}
\begin{minipage}{\linewidth}
    \begin{tikzpicture}[x=1.5cm, y=1.5cm, >=stealth]
    % Layers
    \foreach \m [count=\y] in {1,2,...,21}
    \node [tdnn neuron] (input-\m) at (0-\y*0.25,0) {};

    \foreach \m [count=\y] in {1,2,...,5}
    \node [tdnn neuron] (hidden-1-\m) at (0-\y*1 + 0.25,1) {};

    \foreach \m [count=\y] in {1}
    \node [tdnn neuron] (classify-\m) at (0-\y*0.25 - 10 * 0.25,2) {};

    %Edges
    \foreach \m [
    evaluate=\m as \nstart using ((\m - 1) * 4) + 1,
    evaluate=\m as \nstep using ((\m - 1) * 4) + 2,
    evaluate=\m as \nend using ((\m - 1)* 4) + 5] in {1,2,...,5}
    \foreach \i in {\nstart,\nstep,...,\nend}
    \draw (input-\i.north) -- (hidden-1-\m); % Error happens here. 


    \foreach \m in {1,2,...,5}
    \draw (hidden-1-\m) -- (classify-1);

    \end{tikzpicture}
\end{minipage}
\end{document}

答案1

要获取坐标名称中的整数部分,可以使用\pgfmathparse{int(\i)}

\documentclass{beamer}
\usepackage{pgfplots}   
\tikzset{%
    tdnn neuron/.style={
        rectangle,
        draw,
        minimum height=0.5cm,
        minimum width=0.1cm 
    },
}
\begin{document}
\begin{frame}
    \begin{tikzpicture}[x=1.5cm, y=1.5cm, >=stealth]
    % Layers
    \foreach \m [count=\y] in {1,2,...,21}
    \node [tdnn neuron] (input-\m) at (0-\y*0.25,0) {};

    \foreach \m [count=\y] in {1,2,...,5}
    \node [tdnn neuron] (hidden-1-\m) at (0-\y*1 + 0.25,1) {};

    \foreach \m [count=\y] in {1}
    \node [tdnn neuron] (classify-\m) at (0-\y*0.25 - 10 * 0.25,2) {};

    %Edges
    \foreach \m [
    evaluate=\m as \nstart using ((\m - 1) * 4) + 1,
    evaluate=\m as \nstep using ((\m - 1) * 4) + 2,
    evaluate=\m as \nend using ((\m - 1)* 4) + 5] in {1,2,...,5}
    \foreach \i in {\nstart,\nstep,...,\nend}{
    \pgfmathparse{int(\i)}
    \draw (input-\pgfmathresult.north) -- (hidden-1-\m); % Error happens here. 
        }

    \foreach \m in {1,2,...,5}{
        \draw (hidden-1-\m) -- (classify-1);
        }
    \end{tikzpicture}
\end{frame}
\end{document}

答案2

根据@samcarter的评论,答案是:

我的evaluate表达式得出一个十进制数。因此,Tikz 分别查找input-1.0input-1.0.north。第一种情况有效,第二种情况无效。

解决方案是将表达式转换为整数:

\foreach \m [
evaluate=\m as \nstart using int(((\m - 1) * 4) + 1),
evaluate=\m as \nstep using int(((\m - 1) * 4) + 2),
evaluate=\m as \nend using int(((\m - 1)* 4) + 5)] in {1,2,...,5}
\foreach \i in {\nstart,\nstep,...,\nend}
\draw (input-\i.north) -- (hidden-1-\m); 

相关内容