I’m attempting to define a command to build several similar tikz nodes. These nodes differ only by one parameter (#2 below). Here are the main questions I’ve referenced so far:
- Foreach inside a TikZ matrix
- Forcing macro expansion
- Appending to Variables
- How to left-pad an integer with zeroes to make it many digits? for (
\num[...])
I feel like I’m so close, but I have no idea where to go beyond what I’ve detailed below. The left part of this diagram is pretty much the expected result:
Here’s the current command definition:
\documentclass[11pt,a4paper]{article}
\usepackage{siunitx}
\usepackage{tikz}
\usepackage{xparse}
\begin{document}
\tikzstyle{ht_num} = [draw, semithick]
\tikzstyle{ht_hash} = [draw, semithick, minimum width=2em]
\NewDocumentCommand \HashTable { m m } {%
\def\MatrixContent{\empty}
\pgfmathtruncatemacro\nn{2^#2-1}
\foreach \n in {0,...,\nn}{%
\edef\MatrixContent{\unexpanded\expandafter{\MatrixContent}\unexpanded\expandafter{%
\node[ht_hash] (#1-\n) {\num[minimum-integer-digits=#2]{\pgfmathbin{\n}\pgfmathresult}}; \\
}}
}
\matrix [outer sep=0pt,every node/.style={anchor=west}] (#1) {
\node[ht_num] (#1-a) {#2}; \\
\MatrixContent
};
}
\begin{tikzpicture}
\HashTable{table1}{2}
% Other tikz nodes...
\end{tikzpicture}
\end{document}
And finally, the error message:
./Diagrams.tex:398: Missing } inserted.
<inserted text>
}
l.398 ^^I^^I\end{tikzpicture}
\end{center}
Which isn’t very helpful.
I tried removing the code from a command and placing it directly in the document with hardcoded values:
\def\MatrixContent{\empty}
\pgfmathtruncatemacro\nn{2^2-1}
\foreach \n in {0,...,\nn}{%
\edef\MatrixContent{\unexpanded\expandafter{\MatrixContent}\unexpanded\expandafter{%
\node[ht_hash] (table-\n) {\n};
}}
}
\begin{tikzpicture}
\matrix [outer sep=0pt,every node/.style={anchor=west}] (table) {
\MatrixContent
};
\end{tikzpicture}
Which produced a new error:
./Diagrams.tex:403: Undefined control sequence.
\pgffor@body ... }\unexpanded \expandafter {\node
[ht_hash] (table-\n ) {...
l.403 ^^I^^I}
And finally, I tried using a different method for building \MatrixContent:
\let\MatrixContent\empty
\pgfmathtruncatemacro\nn{2^2-1}
\foreach \n in {0,...,\nn}{%
\expandafter\gappto\expandafter\MatrixContent\expandafter{%
\node[ht_hash] (table-\n) {\n};
}
}
\begin{tikzpicture} ...
Producing yet another error:
./Diagrams.tex:413: Undefined control sequence.
\pgffor@body ...MatrixContent \expandafter {\node
[ht_hash] (table-\n ) {...
l.413 ^^I^^I}



ht_numandht_hashdo. Please provide us with a complete yet minimal document that starts with\documentclassand ends with\end{document}. – Nov 11 '19 at 18:48\\after\MatrixContent, i.e.\matrix [outer sep=0pt,every node/.style={anchor=west}] (#1) { \node[ht_num] (#1-a) {#2}; \\ \MatrixContent \\ };. (It is true, though, that the error message does not immediately tell you that.) – Nov 11 '19 at 18:49\\allows the document to compile okay, but it doesn’t appear to output anything in place of\MatrixContent– MTCoster Nov 11 '19 at 19:03\edefand\foreachstarts a group, so the definitions are local and get forgotten. (Replacing\edefby\xdefyields an error, though, and I need still to figure out what you wish to do.) BTW, which package does\num[minimum-integer-digits=#2]come from? – Nov 11 '19 at 19:21siunitx, updated to include in mcve & link to source – MTCoster Nov 11 '19 at 19:29