12

I want to draw a tree and not draw specific edges. I try it like this

\documentclass[preview]{standalone}
\usepackage{tikz} 
\begin{document}
   \begin{tikzpicture}[every node/.style={circle,draw}]
      \node (r) {$r$}
      child { node (T1) {$T_1$} }
      child { edge from parent[draw=none] node[draw=none] (ellipsis) {$\ldots$} }
      child { node (Tn) {$T_n$} };
   \end{tikzpicture}
\end{document}

This does drop the edge of the middle child, but for some reason shifts the node upwards. I want it to be placed at the same level as its siblings. Is this doable without manually drawing a node between (T1) and (Tn)?

enter image description here

3 Answers 3

12

Edit: After clarifying, what is the problem, I suggest to try the following:

\documentclass[border=3mm,tikz,preview]{standalone}

\begin{document}
   \begin{tikzpicture}[every node/.style={circle,draw}]
      \node (r) {$r$}
      child { node (T1) {$T_1$} }
      child {node {$\ldots$}  edge from parent[draw=none]}
      child { node (Tn) {$T_n$} };
   \end{tikzpicture}
\end{document}

enter image description here

Edit (2): Thank to @cfr, which pointed me to simple solution: your line

child { edge from parent[draw=none] node[draw=none] (ellipsis) {$\ldots$} }

reorder to

 child {node[draw=none] {$\ldots$} edge from parent[draw=none]}

and you will get desired results. Deep explanation you can find in cfr answer. It is very instructive.

6
  • Well, the child simply disappears. I want the \ldots to be in the child's position without having to manually place a node between the two siblings.
    – oarfish
    Nov 27, 2015 at 12:59
  • I was guessing this ... however about this your question isn't clear. As you can see, meanwhile I already add this option to my answer.
    – Zarko
    Nov 27, 2015 at 13:08
  • I could do it like this, but I'm interested in whether I can actually have the child be placed correctly, but without an edge from parent. So I would like to avoid having to manually \draw a node or line. I have edited the question to make it more clear.
    – oarfish
    Nov 27, 2015 at 13:19
  • @oarfish, ok, now I see, what you like to achieve. This is simple and your solution is on the right way ... See edit of my answer.
    – Zarko
    Nov 27, 2015 at 13:39
  • Ah yes, this works. It feels like cheating though. I would have to adjust it for a different background colour.
    – oarfish
    Nov 27, 2015 at 14:03
8

I present two solutions. The first uses the standard TikZ tree syntax. The second uses the powerful tree-drawing package, forest.


TikZ

The problem has nothing to do with the fact that you are not drawing the edge. The problem is that you cannot modify the edge from parent style in that way. edge from parent is both and edge drawing operation and a style. The code in the question confuses the two, but this is easier to explain with examples.

To see this, just change the line

  child { edge from parent[draw=none] node[draw=none] (ellipsis) {$\ldots$} }

to

  child { edge from parent[draw=green] node[draw=none] (ellipsis) {$\ldots$} }

green edge

Clearly, this is not the right way to go about this. edge from parent is an edge drawing operation and a style and we want to change the style. But we cannot change a TikZ style (typically, anyhow) by saying <style name>[<new options>]. This isn't the right syntax. Instead, we'd normally say something like

<style name>/.style={<new options>}

or

<style name>/.append style={<new options>}

or, in some cases,

<style name>={<new options>}

but we can only use these things inside \tikzset{} or somewhere else that TikZ expects a style to be given.

In the case of a child path, TikZ expects to find edge from parent operation at the end. If we put a node after it, TikZ assumes we want to create a label on the path and, by default, this is placed midway. If the node is supposed to be the node in the tree, it needs to come after the edge, just as in other cases of drawing edges in TikZ.

So,

  child { node[draw=none] (ellipsis) {$\ldots$} edge from parent[draw=green] }

produces

node in tree

and

  child { node[draw=none] (ellipsis) {$\ldots$} edge from parent[draw=green] node [draw=none, font=\scriptsize] {label} node [font=\scriptsize, at end, draw=none] {end label} }

produces

labelled edge

And, of course,

  child { node[draw=none] (ellipsis) {$\ldots$} edge from parent[draw=none] }

produces

properly placed node in tree

If we prefer to specify this wherever, we can do so, but we need to make sure that the edge from parent operation still comes at the end. This is the default, so not using it does the trick. We can still change its style, provided we just change its style. For example,

  child { [edge from parent/.style={draw=none}] node[draw=none] (ellipsis) {$\ldots$} }

will produce the same output.

For convenience, and to please Zarko, we can create a forest-alike style which does this more easily:

   no edge/.style={
     edge from parent/.append style={draw=none}
   }

and then say

  child { [no edge] node [draw=none] (ellipsis) {$\ldots$} }

to produce the required result.

Making the example complete,

\documentclass[border=10pt,tikz]{standalone}
\begin{document}
\begin{tikzpicture}
 [
   every node/.style={circle, draw},
   no edge/.style={
     edge from parent/.append style={draw=none}
   }
 ]
  \node (r) {$r$}
  child { node (T1) {$T_1$} }
  child { [no edge] node [draw=none] (ellipsis) {$\ldots$} }
  child { node (Tn) {$T_n$} };
\end{tikzpicture}
\end{document}

produces

standard no edge


Forest

If you are willing to use a different package, forest has a no edge option which does what it says on the tin for whichever node(s) you apply it to.

For example:

\usepackage{forest}
\forestset{
  forest circles/.style={
    for tree={
      math content,
      draw,
      circle,
      minimum size=20pt,
      inner sep=0pt,
      text centered
    },
    no edge/.append style={draw=none}
  },
}
\begin{document}
\begin{forest}
  forest circles,
  [r
    [T_1]
    [\cdots, no edge]
    [T_n]
  ]
\end{forest}
\end{document}

missing edge

8
  • forest seems to be very powerful package. Unfortunately his syntax is to me not so intuitive as at standard TikZ tree (or I need more time to learn it ..), so I wish, if some option from forest (like this, which you use in your answer) ones will be available in TikZ tree too.
    – Zarko
    Nov 28, 2015 at 2:55
  • @Zarko It seems unlikely that the basic TikZ trees stuff is going to enable the sort of functionality provided by forest any time soon, just because the latter is so specialised. I know what you mean about the syntax, but it becomes very easy after a little experimentation. I now find qtree's syntax indigestible, though I drew an entire series of trees with it at one point, and wrote macros to make it compatible with Beamer overlays. I think forest is easier because every node gets brackets and not just some. So the structure is easier to read off the syntax.
    – cfr
    Nov 28, 2015 at 3:10
  • @Zarko Actually, in this case, it is easy. Here's a no edge style for TikZ trees.
    – cfr
    Nov 28, 2015 at 3:42
  • wel no edge doesn't work in TikZ trees, only in graph (by LuaTeX, if my quick check of manual was correct). Anway, reading manula again I found example which for similar case as is asked here. I will improved my answer (thank to you).
    – Zarko
    Nov 28, 2015 at 3:58
  • @Zarko I meant that my answer now provides a no edge style which I've defined. Look at the code in the first part of my answer: it is entirely done with TikZ trees and the full example before the forest version defines and uses no edge. (I said here's a no edge style. That is, I'm providing one in my answer as it doesn't exist by default.)
    – cfr
    Nov 28, 2015 at 4:01
0

I answered a duplicate before find this answer and I used opacity as a hack:

\documentclass[preview]{standalone}
\usepackage{tikz} 
\begin{document}
   \begin{tikzpicture}[every node/.style={circle,draw}]
      \node (r) {$r$}
      child { node (T1) {$T_1$} }
      child[opacity=0] { node[draw=white,opacity=1]  {$\ldots$} }
      child { node (Tn) {$T_n$} };
   \end{tikzpicture}
\end{document}

Output:

enter image description here

PS: Added as another option

You must log in to answer this question.

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