Accessing node name within node text braces

I am wishing for writing $\n$ as node text, where \n has the node's name.

Working example:

\documentclass{standalone}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
[node distance=2em]
\node (0) {0};
\node (h) [above of=0] {};
\node (a) [left of=h] {$a$};
\node (b) [right of=h] {$b$};
\draw (0) -- (a);
\draw (0) -- (b);
\end{tikzpicture}
\end{document}


Ideally, I hope to write the description of node a as

\node (a) [left of=h] {$\n$};


so that I can forget about the text (but still have them in math mode).

• What version of TikZ are you using? Jun 17, 2014 at 18:22
• Seems to be 3.0 (I am using texlive 2013). Jun 17, 2014 at 19:12
• In that case, I was going to suggest essentially what Ignasi wrote. Jun 17, 2014 at 19:18

With TikZ 3.0 you can use node contents and name options:

\documentclass{standalone}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}[node distance=2em,
mathnode/.style={name=#1, node contents=$#1$}]
\node (0) {0};
\node (h) [above of=0] {};
\node [mathnode=a, left of=h];
\node [mathnode=b, right of=h];
\draw (0) -- (a);
\draw (0) -- (b);
\end{tikzpicture}
\end{document}


Note: If you don't need it later on, you can avoid node h:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}

\begin{document}
\begin{tikzpicture}
[node distance=2em,
mathnode/.style={name=#1, node contents=$#1$}]
\node (0) {0};
\node [mathnode=a, above left= of 0];
\node [mathnode=b, above right = of 0];
\draw (0) -- (a);
\draw (0) -- (b);
\end{tikzpicture}
\end{document}