tikz - 步数减1不起作用

tikz - 步数减1不起作用

我希望步数为 -1 但看起来它不起作用!

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

如果步数减少 1,H、I、J 应该下降!

在此处输入图片描述

答案1

\stepcounter没有第二个参数。可能您正在寻找\addtocounter(或\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}

在此处输入图片描述

答案2

您可以使用以下 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}

答案3

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

在此处输入图片描述

相关内容