6

For example (used by another thread):

\documentclass{minimal}
\usepackage{tikz}
\usetikzlibrary{trees}
\begin{document}
\begin{tikzpicture}[level distance=1.5cm, grow=down,
    every node/.style={draw, circle, thin},
    edge from parent/.style={-latex, thick, draw}
]
\node (P) {P}
    child {node (Q) {Q}
        child {node (T) {T}}
        child {node (U) {U}}
    }
    child {node (R) {R}}
    child {node (S) {S}};

\path (P) -- coordinate[midway] (PQ) (Q);
\path (P) -- coordinate[midway] (PR) (R);

\draw (PQ) to[bend right=22] (PR);
\end{tikzpicture}
\end{document}

how can I add a label, e.g., M to the line PQ?

WIZARDELF
  • 1,325
  • 3
  • 14
  • 16

2 Answers2

7

Putting the label is easy by using another node. The problem is, with the draw to every node style, all of your nodes are drawn as circles, which I think you do not want for the node with the label M. You can use scope inside your tikzpicture to separate the styles as I did here.

\documentclass[tikz,border=5]{standalone}
\usetikzlibrary{trees}
\begin{document}
\begin{tikzpicture}
\begin{scope}[level distance=1.5cm, grow=down,
    every node/.style={draw, circle, thin},
    edge from parent/.style={-latex, thick, draw}
]
\node (P) {P}
    child {node (Q) {Q}
        child {node (T) {T}}
        child {node (U) {U}}
    }
    child {node (R) {R}}
    child {node (S) {S}};

\path (P) -- coordinate[midway] (PQ) (Q);
\path (P) -- coordinate[midway] (PR) (R);

\draw (PQ) to[bend right=22] (PR);
\end{scope}

\node [above left] at (PQ) {M}; % Draws the node labeled M

\end{tikzpicture}

\end{document}

enter image description here

hpesoj626
  • 17,282
3

Since the release of pgf/TikZ 3.0 it's easier to label edges, using the quotes syntax. If you already got an edge, it could be as simple as adding "text" to the edge option. Here, let's make it after the tree is drawn:

\path (P) edge["M"'] (Q);

Your bend line looks like an angle. With the also new angles library, you could add

pic[draw,angle radius=0.8cm] {angle=Q--P--R}

to the path above, for this. Now you don't even need to define coordinates (PQ) and (PR). Also the bend line is more like a radius, not just a connection between midpoints.

A complete code, modifying the suggestion by hpesoj626:

\documentclass[tikz,border=5]{standalone}
\usetikzlibrary{trees,quotes,angles}
\begin{document}
\begin{tikzpicture}
\begin{scope}[level distance=1.5cm, grow=down,
    every node/.style={draw, circle, thin},
    edge from parent/.style={-latex, thick, draw}
  ]
  \node (P) {P}
    child {node (Q) {Q}
        child {node (T) {T}}
        child {node (U) {U}}
    }
    child {node (R) {R}}
    child {node (S) {S}};
  \end{scope}
  \path (P) edge["M"'] (Q) pic[draw,angle radius=0.8cm] {angle=Q--P--R};
\end{tikzpicture}
\end{document}

labeled edges and angle

Stefan Kottwitz
  • 231,401