I sometimes need to visualize an array-like structure with tikz. I use nodes for array elements and position the next node right of
the previous one, i.e., like this:
\node[draw,rectangle] (n0) {element 1};
\node[draw,rectangle] (n1) [right of=n0] {element 2};
\node[draw,rectangle] (n2) [right of=n1] {element 3};
% many more nodes
In other words: I always position the next node right of
the previous node. I want to simplify that code, e.g., by using a loop, and ideally would like to have something like this:
\foreach \id in {1,...,9}
\draw let \p0 = {\id-1} in node[draw,rectangle] (n\id) {element \id};
There is two issues with this:
-
The clause
{\id-1}
is not "evaluated", but taken as a string, i.e., "1-1", "2-1", etc. How could I do that? Or, if that is not possible or convenient, can I somehow specifyright of = latest defined id
? -
The first element is special in the sense that it is not relative to any other node. Special cases are always tedious, so I'm wondering if there is a way to define
right of = none
? It'd be nice to draw all nodes with the loop and not having to treat the first element as a special case.
Best Answer
Another version of
evaluate
usesremember
.Output
Code