4

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? – Loop Space Jun 17 '14 at 18:22
  • Seems to be 3.0 (I am using texlive 2013). – Yuning Jun 17 '14 at 19:12
  • In that case, I was going to suggest essentially what Ignasi wrote. – Loop Space Jun 17 '14 at 19:18
6

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}

enter image description here

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}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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