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}

Dai Bowen
  • 6,117

1 Answers1

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

  • 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 :-(.

    – Holger Karl Mar 19 '23 at 11:14
  • @HolgerKarl In my case, after loading positioning, I do get two wordboxes with that code. –  Mar 19 '23 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!! – Holger Karl Mar 20 '23 at 08:07