0

I searched online and in stack overflow for how to add a label to a straight dashed information set but was not successful. I see suggestions to add a label to a curved dashed information set but when I add them to my code for a straight information set, they do not work.

Does anyone have any suggestions? I attached an image of what I am trying to do. Thank you in advance.

I tried this but it did not work: \draw[dashed, label=above right:{my label}](0-1)to(0-2);

This did not work too: \node at($(0-1)!.5!(0-2)$){my label};

Here is my code.

\begin{tikzpicture} [scale=1.5] %[scale=1.5,font=\footnotesize]
    \tikzstyle{solid node} =[circle,draw,inner sep=1.5,fill=black]
    \tikzstyle{hollow node}=[circle,draw,inner sep=1.5]
    \tikzstyle{level 1}=[level distance=15mm,sibling distance=3.5cm]
    \tikzstyle{level 2}=[level distance=15mm,sibling distance=2.5cm]
    \tikzstyle{level 3}=[level distance=15mm,sibling distance=1cm]
\node(0)[hollow node,label=above:{  }]{} 
child{node[solid node]{}
    child{node[hollow node,label=below:{$(1,2)$}]{} edge from parent node[left]{a}}
    child{node[hollow node,label=below:{$(1,-1)$}]{} edge from parent node[left]{b}}
    %child{node[hollow node,label=below:{$(0,2)$}]{} edge from parent node[right]{$E$}}
    edge from parent node[left,xshift=-5]{action1}
}
child{node[solid node]{}
    child{node[hollow node,label=below:{$(2,2)$}]{} edge from parent node[left]{a}}
    child{node[hollow node,label=below:{$(1,3)$}]{} edge from parent node[right]{b}}
    edge from parent node[right,xshift=5]{action2}
};
\draw[dashed, label=above right:{my label}](0-1)to(0-2);
\end{tikzpicture}

enter image description here enter image description here

  • 1
    You can't label a path but you can place a node along it: \draw[dashed] (0-1) to node[above]{label} (0-2); – Qrrbrbirlbel Nov 19 '22 at 22:01
  • Are you aware that they are more powerful additions for TikZ that help in drawing a tree? Namely, the forest package and TikZ own graphs library? The child operation is quite cumbersome and I'd argue outdated. – Qrrbrbirlbel Nov 19 '22 at 22:03
  • Thank you for the information. I have an additional question. How do I add multiple lines at the terminal nodes? See image. When I add \, my latex program crashes. – ithoughtso Nov 20 '22 at 01:28
  • When I add that block, my code generates an error. Perhaps I placed it in the wrong area. Would you kindly be able to add it to the proper place in the code in my original question? I need each terminal node to have a double line. Thank you – ithoughtso Nov 20 '22 at 02:46
  • Yes, I'm missing a {: label={[align=center]below:{$(1,2)$\\$(3,4)$}} – Qrrbrbirlbel Nov 20 '22 at 03:04

3 Answers3

1

Not the best answer, but it solves your problem.

Creates a node on the dashed line and puts the text on the node. Whit yshift and xshiftto control the text position.

\begin{tikzpicture} [scale=1.5] %[scale=1.5,font=\footnotesize]
   \tikzstyle{solid node} =[circle,draw,inner sep=1.5,fill=black]
   \tikzstyle{hollow node}=[circle,draw,inner sep=1.5]
   \tikzstyle{level 1}=[level distance=15mm,sibling distance=3.5cm]
   \tikzstyle{level 2}=[level distance=15mm,sibling distance=2.5cm]
   \tikzstyle{level 3}=[level distance=15mm,sibling distance=1cm]

\node(0)[hollow node,label=above:{ }]{} child{node[solid node]{} child{node[hollow node,label=below:{$(1,2)$}]{} edge from parent node[left]{a}} child{node[hollow node,label=below:{$(1,-1)$}]{} edge from parent node[left]{b}} %child{node[hollow node,label=below:{$(0,2)$}]{} edge from parent node[right]{$E$}} edge from parent node[left,xshift=0.35cm,yshift = 0.55cm,rotate = 38]{action1} } child{node[solid node]{} child{node[hollow node,label=below:{$(2,2)$}]{} edge from parent node[left]{a}} child{node[hollow node,label=below:{$(1,3)$}]{} edge from parent node[right]{b}} edge from parent node[right,xshift=5]{action2} }; \draw[dashed] (0-1) node[midway, yshift = -1.3cm] {$label$} to(0-2) ; \end{tikzpicture}

The output is this:

enter image description here

If you want to rotate the text in lines use the comand rotate, an example in "action1".

Roland
  • 6,655
1

Here is a solution with forest.

enter image description here

\documentclass{article}

\usepackage{forest} \tikzset{open/.style={draw, circle, minimum width=1.5mm, inner sep=0}, closed/.style={open, fill}}

\begin{document}

\begin{forest} for tree={if level=1{closed}{open}, s sep=2cm, l sep=1.5cm } [ [, edge label={node[midway, left=4pt]{action1}}, name=A1 [, label={south:$(1,2)$}, edge label={node[midway, left]{a}}] [, label={south:$(1,-1)$}, edge label={node[midway, left]{b}}] ] [, edge label={node[midway, right=4pt]{action2}}, name=A2 [, label={south:$(2,2)$}, edge label={node[midway, left]{a}}] [, label={south:$(1,3)$}, edge label={node[midway, right]{b}}] ] ] \drawdashed--node[above]{label}(A2); \end{forest}

\end{document}

Sandy G
  • 42,558
0
  • Label to straight line is simple to add, For this you can chose between two options:
    • by use of node: \draw (<coordonate 1>) -- node[above] {text} (<coordinate 2>);
    • by use of the quotes library: \draw (<coordonate 1>) to["text"](<coordinate 2>);.
  • in MWE (Minimal Working Example) below is considered second possibility.
  • Also with `forest˛but with more sophisticated forest diagram preamble and simpler diagram body code:
\documentclass[border=3.141592]{standalone}
\usepackage{forest}
\usetikzlibrary{quotes}

\begin{document} \begin{forest} for tree={ % nodes circle, draw, minimum size=2pt, inner sep=0pt, font=\small, % tree l sep = 12mm, s sep = 11mm, if level = 1{fill}{}, }, %% edge labels /tikz/ELS/.style = {% Edge Label Style pos=0.5, node font=\scriptsize, anchor=#1}, EL/.style = {%Edge Label if n=1{edge label={node[ELS=east]{#1}}} {edge label={node[ELS=west]{#1}}} }, lbl/.style = {label={south:$#1$}} %%%% diagram body [ [, EL=action 1, name=a [, lbl={(1, 2)}, EL=a] [, lbl={(1,-1)}, EL=b] ] [, EL=action 2, name=b [, lbl={(2,2)}, EL=a] [, lbl={(1,3)}, EL=b] ] ] \draw[densely dashed, very thin,font=\scriptsize] (a) to["label"] (b); % <--- \end{forest} \end{document}

enter image description here

Zarko
  • 296,517