![使用 foreach 向 TikZ 节点内的字节字段添加可变数量的行?](https://linux22.com/image/462517/%E4%BD%BF%E7%94%A8%20foreach%20%E5%90%91%20TikZ%20%E8%8A%82%E7%82%B9%E5%86%85%E7%9A%84%E5%AD%97%E8%8A%82%E5%AD%97%E6%AE%B5%E6%B7%BB%E5%8A%A0%E5%8F%AF%E5%8F%98%E6%95%B0%E9%87%8F%E7%9A%84%E8%A1%8C%EF%BC%9F.png)
我想循环遍历要添加到字节字段的字段列表,以显示在 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}