1

I have a simple tree like the following:

\documentclass[crop,tikz]{standalone}
\begin{document}
\begin{tikzpicture}[level/.style={level distance=20mm, sibling distance=20mm/#1}]
\node {(1)}
  child { 
    node {(2)} 
    child { 
        node {...} 
    }
    child { 
        node {...} 
    }
  }
  child { 
    node {(3)} 
};
\end{tikzpicture}
\end{document}

Now I want to add some labels to the edges, but the shape of the tree doesn't look right.

\documentclass[crop,tikz]{standalone}
\begin{document}
\begin{tikzpicture}[level/.style={level distance=20mm, sibling distance=20mm/#1}]
\node {(1)}
  child { 
    node {(2)} edge from parent node[pos=0.5, sloped, above,draw=none] {\emph{label1}}
    child { 
        node {...} edge from parent node[pos=0.5, sloped, above,draw=none] {\emph{label2}}
    }
    child { 
        node {...} edge from parent node[pos=0.5, sloped, above,draw=none] {\emph{label3}}
    }
  }
  child { 
    node {(3)} edge from parent node[right, pos=0.5, sloped, above,draw=none] {\emph{label4}}
};
\end{tikzpicture}
\end{document}

The error only occurs when specifying edge from parent for node (2).

Unrelated question 1: how do I use dash for some edges, setting dashed doesn't work.

Unrelated question 2: how do I use multiple-line label for node. For example, instead of (3), I try $x=1$\\$y=2$, but it doesn't work.

2 Answers 2

1

Zarko gave you already the reason why your diagram got distorted: you need to put the edge after the children. And dashed works if you use it as an option of the edge (not node), and if you add align=center to the node options you can have the text in multiple lines.

\documentclass[crop,tikz]{standalone}
\begin{document}
\begin{tikzpicture}[level/.style={level distance=20mm, sibling distance=20mm/#1}]
  \node {(1)}
    child {
      node {(2)}
        child {
          node {...}
          edge from parent node[pos=0.5, sloped, above,draw=none] {\emph{label2}}
        }
        child {node {...}
        edge from parent[dashed] node[pos=0.5, sloped, above,draw=none] {\emph{label3}}
        }
        edge from parent node[pos=0.5, sloped,above,draw=none] {\emph{label1}}
    }
    child {
      node[align=center] {$x=1$\\$y=2$}
      edge from parent node[right, pos=0.5, sloped, above,draw=none] {\emph{label4}}
    };
\end{tikzpicture}
\end{document}

enter image description here

2

one problem per question please ...

you placed code for edge nodes on the wrong place, it had to be on the end of branch:

\documentclass[crop,tikz]{standalone}
\begin{document}
    \begin{tikzpicture}[
lbl/.style = {font=\itshape\footnotesize, sloped, above},
level/.style={level distance=20mm, sibling distance=20mm/#1}]
\node {(1)}
  child {node {(2)}
    child {node {...}
           edge from parent node[lbl] {label-3}
            }
    child {node {...}
           edge from parent node[lbl] {label-4}
            }
        edge from parent node[lbl] {label-1}
        }
  child {node {(3)}
        edge from parent node[lbl] {label-2}
        };
\end{tikzpicture}
\end{document}

enter image description here

2
  • 1
    I accept the marmot's answer as it also includes your answer, but thank you so much for the help.
    – sean
    Aug 24, 2018 at 5:17
  • @qsp, marmot's does not include my answer, but solve your problem independently! does this means that you will not use my (far more) concise code for your basic problem ( i limited myself only to it, see edited answer) ? :-) :-) :-)
    – Zarko
    Aug 24, 2018 at 5:25

You must log in to answer this question.

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