24

Is there a TikZ style attribute for adding default text to a node? For instance, I want to be able to do something like the following:

\begin{tikzpicture}[%
  stuff/.style={%
    draw,
    text=Bla bla
  },
]

\node at (0,0) [stuff] {};
\node at (0,1) [stuff] {};
\node at (0,2) [stuff] {};
\node at (0,3) [stuff] {};
\end{tikzpicture}

to produce a picture containing 4 nodes, each with the text "Bla bla". I want to be able to create nodes with the same text without having to add it manually.

lockstep
  • 250,273
gablin
  • 17,006

2 Answers2

23

It's not exactly what it was designed for, but you can add fixed text to the font parameter:

\documentclass{article}
\usepackage{tikz}
\begin{document}
 \begin{tikzpicture}[%
  stuff/.style={%
    draw,
    font={A}}
]

\node at (0,0) [stuff] {};
\node at (0,1) [stuff] {};
\node at (0,2) [stuff] {};
\node at (0,3) [stuff] {};
\end{tikzpicture}

\end{document}

output of code

Alan Munn
  • 218,180
15

TikZ 3.0 introduced node contents option which replaces {...}

enter image description here

And another example to easily test it

\documentclass{article}
\usepackage{tikz}
\begin{document}
 \begin{tikzpicture}[%
  stuff/.style={%
    draw,
    node contents={A}}
]

\node at (0,0) [stuff];
\node at (0,1) [stuff];
\node at (0,2) [stuff];
\node at (0,3) [stuff, node contents={B}];
\end{tikzpicture}

\end{document}

enter image description here

Ignasi
  • 136,588
  • 1
    The only inconvenience seems to be this way you can't write \node[stuff] at (0,0); (i.e. you really need to place the style declaration at the end). – glopes Jan 10 '16 at 12:22
  • 1
    @glopes: You're right, but if you prefer to indicate coordinates at the end, you can use option at=: \node[stuff, at={(0,0)}]; – Ignasi Jan 10 '16 at 17:29