编辑

编辑

我想画这棵树,但我希望树枝上的数据与树枝平行,而不是像我得到的结果谢谢!

  \documentclass[12pt,oneside,a4paper,fleqn]{report}


 \usepackage{forest}
 \usepackage{tikz}
 \tikzset{
    dot/.style={circle,draw,inner sep=1.5,fill=black},
   }

  \begin{document}




 \begin{forest}
    [ ,name=a,for tree={s sep=50pt,l sep=5cm,dot,grow=1},
    [ ,name=b,edge label={node[midway, yshift=0.3cm,font=\small]{$p^2_1        =0.6,~ d_1^2=4$}}    
      [ ,name=c,edge label={node[midway, yshift=0.3cm,font=\small]{$p^3_4 =0.25,~ d_4^3=5$}}  ]
  [,name=d, edge label={node[midway, yshift=0.3cm,font=\small]{$p^3_3    =0.25,~ d_3^3=2$}}  ]    
  ]
    [ ,name=e, edge label={node[midway, yshift=0.3cm,font=\small]{$p^2_1 =0.4,~ d_1^2=1.5$}} 
    [,name=g,edge label={node[midway, yshift=0.3cm,font=\small]{$p^2_1 =0.3,~ d_2^3=5$}}  ]
      [,name=h,edge label={node[midway, yshift=0.3cm,font=\small]{$p^2_1 =0.2,~ d_1^3=2$}}  ]
   ]
 ]
 \node[yshift=-5cm,name= tiii,font=\small] at (a) {$t=1$};
 \node[name= tii,font=\small] at (c|-tiii) {$t=3$};
 \node[name= t,font=\small] at (b|-tii) {$t=2$};
\end{forest}


 \end{document} 

答案1

您的意思是希望边缘标签倾斜吗?

斜边标签

\documentclass[12pt,tikz,a4paper,border=5pt]{standalone}
\usepackage{forest}
\tikzset{
  dot/.style={circle,draw,inner sep=1.5,fill=black},
}

\begin{document}

\begin{forest}
  my edge label/.style={
    edge label={node [midway, sloped, yshift=3mm, font=\small] {#1}}
  }
  [,name=a, for tree={s sep=50pt, l sep=5cm, dot, grow=0},
    [,name=b, my edge label={$p^2_1        =0.6,~ d_1^2=4$}
      [,name=c, my edge label={$p^3_4 =0.25,~ d_4^3=5$}
      ]
      [,name=d, my edge label={$p^3_3    =0.25,~ d_3^3=2$}
      ]
    ]
    [,name=e, my edge label={$p^2_1 =0.4,~ d_1^2=1.5$}
      [,name=g, my edge label={$p^2_1 =0.3,~ d_2^3=5$}
      ]
      [,name=h, my edge label={$p^2_1 =0.2,~ d_1^3=2$}
      ]
    ]
  ]
 \node[yshift=-5cm,name= tiii,font=\small] at (a) {$t=1$};
 \node[name= tii,font=\small] at (c|-tiii) {$t=3$};
 \node[name= t,font=\small] at (b|-tii) {$t=2$};
\end{forest}

\end{document}

编辑

您可能还考虑稍微调整一下边缘标签的样式,例如Very23 建议。 例如:

  my edge label/.style={
    edge label={node [pos=.6, auto, anchor=south, sloped, font=\small] {#1}}
  }

调整定位

或者,您可以进行设置,使下部分支的标签位于线下方,如 Very23 的示例所示,但要forest担心何时会出现这种情况:

  /tikz/my edge labels/.style={pos=.6, sloped, font=\small},
  my edge label/.style={
    if n=1{
      edge label={node [my edge labels, below] {#1}}
    }
    {
      edge label={node [my edge labels, above] {#1}}
    }
  }

自动标签放置

完整代码

\documentclass[12pt,tikz,a4paper,border=5pt]{standalone}
\usepackage{forest}
\tikzset{
  dot/.style={circle,draw,inner sep=1.5,fill=black},
}

\begin{document}

\begin{forest}
  /tikz/my edge labels/.style={pos=.6, sloped, font=\small},
  my edge label/.style={
    if n=1{
      edge label={node [my edge labels, below] {#1}}
    }
    {
      edge label={node [my edge labels, above] {#1}}
    }
  }
  [,name=a, for tree={s sep=50pt, l sep=5cm, dot, grow=0},
    [,name=b, my edge label={$p^2_1        =0.6,~ d_1^2=4$}
      [,name=c, my edge label={$p^3_4 =0.25,~ d_4^3=5$}
      ]
      [,name=d, my edge label={$p^3_3    =0.25,~ d_3^3=2$}
      ]
    ]
    [,name=e, my edge label={$p^2_1 =0.4,~ d_1^2=1.5$}
      [,name=g, my edge label={$p^2_1 =0.3,~ d_2^3=5$}
      ]
      [,name=h, my edge label={$p^2_1 =0.2,~ d_1^3=2$}
      ]
    ]
  ]
 \node[yshift=-5cm,name= tiii,font=\small] at (a) {$t=1$};
 \node[name= tii,font=\small] at (c|-tiii) {$t=3$};
 \node[name= t,font=\small] at (b|-tii) {$t=2$};
\end{forest}

\end{document}

如果您想要真正利用forest的功能(或者至少多利用一点),您还可以将t=标签指定为树的一部分,并创建一种方便的样式。例如:

\documentclass[12pt,tikz,a4paper,border=5pt]{standalone}
\usepackage{forest}
\tikzset{
  dot/.style={circle,draw,inner sep=1.5,fill=black},
}
\usetikzlibrary{positioning}

\begin{document}

\begin{forest}
  /tikz/my edge labels/.style={pos=.6, sloped, font=\small},
  my edge label/.style={
    if n=1{
      edge label={node [my edge labels, below] {#1}}
    }
    {
      edge label={node [my edge labels, above] {#1}}
    }
  },
  t value/.style={
    tikz={\node [font=\small, below=2cm of |- c] {$t=#1$};}
  }
  [,name=a, for tree={s sep=50pt, l sep=5cm, dot, grow=0}, t value=1
    [,name=b, my edge label={$p^2_1        =0.6,~ d_1^2=4$}, t value=2
      [,name=c, my edge label={$p^3_4 =0.25,~ d_4^3=5$}, t value=3
      ]
      [,name=d, my edge label={$p^3_3    =0.25,~ d_3^3=2$}
      ]
    ]
    [,name=e, my edge label={$p^2_1 =0.4,~ d_1^2=1.5$}
      [,name=g, my edge label={$p^2_1 =0.3,~ d_2^3=5$}
      ]
      [,name=h, my edge label={$p^2_1 =0.2,~ d_1^3=2$}
      ]
    ]
  ]
\end{forest}

\end{document}

唯一一个节点需求这里要指出的是,c但我留下了其他的,因为命名它们往往很有用。

以下是(非常相似的)输出:

类似的结果

答案2

正如 @cfr 在他的回答中提到的那样,你希望你的边缘标签是倾斜的,但是通过删除节点 c 和 g 中的 yshift 选项,你会得到这样的结果

在此处输入图片描述

代码

\documentclass[12pt,oneside,a4paper,fleqn]{report}


\usepackage{forest}
\usepackage{tikz}
\tikzset{
dot/.style={circle,draw,inner sep=1.5,fill=black},}

\begin{document}




\begin{forest}
[,name=a,for tree={s sep=50pt,l sep=5cm,dot,grow=1},
[,name=b,edge label={node[midway, yshift=0.3cm,font=\small,sloped]{$p^2_1=0.6,~ d_1^2=4$}}    
[,name=c,edge label={node[midway,font=\small,sloped,below]{$p^3_4 =0.25,~ d_4^3=5$}}  ]
[,name=d, edge label={node[midway, yshift=0.3cm,font=\small,sloped]{$p^3_3    =0.25,~ d_3^3=2$}}  ] ]
[,name=e, edge label={node[midway, yshift=0.3cm,font=\small,sloped]{$p^2_1 =0.4,~ d_1^2=1.5$}} 
[,name=g,edge label={node[midway,font=\small,sloped,below]{$p^2_1 =0.3,~ d_2^3=5$}}  ]
[,name=h,edge label={node[midway, yshift=0.3cm,font=\small,sloped]{$p^2_1 =0.2,~ d_1^3=2$}}  ] ]]
\node[yshift=-5cm,name= tiii,font=\small] at (a) {$t=1$};
\node[name= tii,font=\small] at (c|-tiii) {$t=3$};
\node[name= t,font=\small] at (b|-tii) {$t=2$};
\end{forest}
\end{document} 

相关内容