3

I have a graph defined as follows. The real thing has hundreds of nodes, this is just an example. How can I add a label (rotated x degrees) on top of a node?

\documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary{backgrounds}
\usetikzlibrary{positioning}
\usetikzlibrary{graphs}
\usetikzlibrary{shapes.arrows}

\begin{document}
\begin{frame}
\frametitle{Title}
\framesubtitle{Subtitle}

\tikz [>=stealth, black!50, text=black, very thin,
    every new ->/.style = {shorten >=1pt},
    graphs/every graph/.style = {edges=rounded corners},
    hv path/.style = {to path={-| (\tikztotarget)}},
    vh path/.style = {to path={|- (\tikztotarget)}},
    t/.style = {
      circle, minimum size=1mm, thin, draw=black!80, scale=0.1, fill=black!80},
    shape = coordinate
    ]
    \graph [grow left sep=1.5mm, branch down=1.0mm, simple] {
        
t0/""[t]
 -- {
  t1/""[t]
   -- {
     t17/""[t],
     t18/""[t]
   }
 };
 };
\end{frame}
\end{document}

enter image description here

7
  • 1
    On topic: you can use label with the graphs nodes, as well, say label={[rotate=90, anchor=west]north:Hello World}. You can of course set every label/.style={rotate=90, anchor=west}, label position=north then that can be shortened to label=Hello World. With the quotes library loaded, you can even use "Hello World" as part of the nodes' options. If you want to use the graph's nodes' name or text instead of Hello World, this can be done with a bit of work, as well. Commented Sep 1, 2023 at 18:55
  • 2
    @Qrrbrbirlbel Does label actually work with this code?
    – Alan Munn
    Commented Sep 1, 2023 at 19:10
  • 1
    @Qrrbrbirlbel, Off-topic #1: I don't understand empty nodes and nodes=t. Can you please give an example with a bit of context around it? Off-topic #2: I tried t0/""[t, label={[rotate=90, anchor=west]north:Hello World}], there is no label in the output.
    – user443854
    Commented Sep 1, 2023 at 19:28
  • 2
    Oh, wait a minute. What's that shape = coordinate doing there? This will turn every node into a coordinate (unless otherwise specified like with circle in t) thereby gobbling the text and any visual output of a node. (And a label ist just another node.) Commented Sep 1, 2023 at 20:15
  • 1
    Removing shape=coordinate does it!
    – user443854
    Commented Sep 1, 2023 at 20:57

1 Answer 1

4

You can use TikZ's own label key to add additional nodes to those that make up the graph.

You can just add these to the options of each node, say

t0/""[t, label={[rotate=90, anchor=west]north:Hello World}]

If all labels will look the same, it makes sense to set the every label key to the needed settings.

You will have to make enough space between the graph's nodes since the graphs library will not take the labels into account. If an automatic approach is needed here, we will need to invest a bit more work.


If the labels content corresponds to the nodes' names (t0, t1, …) or the nodes' text, you can use \tikzgraphnodename and/or \tikzgraphnodetext as part of the label's text.


Since shape = coordinate sets the shape for all nodes unless otherwise specified you will have to either remove it or set rectangle/shape = rectangle explicitly for the labels. I've opted for the former since I don't see the benefit for setting all nodes to coordinates.

Code

\documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary{graphs}
\begin{document}
\begin{frame}
\frametitle{Title}
\framesubtitle{Subtitle}
\tikz[
  black!50, text=black, very thin,
  every new ->/.style = {shorten >=1pt},
  graphs/every graph/.style = {edges=rounded corners},
  hv path/.style = {to path={-| (\tikztotarget)}},
  vh path/.style = {to path={|- (\tikztotarget)}},
  every label/.style={
    rotate=90, label position=north, anchor=west, 
    node font=\footnotesize, inner sep=+.15em,
  },
  t/.style = {
    circle, minimum size=1mm, inner sep=+0pt,
    thin, draw=black!80, fill=black!80},
%  shape = coordinate % don't do this or add rectangle to every label
]\graph[
  grow left sep=1em, branch down sep=1.3em, simple,
  empty nodes, nodes={t, label={\tikzgraphnodetext}},
] {
  0 -- {
    1 -- {
      17,
      18/Hi
    }
  }
};
\end{frame}
\end{document}

Output

enter image description here

You must log in to answer this question.

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