# Using foreach to add variable number of rows to a bytefield inside a TikZ node?

I'd like to loop over a list of fields to be added to a bytefield, to be shown inside a TikZ node. MWE:

\documentclass[tikz]{standalone}

\usepackage{bytefield}
\usepackage{etoolbox}

\begin{document}

\begin{tikzpicture}
\node {
\begin{bytefield}{8}
\wordbox{1}{Source: A} \\
\makeatletter
\let\@gtempa\@empty
\foreach \f in {1,2,3} { \xappto\@gtempa{\noexpand\wordbox{1}{Field: \f}\noexpand\\}}
\@gtempa
\makeatother
\end{bytefield}
};
\end{tikzpicture}
\end{document}


It almost works, but the first field is indented:

How to get rid of that indentation??

What I REALLY want to do is to loop over a list of nodes with variable number of fields and to construct byte fields dynamically. Something like below (of course, this is simplified, just to get the idea across).

% What I REALLY want to do; just a sketch; this fails
\begin{tikzpicture}

\node [draw] (a) {a};
\node [draw] at (6,0) (b) {b};

\foreach \n/\fields in {a/{1,2,3},b/{4,5}} {
\node [below=of \n] {
\begin{bytefield}{8}
\wordbox{1}{Source: \n} \\
\wordbox{1}{\fields} \\
% This sort of works, but not what I want:
\wordbox{1}{\foreach \f in \fields {\f ; } }
% What I really want, but fails with Missing \endgroup inserted.:
% \foreach \f in \fields {
% \wordbox{1}{\f} \\
% }
\end{bytefield}
};
}

\end{tikzpicture}


Just use the /.list handler.

\documentclass[tikz]{standalone}

\usepackage{bytefield}

\begin{document}

\begin{tikzpicture}[wordbox/.code={\\\wordbox{1}{Field: #1}}]
\node {
\begin{bytefield}{8}
\wordbox{1}{Source: A}
\tikzset{wordbox/.list={1,2,3}}
\end{bytefield}
};
\end{tikzpicture}
\end{document}


• That is great, thanks. But it does not seem to generalise to the following, actual problem:  \begin{tikzpicture}[wordbox/.code={\\\wordbox{1}{Field: #1}}] \node [draw] (a) {a}; \node [draw] at (6,0) (b) {b}; \foreach \n/\fields in {a/{1,2,3},b/{4,5}} { \node [below=of \n] { \begin{bytefield}{8} \wordbox{1}{Source: \n} \tikzset{wordbox/.list=\fields} % the following makes no difference % \tikzset{wordbox/.list={\fields}} \end{bytefield} }; } \end{tikzpicture}  That gives just a single woodbox :-(. Mar 19 at 11:14
• @HolgerKarl In my case, after loading positioning, I do get two wordboxes with that code.
– user293154
Mar 19 at 16:32
• Sure, with positioning (sorry...). I also get two boxes, but I want four on the left, three on the right. I will open another question; this should probably be cleaner. Thanks for your help!! Mar 20 at 8:07