1

I'd like to place a node below a \graph in a tikzpicture. But, for some reason, I can't access the \graph.

MWE:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning,graphs}
\begin{document}
    \begin{tikzpicture}
    \graph [name=graph] {a -> b};
    \node[below of=graph] {$t_1$};
    \end{tikzpicture}
\end{document}

results in: Package pgf Error: No shape named graph is known. ^^I\node[below of=graph] {$t_1$};

\graph (name) {...}; leads to File ended while scanning use of \tikz@lib@graph@node@naked.

\graph {a -> b} node[below] {$t_1$}; works but the label $t_1$ appears below the node a. Can I shift it to the centre?

(I'd prefer to use the graphs library and not graphdrawing, since the latter requires LuaLaTeX)

  • 1
    There is also (current bounding box) to position nodes relative to. – John Kormylo Feb 26 at 14:11
  • @JohnKormylo of course! \node[below of=current bounding box] {$t_1$}; works perfectly. Would you like to turn that into an answer so I can accept it? – Felix Emanuel Feb 26 at 14:57
  • Nah. Don't really need the points. AFAICT, there are no more superpowers to be gained. – John Kormylo Feb 26 at 16:33
4

You can use a local bounding box.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning,graphs}
\begin{document}
    \begin{tikzpicture}
    \begin{scope}[local bounding box=graph]
     \graph  {a -> b};
    \end{scope} 
    \node[below=1ex of graph] {$t_1$};
    \end{tikzpicture}
\end{document}

enter image description here

|improve this answer|||||
2

I don't know if you can consider the graph as a whole like a big node (edit: Schrödinger's cat's answer does exactly that), but you can certainly name individual nodes of the graph and reference them in the rest of the tikzpicture:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc, graphs, positioning}

\begin{document}

\begin{tikzpicture}
\graph {a[name=a] -> b[name=b]};
\coordinate (m) at ($(a)!0.5!(b)$); % midpoint between a and b
\node[below=0.5cm of m] {$t_1$};
\end{tikzpicture}

\end{document}

enter image description here

Note the use of below=0.5cm of m instead of the syntax below of=..., which doesn't make use of the positioning TikZ library.

There are other possibilities to tell that some particular node of your graph should be anchored at some particular position of your tikzpicture: see anchor node=, anchor here and desired at= in section Anchoring a Graph of the TikZ & PGF manual, around page 425.

Other ways to do the same:

\graph {a[name=a] -> b[name=b]};
\path (a) -- (b) node[pos=0.5, below=0.5cm] {$t_1$};

and

\graph {a[name=a] -> b[name=b]};
\path (a) -- node[pos=0.5, below=0.5cm] {$t_1$} (b);

The TikZ calc library is not needed for these last two techniques.

|improve this answer|||||

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.