为森林边缘路径添加坐标

为森林边缘路径添加坐标

我有: 在此处输入图片描述 我想要这样的东西: 在此处输入图片描述

代码:

\documentclass{article}
\usepackage{tikz}
\usepackage{forest}

\begin{document}
\begin{forest}
for tree={rectangle,rounded corners,draw, l sep=20pt,
    if n children=0{
        tier=terminal
    }{}}
[Initial State 
    [No Failures,edge label={node[midway,left] {$0.9994$}} [OK,green,edge label={node[midway,left] {$1$}}]]
    [Failure,edge label={node[midway,right] {$0.0006$}}
        [A,edge label={node[midway,left] {\small$5\cdot 10^{-4}$}}[OK,green,edge label={node[midway,left] {$0.95$}}]]
        [B,edge label={node[midway,right,yshift=-1.5mm,xshift=-0.4mm] {\small$8\cdot 10^{-5}$}}[OK,green,edge label={node[midway,left] {$0.9$}}]] 
        [C,edge label={node[midway,right,xshift=2mm] {\small$2\cdot 10^{-5}$}}
            [OK,green, edge label={node[midway,right,yshift=-5mm] {$0.4$}}]
            [1, edge label={node[midway,left] {$0.3$}}
                 [FAIL, red]]
            [2, edge label={node[midway,right] {$0.3$}}
                 [FAIL, red]]               
        ]
    ] 
]
\end{forest}
\end{document}

有人有解决方案吗?

答案1

像这样吗?

\documentclass{article}
\usepackage{forest}
\forestset{special edge/.style={        edge path={
            \noexpand\path[\forestoption{edge}]
            let \noexpand\p1=(!u.parent anchor), 
            \noexpand\p2=(.child anchor) in (!u.parent anchor) -- 
            (\noexpand\x2,{\noexpand\y1-abs(\noexpand\x2-\noexpand\x1)}) 
    --   (.child anchor)\forestoption{edge label};
        },      
}}
\begin{document}
\begin{forest}
for tree={rectangle,rounded corners,draw, l sep=20pt,
    if n children=0{
        tier=terminal,
    }{}}
[Initial State 
    [No Failures,edge label={node[midway,left] {$0.9994$}} [OK,green,edge label={node[midway,left] {$1$}}]]
    [Failure,edge label={node[midway,right] {$0.0006$}}
        [A,edge label={node[midway,left] {\small$5\cdot 10^{-4}$}}[OK,green,edge label={node[midway,left] {$0.95$}}]]
        [B,edge label={node[midway,right,yshift=-1.5mm,xshift=-0.4mm] {\small$8\cdot 10^{-5}$}}[OK,green,edge label={node[midway,left] {$0.9$}}]] 
        [C,edge label={node[midway,right,xshift=2mm] {\small$2\cdot 10^{-5}$}}
            [OK,special edge,green, edge label={node[pos=0,above left] {$0.4$}}]
            [1, edge label={node[midway,right] {$0.3$}}
                 [FAIL, red]]
            [2, edge label={node[midway,right] {$0.3$}}
                 [FAIL, red]]               
        ]
    ] 
]
\end{forest}
\end{document}

在此处输入图片描述

为了消除差距,你可能需要使用

\documentclass{article}
\usepackage{forest}
\usetikzlibrary{shapes.misc}
\forestset{special edge/.style={        edge path={
            \noexpand\path[\forestoption{edge}]
            let \noexpand\p1=(!u.parent anchor), 
            \noexpand\p2=(.child anchor) in (!u.parent anchor) -- 
            (\noexpand\x2,{\noexpand\y1-abs(\noexpand\x2-\noexpand\x1)}) 
    --   (.child anchor)\forestoption{edge label};
        },      
}}
\begin{document}
\begin{forest}
for tree={rounded rectangle,draw, l sep=20pt,
    if n children=0{
        tier=terminal,
    }{}}
[Initial State 
    [No Failures,edge label={node[midway,left] {$0.9994$}} [OK,green,edge label={node[midway,left] {$1$}}]]
    [Failure,edge label={node[midway,right] {$0.0006$}}
        [A,edge label={node[midway,left] {\small$5\cdot 10^{-4}$}}[OK,green,edge label={node[midway,left] {$0.95$}}]]
        [B,edge label={node[midway,right,yshift=-1.5mm,xshift=-0.4mm] {\small$8\cdot 10^{-5}$}}[OK,green,edge label={node[midway,left] {$0.9$}}]] 
        [C,edge label={node[midway,right,xshift=2mm] {\small$2\cdot 10^{-5}$}}
            [OK,special edge,green, edge label={node[pos=0,above left] {$0.4$}}]
            [1, edge label={node[midway,right] {$0.3$}}
                 [FAIL, red]]
            [2, edge label={node[midway,right] {$0.3$}}
                 [FAIL, red]]               
        ]
    ] 
]
\end{forest}
\end{document}

在此处输入图片描述

相关内容