6

I wish to draw nodes line exactly below previous line, but below example looks weird.

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

Output: enter image description here

The U,V lines looks good but the Y lines not as expected.

0

1 Answer 1

8

Use \pgfmathtruncatemacro\j{\i-10} instead of \pgfmathsetlengthmacro\j{\i-10}. The former will give you a decimal number, so below=of N\j becomes e.g. below=of N1.0, and the .0 is read as the anchor.

\pgfmathtruncatemacro gives the integer value.

output of code

\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}
1
  • Great, it's hard for me to figure out without your help! Thanks!
    – lucky1928
    Jan 3, 2018 at 16:24

Not the answer you're looking for? Browse other questions tagged .