我想循环遍历要添加到字节字段的字段列表,以显示在 TikZ 节点内。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}
这个想法基于使用 \foreach 在节点矩阵中添加包含对 pgfplots 对象的引用的行
它几乎可以工作,但第一个字段缩进:
如何消除那个凹痕?
我真正想要做的是循环遍历具有可变字段数量的节点列表并动态构造字节字段。如下所示(当然,这是简化的,只是为了表达我的想法)。
% 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}