TikZ 线点和树

TikZ 线点和树

给出以下代码:

\documentclass[12pt]{article}

\usepackage{tikz}
\usetikzlibrary{arrows,shapes}

\begin{document}

\begin{center}
\begin{tikzpicture}
\node (node1)   at (-6,0)   [draw,fill=gray!30] {node1};
\node (node2)   at (-2,-1.5)    [draw] {node2};
\node (node3)   at (2,-1.5) [draw] {node3};
\node (node4)   at (6,-1.5) [draw] {node4};

\draw (node cs:name=node1,anchor=east) |- (8,0);
\draw (node cs:name=node4,anchor=north)[-*] |- (6,0);
\draw (node cs:name=node3,anchor=north)[-*] |- (2,0);
\draw (node cs:name=node2,anchor=north)[-*] |- (-2,0);
\end{tikzpicture}
\end{center}

\end{document}

在生成的图片中,结束来自node2node3和的线条的点node4刚好位于来自 的水平线下方node1

在此处输入图片描述

1)如何放置点,使它们垂直居中超过水平线?

trees2) 是否可以使用TikZ 库或包来实现具有完全相同特征的相同图形forest? 怎么做?

答案1

下面的代码显示了将点放置在线上的两种不同选项。第一种使用自定义箭头尖(名为Midcircle),它与您使用的箭头尖类似*,只是圆圈以路径的结束坐标为中心。第二种选项是使用 来node制作点,而不是箭头尖。

我还稍微改变了你使用的坐标。node cs坐标系具有隐式形式,因此你可以说node1.east,而不是node cs:name=node1,anchor=east

我使用垂直坐标定义了垂直线的末端:(node2.north |- node1.east)

代码输出

\documentclass[12pt]{article}

\usepackage{tikz}
\usetikzlibrary{arrows.meta}

% define a new arrow tip, filled circle centered on the end of the path
\pgfdeclarearrow{
  name=Midcircle,
  parameters= {\the\pgfarrowlength},  
  setup code={
   \pgfarrowssettipend{0.25\pgfarrowlength}
   \pgfarrowssetlineend{-0.25\pgfarrowlength}
   \pgfarrowlinewidth=\pgflinewidth
   \pgfarrowssavethe\pgfarrowlength
  },
  drawing code={
   \pgfpathcircle{\pgfpoint{0.25\pgfarrowlength}{0pt}}{0.5\pgfarrowlength}
   \pgfusepathqfill
  },
  defaults = { length = 5pt }
}
\begin{document}

\begin{center}
\begin{tikzpicture}[
  box/.style={draw}
]
\node [box,fill=gray!30] (node1) at (-6,0)    {node1};
\node [box]              (node2) at (-2,-1.5) {node2};
\node [box]              (node3) at (2,-1.5)  {node3};
\node [box]              (node4) at (6,-1.5)  {node4};

\draw               (node1.east)  -- (8,0);
\draw [-Midcircle] (node4.north) -- (node4.north |- node1.east);
\draw [-Midcircle] (node3.north) -- (node3.north |- node1.east);
\draw [-Midcircle] (node2.north) -- (node2.north |- node1.east);
\end{tikzpicture}

\begin{tikzpicture}[
  box/.style={draw},
  dot/.style={minimum size=5pt,inner sep=0pt,fill,circle,node contents={}}
]
\node [box,fill=gray!30] (node1) at (-6,0)    {node1};
\node [box]              (node2) at (-2,-1.5) {node2};
\node [box]              (node3) at (2,-1.5)  {node3};
\node [box]              (node4) at (6,-1.5)  {node4};

\draw (node1.east)  -- (8,0);
\draw (node4.north) -- (node4.north |- node1.east) node[dot];
\draw (node3.north) -- (node3.north |- node1.east) node[dot];
\draw (node2.north) -- (node2.north |- node1.east) node[dot];
\end{tikzpicture}
\end{center}

\end{document}

答案2

您可以使用该shorten选项将圆置于线上的中心。

\documentclass[12pt]{article}

\usepackage{tikz}
\usetikzlibrary{arrows,shapes}
\usetikzlibrary{arrows.meta}

\begin{document}

    \begin{center}
        \begin{tikzpicture}
        \node (node1)   at (-6,0)   [draw,fill=gray!30] {node1};
        \node (node2)   at (-2,-1.5)    [draw] {node2};
        \node (node3)   at (2,-1.5) [draw] {node3};
        \node (node4)   at (6,-1.5) [draw] {node4};

        \draw (node cs:name=node1,anchor=east) -- (8,0);
        \draw[-*, shorten >=-2pt] (node cs:name=node4,anchor=north) |- (6,0);
        \draw[-*, shorten >=-2pt] (node cs:name=node3,anchor=north) |- (2,0);
        \draw[-*, shorten >=-2pt] (node cs:name=node2,anchor=north) |- (-2,0);
        \end{tikzpicture}
    \end{center}

\end{document}

输出:

在此处输入图片描述

相关内容