我想垂直对齐 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\p
但N\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}
输出: