0

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}

% inspired by : https://tex.stackexchange.com/questions/654504/using-foreach-to-add-rows-containing-references-to-pgfplots-objects-in-a-matrix

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

Idea is based on Using \foreach to add rows containing references to pgfplots objects in a matrix of nodes

It almost works, but the first field is indented:

First field intended??

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}

1 Answer 1

1

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}

enter image description here

3
  • 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 :-(. Commented Mar 19, 2023 at 11:14
  • @HolgerKarl In my case, after loading positioning, I do get two wordboxes with that code.
    – user293154
    Commented Mar 19, 2023 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!! Commented Mar 20, 2023 at 8:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .