编辑

编辑

我想复制下图的结构。我不想复制外观。

在此处输入图片描述

这个 LaTeX 代码

\documentclass[tikz,convert={size=640},multi]{standalone}
\usetikzlibrary{graphs,positioning}
\usepackage{forest}
\forestset{
  default preamble={
    grow=west,
    for tree={
      parent anchor=children,
      child anchor=east,
      anchor=east,
      draw
    }
  }
}
\begin{document}
\begin{forest}
  [Acceleration
  [Map] [Destination] [Vector] [Location]
  [{Kind(Street signs, cars, people,...)},name=kind] [Distance,name=distance]]
  \graph {
    Pixel -- Line -- Shape[left=of kind.west] -- {(distance.west),(kind.west)}
  };
\end{forest}
\begin{tikzpicture}
  \graph[grow left,left anchor=west,right anchor=east] {
    Acceleration[anchor=west] --
    {Map[anchor=east], Destination[anchor=east], Vector[anchor=east],
      Location[anchor=east],
      {{Kind/{Kind(Street signs, cars, people,...)}[anchor=east],
      Distance[anchor=east]} -- Shape[anchor=east]}}
  };
\end{tikzpicture}
\end{document}
%%% Local Variables:
%%% TeX-command-extra-options: "-shell-escape"
%%% mode: latex
%%% TeX-master: t
%%% End:

渲染下面两张图片。

在此处输入图片描述

在此处输入图片描述

我做错了什么?我该如何修复这些图片?

答案1

你说的“结构”并不完全清楚。通常,对于图或树,我会理解这是指节点相互连接的方式,而不是一个节点相对于另一个节点的距离和方向。但似乎你的意思也是后者,尽管这是否真的是另一回事。

如果是这样,我认为你想要这样的东西:

结构复制

在 中这并不难做到forest。只是有点麻烦:

\documentclass[tikz,border=10pt,multi]{standalone}
\usepackage{forest}
\forestset{
  default preamble={
    for tree={
      grow=west,
      parent anchor=children,
      child anchor=parent,
      anchor=east,
      draw,
      minimum height=4ex
    }
  }
}
\begin{document}
\begin{forest}
  [Acceleration
    [Map]
    [Destination]
    [Vector]
    [Location]
    [{Kind(Street signs, cars, people,...)}, name=kind, tikz+={%
      \draw [\forestoption{edge}] (.parent anchor) -- (shape.child anchor);
    }
    ]
    [, phantom
    [Shape, before computing xy={%
      l+/.wrap pgfmath arg={#1}{width(content("kind"))},
    }, name=shape
        [Line
          [Pixel]
        ]
      ]
    ]
    [Distance, before drawing tree={%
      x-/.wrap pgfmath arg={#1}{width(content("kind"))-width(content())}
    }, edge path'={%
      (!u.parent anchor) -- (kind.child anchor |- .child anchor) -- (.child anchor)
    }, tikz+={%
      \draw [\forestoption{edge}] (.parent anchor) -- (shape.child anchor);
    }
    ]
  ]
\end{forest}
\end{document}

编辑

我不知道您在评论中要求删除什么“垂直空间”。以下代码牺牲了结构复制,以获得可能或可能不是所需的外观。

\documentclass[tikz,border=10pt,multi]{standalone}
\usepackage{forest}
\forestset{
  default preamble={
    for tree={
      grow=west,
      parent anchor=children,
      child anchor=parent,
      anchor=east,
      draw,
      minimum height=4ex
    }
  }
}
\begin{document}
\begin{forest}
  [Acceleration
    [Map]
    [Destination]
    [Vector]
    [Location]
    [{Kind(Street signs, cars, people,...)}, name=kind
      [Shape, before computing xy={%
        s/.wrap pgfmath arg={#1}{(-s("!u")+s("!un"))/2},
      }, name=shape
        [Line
          [Pixel]
        ]
      ]
    ]
    [Distance, before drawing tree={%
      x-/.wrap pgfmath arg={#1}{width(content("kind"))-width(content())}
    }, edge path'={%
      (!u.parent anchor) -- (kind.child anchor |- .child anchor) -- (.child anchor)
    }, tikz+={%
      \draw [\forestoption{edge}] (.parent anchor) -- (shape.child anchor);
    }
    ]
  ]
\end{forest}
\end{document}

改良的摆弄

答案2

问题确实出在距离、种类和像素之间的联系上。由于“森林”实际上是一棵树,它们无法与森林联系起来,我们必须使用绘制命令。因此我们命名了一些节点。随意装饰。在此处输入图片描述

\documentclass[tikz,convert={size=640},multi]{standalone}
\usepackage{tikz}
\usepackage{forest}
\usetikzlibrary{graphs,positioning}
\usetikzlibrary{shapes.geometric}
\usetikzlibrary{arrows}

\begin{document}

\begin{forest}
 for descendants={anchor=east, child anchor=east, grow=west},
 grow=west, anchor=north, parent anchor=north
 [Acceleration, rotate=90
 [Map]
 [Destination]
 [Vector]
 [Location]
 [ [Kind {\tiny (street signs, cars, people)},name=Kind [Pixel, name=Pixel]]
 [Distance, name= Distance]]]
 \draw[->] (Kind) to[out=west,in=east] (Pixel);
 \draw[->] (Distance) to[out=west,in=south east] (Pixel);
 \end{forest}
 \end{document}

答案3

如果你的forest版本包含grow=west内部for tree选项,结果是:

在此处输入图片描述

\documentclass[tikz,convert={size=640},multi]{standalone}
\usetikzlibrary{graphs,positioning}
\usepackage{forest}
\forestset{
  default preamble={
    for tree={
      grow=west,
      parent anchor=children,
      child anchor=east,
      anchor=east,
      draw
    }
  }
}
\begin{document}
\begin{forest}
  [Acceleration
  [Map] [Destination] [Vector] [Location]
  [{Kind(Street signs, cars, people,...)},name=kind]
  [Distance,name=distance
    [Shape [Line [Pixel]]]]]
\end{forest}
\end{document}

更新:现在具有shapekind手动连接

在此处输入图片描述

\documentclass[tikz,convert={size=640},multi]{standalone}
\usetikzlibrary{graphs,positioning}
\usepackage{forest}
\forestset{
  default preamble={
    for tree={
      grow=west,
      parent anchor=children,
      child anchor=east,
      anchor=east,
      %draw
    }
  }
}
\begin{document}
\begin{forest}
  [Acceleration
  [Map] [Destination] [Vector] [Location]
  [{Kind(Street signs, cars, people,...)},name=kind]
  [Distance,name=distance
    [Shape, name=shape [Line [Pixel]]]]]
    \draw (shape.north)--++(90:1mm)-|([shift={(5mm,0mm)}]kind.south west);
\end{forest}
\end{document}

相关内容