3

Tree diagram I would like to understand how to add labels to edges and vertices and how to make an invisible vertex such that an edge can start or end in it, but the vertex itself is not visible. I'm interested only in ascending trees of this type, but i couldn't find any similar question or any specific documentation about it. I'd rather understand how to improve my (poor) code instead of changing it at all if it's possible.

CODE:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{graphs}
\begin{document}
\begin{tikzpicture}
\graph [chain shift=(45:1), branch left, nodes={inner sep=0pt, minimum size=2.5pt, circle, fill, draw}, empty nodes]
{a -- b -- {c,d,e -- {f,g}}
};
\end{tikzpicture}
\end{document}

Any help would be appreciated.

0
4

Figuring out how to do stuff with the graph syntax is really just a question of reading the relevant part of the manual and/or looking at examples. If it is a tree, I would use Forest since you are not using the automatic layout algorithms anyway. However, here's an example based on your code which uses the quotes library to label the edges and the standard label option for the vertices. coordinate is used to override the default node keys you've considered for a single node so that it isn't drawn.

\documentclass[border=10pt,multi,tikz]{standalone}
\usetikzlibrary{graphs,quotes}
\begin{document}
\begin{tikzpicture}
  \graph [chain shift=(45:1), branch left, nodes={inner sep=0pt, minimum size=2.5pt, circle, fill, draw}, empty nodes, every edge quotes/.append style={font=\scriptsize, inner sep=0pt}]
  {%
    a -- ["$a$"'] b[shape=coordinate] -- {c[label=0:$w$, >"$c$"'],d[label=0:$v$],e[label=180:$u$, >"$b$"] -- {f,g}}
  };
\end{tikzpicture}
\end{document}

tree in graph syntax

EDIT

Note that the way the nodes are placed depends on the placement strategy in use. chain shift shifts nodes at new levels. 45:1 means the node is place at an angle of 45 and distance of 1 from the current node. branch left means that branches always go left.

You can

  1. place nodes manually (you specify exactly where to put each node);
  2. use an online placement strategy, like the one you've tried (you specify a strategy which takes account of the graph-so-far but not the graph-yet-to-come);
  3. use an offline placement strategy, which requires the advanced facilities supported by LuaTeX (you specify a strategy which takes account of the entire graph).

Sticking to the current strategy type, you can, for example, change it to grow up, branch left:

a different strategy

Notice that the graph is specified in the same way - only one of the specified strategies (what to do for a new level of nodes, as opposed to a new branch) changes.

\begin{tikzpicture}
  \graph [grow up, branch left, nodes={inner sep=0pt, minimum size=2.5pt, circle, fill, draw}, empty nodes, every edge quotes/.append style={font=\scriptsize, inner sep=0pt}]
  {%
    a -- ["$a$"'] b[shape=coordinate] -- {c[label=0:$w$, >"$c$"'],d[label=0:$v$],e[label=180:$u$, >"$b$"] -- {f,g}}
  };
\end{tikzpicture}

If you want something more like this:

tree layout

then you might want to consider using the graphdrawing algorithms library with the trees graph-drawing library. This is an offline placement strategy requiring LuaTeX for compilation.

\documentclass[border=10pt,multi,tikz]{standalone}
\usepackage{luatex85}
\usetikzlibrary{graphs,quotes,graphdrawing}
\usegdlibrary{trees}
\begin{document}
\begin{tikzpicture}
  \graph [tree layout, grow=up, nodes={inner sep=0pt, minimum size=2.5pt, circle, fill, draw}, empty nodes, every edge quotes/.append style={font=\scriptsize, inner sep=0pt}]
  {%
    a -- ["$a$"'] b -- {c[label=0:$w$, >"$c$"'],d[label=0:$v$],e[label=180:$u$, >"$b$"] -- {f,g}}
  };
\end{tikzpicture}
\end{document}

If LuaTeX is not an option, for some reason, then you could always try something like Forest. (Forest can easily do this, as I mentioned above. It is probably the most powerful and flexible package for drawing trees, although it obviously does not support the other kinds of graphs the LuaTeX algorithms do.)

5
  • Thank you very much! It's exactly what i was looking for! If i want to make the root edge vertical, how can i do it? – I'm D. Aug 12 '16 at 12:15
  • Well, the chain shift is setting it at an angle of 45, so if you don't want that, you shouldn't ask for it ;). – cfr Aug 12 '16 at 15:42
  • Unfortunately i do not understand the options that i used for $\graph[]$, because i copied them from an example that i found while looking for a solution. I set $chain shift=(90:1)$ but then the resulting tree was too shifted on the left. Then i tried to delete the option $branch left$ but it got worse: it all collapsed to a vertical line.. – I'm D. Aug 12 '16 at 16:51
  • @Davide Please see edit. – cfr Aug 12 '16 at 21:59
  • How can I use LuaTex with Overleaf or TeXstudio? Sorry but I'm new to LaTeX.. – I'm D. Aug 13 '16 at 10:25
3

As a lark, I did this with a stack. But I don't recommend it for general use.

\documentclass{article}
\usepackage{stackengine,graphicx}
\newcommand\undertheline[5][1in]{\raisebox{.5\baselineskip}{%
  \rotatebox{#2}{\smash{\stackon[\dimexpr-5pt#3.9\baselineskip]{\rule{#1}{.5pt}}{%
    \rotatebox{-\numexpr#2}{$#4$}}\ifx\relax#5\relax\else\,\raisebox{-.15\baselineskip}{%
      \llap{\rotatebox[origin=right]{-\numexpr#2}{$#5\,\,\,$}}$\bullet$}\fi%
}}}}
\begin{document}
\savestack{\levelA}{\makebox[-.5\baselineskip]{%
  \undertheline{135}{+}{c}{}\llap{$v\,$}$\bullet$\undertheline{45}{-}{f}{}}}
\stackengine{0pt}{\levelA}{\undertheline{135}{+}{b}{}%
  \stackengine{3pt}{$\bullet$\smash{\brlap[8pt]{\,$r$}}}{%
    \undertheline[.5in]{90}{+}{a}{}}{U}{c}{F}{T}{S}%
  \stackengine{-43pt}{\undertheline{45}{+}{c}{}}{%
    \,\undertheline[1.5in]{20}{-}{d}{w}}{U}{l}{F}{T}{S}}
  {U}{l}{F}{F}{S}
\end{document}

enter image description here

Just to put the provided macro in perspective, the invocation of \undertheline[1.5in]{55}{+}{X}{Z} will produce this:

enter image description here

The optional argument #1 is the line length, #2 is the rotation angle, #3 defines whether the label is CCW from (+) or CW from (-) the line, #4 is the line label, and #5, if present, is the label for an optional bullet.

2
  • 2
    I'm tempted to ask a question, 'is there anything you can't do with a stack?' but perhaps it is too user-local to be of general interest ... :-) – cfr Aug 12 '16 at 20:56
  • 1
    @cfr This one was a real stretch, not at all user friendly. But it is hard to resist the challenge. – Steven B. Segletes Aug 12 '16 at 21:02

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.