# Creating TikZ Node Using Macro

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] {};

\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.

-
Related: tex.stackexchange.com/a/19288. – Gonzalo Medina Oct 11 '12 at 5:00
Thakns for the link but the linked list isn't the problem here. That I've got working cleanly. The issue is with the label through the command. – recluze Oct 11 '12 at 5:15

You had some errors in the definition of your command: you forgot the initial backslash (it's \chainlabel instead of chainlabel) and, since you used \path #1 |- ... the first argument should have parentheses, unless you used \path (#1) |- ..., as I did in my example below:

\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] {};
\end{tikzpicture}

\end{document}


Perhaps you can add some minimum height to the rectangle split so even if empty they will have an uniform size:

\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,minimum height=14pt}, 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] {};