使用 TikZ 绘制图片

使用 TikZ 绘制图片

我问自己是否可以用 TikZ 创建如下所示的图片:

在此处输入图片描述

编辑: 这是我迄今为止尝试过的。我真的想像上面显示的原始树一样包含括号。

    \begin{document}
    \begin{tikzpicture}
      [
        grow                    = right,
        sibling distance        = 6em,
        level distance          = 10em,
        edge from parent/.style = {draw, -latex},
        every node/.style       = {font=\footnotesize},
        sloped
      ]
      \node [root] {$\textbf{x}$: \textbf{status quo}}
        child { node [env] {\color{red}{$\textbf{x}$ \textbf{is}}\\ \color{red}{\textbf{chosen}}}
          edge from parent node [below] {\textbf{No objection}} }
        child { node [dummy] {\textbf{\color{blue}{Option y}}}
          child { node [dummy] {\color{red}{$\textbf{S}$' \textbf{actions}}: \\\color{blue}{\textbf{withdraws}} or\\\color{blue}{ \textbf{maintains}}}
            child { node [env] {\textbf{\color{blue}{Option y}}}
            child { node [env] {\color{red}{$\textbf{z}$ \textbf{is chosen}}}
              edge from parent node [above] {\textbf{counterob.}}
                               node [below] {\textbf{by} $\textbf{T}$} }
                               child { node [env] {\color{red}{$\textbf{y}$ \textbf{chosen}}}
              edge from parent node [above] {\textbf{No counter-}}
                               node [below] {\textbf{objection}} }
              edge from parent node [below] {\textbf{Maintains}} }
                       child { node [env] {\color{red}{$\textbf{x}$ \textbf{is}} \\ \color{red}{\textbf{chosen}}}
                    edge from parent node [above] {\textbf{Withdraws}} }
            edge from parent node [below] {\textbf{oppose} \textbf{y}} }
          child { node [env] {\color{red}{\textbf{y} \textbf{is chosen}}}
                  edge from parent node [above, align=center]
                    {\textbf{No}\\ \textbf{opposition}\\\textbf{against} \textbf{y}}}
                  edge from parent node [above] {$\textbf{S}$ \textbf{Objects}} };
    \end{tikzpicture}
\end{document}

答案1

这是改编自Zarko 的回答使用 Forest,这使得可以更简洁地指定树并自动执行大量格式化。如果这是一次性图表,则此解决方案可能没什么意思。但是,如果您需要制作多棵树,则值得学习 Forest 或使用tikz-qtree简洁的语法。Forest 功能更强大,支持大量自动化,可以轻松灵活、一致地设置树的样式。(也就是说,我是它的粉丝。)

例如,在 Forest 中,在设置好我们想要的外观的树后,我们可以简单地写

  [x status quo
    [x is chosen, my label=No objection]
    [Option y, my label=S Objects
      [S' actions:\\withdraws or\\maintains, my label=oppose y
        [Option y, my label=Maintains
          [z is chosen, my label=counterob.\\by T]
          [y chosen, my label=No\\opposition\\against y]
        ]
        [x is chosen, my label=Withdraws]
      ]
      [y is chosen, my label=No counter-\\objection]
    ]
  ]

创建树的主要部分左侧的阶段标签。使用简单的\foreach循环添加阶段标签:

  before typesetting nodes={
    tikz+={
      \coordinate (c) at (current bounding box.west);
      \foreach \i/\j/\k [count=\n] in {!r.north/!r1.south/{(y,S): objection against x},!r1.south/!r211.north/Right to opposition,!r211.north/!r2111.south/Counter-objection by T}
      \draw [draw=blue!50!cyan, thick, decorate, decoration={brace, mirror, amplitude=5pt}] ([yshift=-1.5pt]c |- \i) -- ([yshift=1.5pt]c |- \j) node [midway, left, font=\bfseries\itshape, align=center, xshift=-15pt] {\textcolor{red}{Stage \n:}\\\k};
    }
  }

结果如下:

森林解决方案

这颜色实在是太多了。我这样做部分是为了方便看清添加颜色的位置。显然,可以进行调整以适应。

完整代码:

\documentclass[tikz,border=10pt]{standalone}
\usepackage{forest}
\usetikzlibrary{decorations.pathreplacing,arrows.meta}
\begin{document}
\begin{forest}
  circle here/.style={
    tikz+={
      \path [fill=teal] ([yshift=-1.5pt].parent anchor) circle (1.5pt);
    },
  },
  for tree={
    align=center,
    if level=0{
      font=\bfseries,
      text=teal,
      circle here,
    }{
      font=\bfseries\itshape,
      if n children=0{
        text=red,
      }{
        text=blue,
        circle here,
      },
    },
    l sep'+=80pt,
    s sep'+=20pt,
    edge={-Latex, draw=teal},
    parent anchor=children,
  },
  my label/.style={
    edge label={node [midway, sloped, font=\bfseries\itshape, align=center, above] {#1}}
  },
  before typesetting nodes={
    tikz+={
      \coordinate (c) at (current bounding box.west);
      \foreach \i/\j/\k [count=\n] in {!r.north/!r1.south/{(y,S): objection against x},!r1.south/!r211.north/Right to opposition,!r211.north/!r2111.south/Counter-objection by T}
      \draw [draw=blue!50!cyan, thick, decorate, decoration={brace, mirror, amplitude=5pt}] ([yshift=-1.5pt]c |- \i) -- ([yshift=1.5pt]c |- \j) node [midway, left, font=\bfseries\itshape, align=center, xshift=-15pt] {\textcolor{red}{Stage \n:}\\\k};
    }
  }
  [x status quo
    [x is chosen, my label=No objection]
    [Option y, my label=S Objects
      [S' actions:\\withdraws or\\maintains, my label=oppose y
        [Option y, my label=Maintains
          [z is chosen, my label=counterob.\\by T]
          [y chosen, my label=No\\opposition\\against y]
        ]
        [x is chosen, my label=Withdraws]
      ]
      [y is chosen, my label=No counter-\\objection]
    ]
  ]
\end{forest}
\end{document}

答案2

在此处输入图片描述

我不确定,如果猜对了,你想得到什么。在你的代码中(添加缺失定义后)增长到右边...

\documentclass[tikz, border=3mm, preview]{standalone}
\usetikzlibrary{decorations,
                calligraphy,
                }
\begin{document}
\begin{tikzpicture}[
Brace/.style = {decorate,
        decoration={calligraphic brace, mirror,
        pre =moveto, pre  length=1pt,
        post=moveto, post length=1pt,
        raise=3pt, amplitude=6pt},
        thick, pen colour={red}
                },
every node/.style = {font=\footnotesize\bfseries\itshape, align=center},
      root/.style = {font=\sffamily\bfseries,text=teal},
    redtxt/.style = {text=red},
   bluetxt/.style = {font=\footnotesize\bfseries\itshape\color{blue}},
% tree options
    grow                    = down,
    sibling distance        = 9em,
    level distance          = 9em,
    edge from parent/.style = {draw, -latex, sloped, auto=above, anchor=south},
                    ]
\node (root) [root] {x status quo}
    child { node (x) [redtxt] {x is chosen}
      edge from parent node {No objection} }
    child { node     [bluetxt] {Option y}
          child { node [bluetxt] {\textvolor{red}{S'} actions: \\ withdraws or\\ maintains}
            child { node (y) [bluetxt] {Option y}
            child { node (z) [redtxt] {z is chosen}
              edge from parent node  {counterob.\\ by T} }
                       child { node [redtxt] {y is chosen}
              edge from parent node {No counter-\\ objection} }
              edge from parent node {Maintains} }
                       child { node [redtxt] {x is chosen}
                    edge from parent node {Withdraws} }
            edge from parent node [above] {oppose y} }
          child { node [redtxt] {y is chosen}
                  edge from parent node {No\\ opposition\\ againsty}}
                  edge from parent node {S Objects} };
%              
\draw[Brace] (root.north -| z.west) -- 
    node[left=12pt] {Stage 1:\\ bla bla bla bla} (x -| z.west);
\draw[Brace] (x -| z.west) -- 
    node[left=12pt] {Stage 2:\\ bla bla bla bla} (y -| z.west);
\draw[Brace] (y -| z.west) --
    node[left=12pt] {Stage 3:\\ bla bla bla bla} (z.west);
\end{tikzpicture}
\end{document}

从代码中可以看出,我在 xour 代码中做了很多简化:由于我在 styler 中添加了,\texbf{...}所以删除了所有内容,我还重命名了反映节点文本颜色的节点样式。对于括号,选择了花哨的 TikZ 库。所有边缘标签都放置在边缘上方。every nodefont=\bfseries\itshapecalligraphy,

编辑: 在您的代码中,节点中有两种颜色的文本child { node [bluetxt] {S' actions: \\ withdraws or\\ maintains}这是我在第一个版本的答案中无法使用所用方法解决的。在@cfr 的帮助下(请参阅问题的评论树节点中的文本着色)我解决了这个错误的问题(请参阅问题的评论如何将颜色选项应用于 TikZ 节点的内容......?) 并且现在相应地改变 MWE 之上。

相关内容