2

I expect stepcount by -1 but looks like it not work that way!

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{chains}
\begin{document}
\begin{tikzpicture}[
    start chain,
    node distance=0 and 0]
  \pgfmathsetmacro{\row}{4}
  \newcounter{r}\setcounter{r}{0}
  \newcounter{dir}\setcounter{dir}{0}
  \foreach \n[count=\i] in {A,B,C,D,E,F,G,H,I,J} {
    \ifnum\thedir=0
        \node[on chain=going below,draw] (P\i) {\n};
        \stepcounter{r}{1}
        \ifnum\ther=\row-1
            \setcounter{dir}{1}
            \stepcounter{r}{-1}
        \fi
    \else
        \node[on chain=going above right,draw] (P\i) {\n};
        \stepcounter{r}{-1}
        \ifnum\ther=1
            \setcounter{dir}{0}
            \stepcounter{r}{-1}
        \fi        
    \fi
  }
\end{tikzpicture}  
\end{document}

H,I,J should going down if stepcount by -1!

enter image description here

1
  • Use \addtocounter instead of \stepcounter.
    – user240002
    Apr 18 at 4:26
3

\stepcounter does not have a second argument. Possibly you are looking for \addtocounter (or \setcounter).

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{chains}
\begin{document}
\begin{tikzpicture}[
    start chain,
    node distance=0 and 0,
    nodes={outer sep=0pt,minimum size=1.5em}]
  \pgfmathtruncatemacro{\row}{4}
  \newcounter{r}\setcounter{r}{0}
  \newcounter{dir}\setcounter{dir}{0}
  \foreach \n[count=\i] in {A,B,C,D,E,F,G,H,I,J} {
    %\typeout{\n,\i,\number\value{dir},\number\value{r}}
    \ifnum\value{dir}=0
        \node[on chain=going below,draw] (P\i) {\n};
        \addtocounter{r}{1}
        \ifnum\value{r}=\row
            \setcounter{dir}{1}
            \addtocounter{r}{-1}
        \fi
    \else
        \node[on chain=going above right,draw] (P\i) {\n};
        \addtocounter{r}{-1}
        \ifnum\value{r}=1
            \setcounter{dir}{0}
            \addtocounter{r}{-1}
        \fi        
    \fi
  }
\end{tikzpicture}  
\end{document}

enter image description here

1

Your image you can reproduce with the following MWE:

\documentclass[border=3mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{chains,
                positioning}

\begin{document}
    \begin{tikzpicture}[
node distance = 0mm,
  start chain,
    N/.style = {draw, minimum size=1.5em, outer sep=0pt}
                       ]
\foreach \i [count=\j] in {A,B,C,D}
{
\node (n\j) [N,on chain=going below] {\i};
}

\node[N,right=of n3, =going above right] {E};
\foreach \i in {E, F,G,H,I,J}
{
\node[N,on chain=going above right] {\i};
}
     \end{tikzpicture}
\end{document}
0
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{chains}
\begin{document}
\begin{tikzpicture}[
    start chain,
    box/.style={draw,inner sep=0,minimum width=1em,minimum height=1em},
    node distance=-\pgflinewidth and -\pgflinewidth]
  \pgfmathsetmacro{\row}{3}
  \pgfmathsetmacro{\rowp}{\row-1}
  \newcounter{r}\setcounter{r}{0}
  \newcounter{dir}\setcounter{dir}{0}
  \foreach \n[count=\i] in {A,B,C,D,E,F,G,H,I,J} {
    \ifnum\thedir=0
        \node[box,on chain=going below] (P\i) {\n};
        \addtocounter{r}{1}
        \ifnum\ther=\row
            \addtocounter{r}{-2}
            \setcounter{dir}{1}% from row - 2
        \fi
    \else
        \node[box,on chain=going above right] (P\i) {\n};
        \addtocounter{r}{-1}
        \ifnum\ther=-1
            \setcounter{dir}{0}
            \addtocounter{r}{2} % from 1
        \fi
    \fi
  }
\end{tikzpicture}  
\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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