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.

4
  • I once looking into something like that. I can't remember the final outcome just now, but it wasn't very positive. This requires some serious hacking of the TikZ internals. Nov 13, 2011 at 19:53
  • As a workaround, you could perhaps use a loop to make the nodes, iterating over coordinates. Nov 13, 2011 at 20:02
  • @TorbjørnT.: Yes, that would be the most reasonable solution in most instances, but I've encountered numerous situations where that's not possible because the nodes are not placed in areas where a \for can be used.
    – gablin
    Nov 13, 2011 at 20:53
  • For yet another alternative solution see Default node text with fill color
    – percusse
    Mar 22, 2012 at 11:45

2 Answers 2

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

4
  • 9
    If there were a "sneaky" badge, this would be my candidate for it. Nov 13, 2011 at 21:53
  • Also see tikzstyle default node text with fill color which provides a detailed explanation of the "hooks" available. Mar 22, 2012 at 15:17
  • Do you try align=right,font={$x+2$\\very bad idea }? Mar 22, 2012 at 22:48
  • @Altermundus Well, it's already a trick, so expecting to put a \\ inside is asking for trouble. :)
    – Alan Munn
    Mar 23, 2012 at 0:34
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

2
  • 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, 2016 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, 2016 at 17:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .