树中的垂直线和节点的延伸

树中的垂直线和节点的延伸

我正在尝试生成一棵树,树中有一条垂直线正好穿过中间,就在顶部节点的 0.5 和底部节点的 2 之后,我想在它后面直接写上 1.25,1.25,从某种意义上说,这是在延伸节点

\documentclass{article}
\usepackage{tikz}
\begin{document}
\tikzstyle{bag} = [text width=2em, text centered]
\tikzstyle{end} = []
\begin{tikzpicture}[sloped]
  \node (a) at ( 0,0) [bag] {$\textcolor{blue}{1},\textcolor{purple}{1}$};
  \node (b) at ( 4,-1.5) [bag] {$\textcolor{blue}{0.5},\textcolor{purple}{2}$};
  \node (c) at ( 4,1.5) [bag] {$\textcolor{blue}{2},\textcolor{purple}{0.5}$};
  \node (e) at ( 8,0) [bag] {$\textcolor{blue}{1},\textcolor{purple}{1}$};
  \draw [->] (a) to node [below] {$1/2$} (b);
  \draw [->] (a) to node [above] {$1/2$} (c);
  \draw [->] (c) to node [above] {$1/2$} (e);
  \draw [->] (b) to node [below] {$1/2$} (e);

\end{tikzpicture}
\end{document}

有没有直接的方法可以做到这一点?我想得到的图像: 在此处输入图片描述

虽然我更喜欢更微妙的虚线直线,但如果可能的话,我也想在下面写一些文字

答案1

一种可能性显示三种选项:

  • 前两个,使用你的 TikZ 方法,首先使用直线,然后使用装饰snake

    \documentclass{article}
    \usepackage{tikz}
    \usetikzlibrary{decorations.pathmorphing}
    
    \tikzset{
      bag/.style={text width=2em, text centered},
     end/.style={}
    } 
    
    \begin{document}
    
    \begin{tikzpicture}[sloped]
      \node (a) at ( 0,0) [bag] {$\textcolor{blue}{1},\textcolor{purple}{1}$};
      \node (b) at ( 4,-1.5) [bag,text width=4em] {$\textcolor{blue}{0.5},\textcolor{purple}{2\phantom{.5}}$};
      \node (c) at ( 4,1.5) [bag,text width=3em] {$\textcolor{blue}{\phantom{.5}2},\textcolor{purple}{0.5}$};
      \node (e) at ( 8,0) [bag] {$\textcolor{blue}{1},\textcolor{purple}{1}$};
      \draw [->] (a) to node[below] {$1/2$} (b);
      \draw [->] (a) to node[above] {$1/2$} (c);
      \draw [->] (c.south east) to node[above] {$1/2$} (e);
      \draw [->] (b) to node[below] {$1/2$} (e);
      \draw ([yshift=2ex]c.north) -- ([yshift=-2ex]b.south) node[below] {$\textcolor{blue}{1.25},\textcolor{purple}{1.25}$};
    \end{tikzpicture}\par\bigskip
    
    
    \begin{tikzpicture}[sloped,decoration=snake]
      \node (a) at ( 0,0) [bag] {$\textcolor{blue}{1},\textcolor{purple}{1}$};
      \node (b) at ( 4,-1.5) [bag,text width=4em] {$\textcolor{blue}{0.5},\textcolor{purple}{2\phantom{.5}}$};
      \node (c) at ( 4,1.5) [bag,text width=3em] {$\textcolor{blue}{\phantom{.5}2},\textcolor{purple}{0.5}$};
      \node (e) at ( 8,0) [bag] {$\textcolor{blue}{1},\textcolor{purple}{1}$};
      \draw [->] (a) to node[below] {$1/2$} (b);
      \draw [->] (a) to node[above] {$1/2$} (c);
      \draw [->] (c.south east) to node[above] {$1/2$} (e);
      \draw [->] (b) to node[below] {$1/2$} (e);
      \draw[decorate] ([yshift=2ex]c.north) -- ([yshift=-2ex]b.south) node[below] {$\textcolor{blue}{1.25},\textcolor{purple}{1.25}$};
    \end{tikzpicture}
    
    \end{document}
    

    在此处输入图片描述

    您可能希望在最后一个示例中的数字之间添加一些额外的空格。

  • 还有一个选项是使用tikz-cd

    \documentclass{article}
    \usepackage{tikz-cd}
    
    \begin{document}
    
    \begin{tikzcd}[
    column sep=2cm,
    execute at end picture={
      \draw ([yshift=2ex]top.north) -- ([yshift=-2ex]bottom.south)
        node[below] {$\textcolor{blue}{1.25},\textcolor{purple}{1.25}$};
      }
    ]
    & |[alias=top]|\textcolor{blue}{\phantom{.5}2},\textcolor{purple}{0.5}\ar[dr,"1/2"] & \\
     \textcolor{blue}{1},\textcolor{purple}{1}\ar[ur,"1/2"]\ar[dr,"1/2",swap] & & \textcolor{blue}{1},\textcolor{purple}{1} \\
    & |[alias=bottom]|\textcolor{blue}{0.5},\textcolor{purple}{2\phantom{.5}}\ar[ur,"1/2",swap] &
    \end{tikzcd}
    
    \end{document}
    

    在此处输入图片描述

答案2

您的代码让我想起了自动化的草图,因此以下代码也许就是您要找的:

\documentclass[border=3mm,
               tikz]{standalone}
\usetikzlibrary{arrows.meta,automata,quotes,positioning}

    \begin{document}
    \begin{tikzpicture}[
     node distance = 24mm and 42mm,
                 > = {Stealth[length=5pt,width=4pt]},
bar/.style = {inner ysep=2pt},
every edge quotes/.style = {sloped, anchor=north}
                        ]
\node (a) [bar]             {$\textcolor{blue}{1},\textcolor{purple}{1}$};
\node (b) 
     [bar,below right=of a] {$\textcolor{blue}{0.5},\textcolor{purple}{2}$};
\node (c) 
     [bar,above right=of b] {$\textcolor{blue}{1},\textcolor{purple}{1}$};
\node (d) 
     [bar,above right=of a] {$\textcolor{blue}{2},\textcolor{purple}{0.5}$};
\path[->]   (a) edge ["$1/2$"]  (b)
            (b) edge ["$1/2$"]  (c)
            (a) edge ["$1/2$"]  (d)
            (d) edge ["$1/2$"]  (c);
    \end{tikzpicture}
\end{document}

图片:

在

升级:好吧,另一个答案已经被接受了,而且这个答案更清楚,问题是什么。无论如何,另一个(我认为简单的)解决方案是:

\documentclass[border=3mm,
               tikz, ]{standalone}
\usetikzlibrary{arrows.meta,automata,quotes,positioning}

    \begin{document}
    \begin{tikzpicture}[
every edge quotes/.style = {sloped, anchor=north},
       every node/.style = {font=\bfseries,text width=1.5em, align=center}
                        ]
\node (a)                    {\textcolor{blue}{1},\;\textcolor{purple}{1}};
\node (b1) [left]   at (4,2) {\textcolor{blue}{0.5}};
\node (b2) [right]  at (4,2) {\textcolor{purple}{2}};
\node (c)           at (8,0) {\textcolor{blue}{2},\;\textcolor{purple}{0.5}};
\node (d1) [left]   at (4,-2){\textcolor{blue}{1}};
\node (d2) [right]  at (4,-2){\textcolor{purple}{1}};
\path[very thick,purple,->]
    (a) edge["$1/2$"] (b1.west)
    (a) edge["$1/2$"] (d1.west)
    (b2.east) edge["$1/2$"] (c)
    (d2.east) edge["$1/2$"] (c);
\draw[decorate,decoration=snake,thick] (b1.north east) -- (d1.south east);
\end{tikzpicture}
    \end{document}

这使:

在此处输入图片描述

相关内容