Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question
    
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
add comment

1 Answer

up vote 6 down vote accepted

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] {};
\chainlabel{chain-1.one north}{head};
\end{tikzpicture}  

\end{document}

enter image description here

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] {};
\chainlabel{chain-1.one north}{head};
\end{tikzpicture}  

\end{document}

enter image description here

share|improve this answer
    
Oh. How stupid of me. I had to stare at the code to figure out the mistake. How could I miss the slash in the command definition! –  recluze Oct 11 '12 at 5:20
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.