以下=节点行为奇怪

以下=节点行为奇怪

我希望在前一行的正下方绘制节点线,但下面的例子看起来很奇怪。

\documentclass[convert={outfile=\jobname.png}]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows,chains,positioning,scopes,quotes,patterns}
\begin{document}
\begin{tikzpicture}[
    >=stealth',
    node distance=0 and 0,
    block/.style={draw,on chain,minimum size=1.4em},
]

{[start chain=C1]
 %Y
   \node[block] (N0) {Y};
   \foreach \i in {1,...,39} {
       \pgfmathparse{Mod(\i,10)==0?1:0}
       \ifnum\pgfmathresult>0
           \pgfmathsetlengthmacro\j{\i-10}
           \node[block,below=of N\j] (N\i) {Y};
       \else
           \pgfmathparse{Mod(\i,10)>7?1:0}
            \ifnum\pgfmathresult>0
               \node[block,pattern=north west lines] (N\i) { };
           \else
               \node[block] (N\i) {Y};
           \fi
       \fi
   }
   % U
   \node[block,below=of N31] (N41) {U};
   \foreach \i in {42,...,50} {
       \ifnum\i>48
            \node[block,pattern=north west lines] (N\i) { };
       \else
           \pgfmathparse{Mod(\i,2)==0?1:0}
           \ifnum\pgfmathresult>0
               \node[block] (N\i) {};
           \else
               \node[block] (N\i) {U};
           \fi
       \fi
   }
   \node[block,below=of N41] (N51) {U};
   \foreach \i in {52,...,60} {
       \ifnum\i>58
            \node[block,pattern=north west lines] (N\i) { };
       \else
           \pgfmathparse{Mod(\i,2)==0?1:0}
           \ifnum\pgfmathresult>0
               \node[block] (N\i) {};
           \else
               \node[block] (N\i) {U};
           \fi
       \fi
   }
   %V
 \node[block,below=of N51] (N61) {U};
   \foreach \i in {62,...,70} {
       \ifnum\i>68
            \node[block,pattern=north west lines] (N\i) { };
       \else
           \pgfmathparse{Mod(\i,2)==0?1:0}
           \ifnum\pgfmathresult>0
               \node[block] (N\i) {};
           \else
               \node[block] (N\i) {V};
           \fi
       \fi
   }
   \node[block,below=of N61] (N71) {U};
   \foreach \i in {72,...,80} {
       \ifnum\i>78
            \node[block,pattern=north west lines] (N\i) { };
       \else
           \pgfmathparse{Mod(\i,2)==0?1:0}
           \ifnum\pgfmathresult>0
               \node[block] (N\i) {};
           \else
               \node[block] (N\i) {V};
           \fi
       \fi
   }
}

\end{tikzpicture}
\end{document}

输出: 在此处输入图片描述

U、V 线看起来不错,但 Y 线不如预期。

答案1

使用\pgfmathtruncatemacro\j{\i-10}而不是\pgfmathsetlengthmacro\j{\i-10}。前者将返回一个十进制数,因此below=of N\j变为例如below=of N1.0,而 则.0读作锚点。

\pgfmathtruncatemacro给出整数值。

代码输出

\documentclass[border=5mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows,chains,positioning,scopes,quotes,patterns}
\begin{document}
\begin{tikzpicture}[
    >=stealth',
    node distance=0 and 0,
    block/.style={draw,on chain,minimum size=1.4em},
]

{[start chain=C1]
 %Y
   \node[block] (N0) {Y};
   \foreach \i in {1,...,39} {
       \pgfmathparse{Mod(\i,10)==0?1:0}
       \ifnum\pgfmathresult>0
           \pgfmathtruncatemacro\j{\i-10}
           \node[block,below=of N\j] (N\i) {Y};
       \else
           \pgfmathparse{Mod(\i,10)>7?1:0}
            \ifnum\pgfmathresult>0
               \node[block,pattern=north west lines] (N\i) { };
           \else
               \node[block] (N\i) {Y};
           \fi
       \fi
   }
   % U
   \node[block,below=of N30] (N41) {U};
   \foreach \i in {42,...,50} {
       \ifnum\i>48
            \node[block,pattern=north west lines] (N\i) { };
       \else
           \pgfmathparse{Mod(\i,2)==0?1:0}
           \ifnum\pgfmathresult>0
               \node[block] (N\i) {};
           \else
               \node[block] (N\i) {U};
           \fi
       \fi
   }
   \node[block,below=of N41] (N51) {U};
   \foreach \i in {52,...,60} {
       \ifnum\i>58
            \node[block,pattern=north west lines] (N\i) { };
       \else
           \pgfmathparse{Mod(\i,2)==0?1:0}
           \ifnum\pgfmathresult>0
               \node[block] (N\i) {};
           \else
               \node[block] (N\i) {U};
           \fi
       \fi
   }
   %V
 \node[block,below=of N51] (N61) {U};
   \foreach \i in {62,...,70} {
       \ifnum\i>68
            \node[block,pattern=north west lines] (N\i) { };
       \else
           \pgfmathparse{Mod(\i,2)==0?1:0}
           \ifnum\pgfmathresult>0
               \node[block] (N\i) {};
           \else
               \node[block] (N\i) {V};
           \fi
       \fi
   }
   \node[block,below=of N61] (N71) {U};
   \foreach \i in {72,...,80} {
       \ifnum\i>78
            \node[block,pattern=north west lines] (N\i) { };
       \else
           \pgfmathparse{Mod(\i,2)==0?1:0}
           \ifnum\pgfmathresult>0
               \node[block] (N\i) {};
           \else
               \node[block] (N\i) {V};
           \fi
       \fi
   }
}

\end{tikzpicture}
\end{document}

相关内容