1

I want to align nodes in foreach loop vertically!

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

But now the output is: enter image description here

I guess I should change the N\p to N\p.south west but it can not compile.

  • \i as \p using int(\i-1) – percusse Feb 9 '17 at 21:56
1

The expression evaluate=\i as \p using \i-1 when \i > 0 results in 0.0, 1.0, 2.0, ..., 4.0 and you mean 0, 1, 2, ..., 4, so you can choose count=\p from -1 or as noted out by @percusse in a comment \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}

enter image description here

  • hmmm, sounds weird, that means in my code, N1.0 actually does exist, compile should report error then we can know it's not a valid node at all. – lucky1928 Feb 10 '17 at 2:38
2

You can use chain as below:

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

Output:

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.