I'm trying to create a linked list using TikZ. I currently have this:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{chains,shapes}
\newcommand{chainlabel}[2]{\path [<-, draw, shorten >=10pt] #1 |- node [at end] {#2} ++(-1,1);}
\begin{document}
\begin{tikzpicture}[every node/.style={rectangle split, rectangle split parts=2, rectangle split horizontal}, node distance=1em, start chain,
every join/.style={->, shorten <=-4.5pt}]
\node[draw, on chain, join] { 1 };
\node[draw, on chain, join] { 7 };
\node[draw, on chain, join] { 5 };
\node[draw, on chain, join] { 2 };
\node[draw, on chain, join] {};
\chainlabel{chain-1.one north}{head};
\end{tikzpicture}
\end{document}
The problem is with the chainlabel
command. I'm getting all kinds of errors about missing control sequences. I have read somewhere that there needs to be some sort of expandafter
somewhere but I'm not sure how to work with it.
p.s. The idea behind chainlabel is that we can pass a coordinate to the command and a label and it will label the node with the provided text.
Best Answer
You had some errors in the definition of your command: you forgot the initial backslash (it's
\chainlabel
instead ofchainlabel
) and, since you used\path #1 |- ...
the first argument should have parentheses, unless you used\path (#1) |- ...
, as I did in my example below:Perhaps you can add some
minimum height
to therectangle split
so even if empty they will have an uniform size: