tikz-在foreach循环中垂直对齐节点

tikz-在foreach循环中垂直对齐节点

我想垂直对齐 foreach 循环中的节点!

\documentclass[border=10pt,convert={outfile=\jobname.png}]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}
\usepackage{fmtcount}
\begin{document}
\begin{tikzpicture}[node distance=0]
\tikzset{box/.style={draw}}
\foreach \i [evaluate=\i as \p using \i-1] in {0,...,5} {
    \pgfmathsetmacro\addr{int(0xbc - \i)}
    \def\hexaddr{0xE056\Hexadecimalnum{\addr}}
    \ifnum\i=0
        \node[box,label={left:\hexaddr}] (N\i) {\i};
    \else
        \node[box,label={left:\hexaddr},below=0cm of N\p] (N\i) {\i};
    \fi
}
\end{tikzpicture}
\end{document}

但现在输出是: 在此处输入图片描述

我想我应该将其改为N\pN\p.south west它无法编译。

答案1

evaluate=\i as \p using \i-1当表达式\i > 0导致0.0, 1.0, 2.0, ..., 4.0并且您的意思是时0, 1, 2, ..., 4,因此您可以选择count=\p from -1或如@percusse 在评论中指出的那样\i as \p using int(\i-1)

\documentclass[border=10pt,convert={outfile=\jobname.png}]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}
\usepackage{fmtcount}
\begin{document}
\begin{tikzpicture}[node distance=0]
\tikzset{box/.style={draw}}
\foreach \i [count=\p from -1] in {0,...,5} {
    \pgfmathsetmacro\addr{int(0xbc - \i)}
    \def\hexaddr{0xE056\Hexadecimalnum{\addr}}
    \ifnum\i=0
        \node[box,label={left:\hexaddr}] (N\i) {\i};
    \else
        \node[box,label={left:\hexaddr},below=0cm of N\p] (N\i) {\i};
    \fi
}
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

您可以按如下方式使用链条:

\documentclass[border=10pt,convert={outfile=\jobname.png}]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}
\usepackage{fmtcount}
\usetikzlibrary{chains,shapes}

\begin{document}
\begin{tikzpicture}[start chain=values going below,node distance=0,outer sep=0]
\tikzset{box/.style={draw}}
\foreach \i [evaluate=\i as \p using \i-1] in {0,...,5} {
    \pgfmathsetmacro\addr{int(0xbc - \i)}
    \def\hexaddr{0xE056\Hexadecimalnum{\addr}}
    \node[box,on chain,label={left:\hexaddr}] (N\i) {\i};
}
\end{tikzpicture}
\end{document}

输出:

在此处输入图片描述

相关内容