如何在 Tikz 图表中指示层级?

如何在 Tikz 图表中指示层级?

我有一个用 Tikz 制作的网络。该图有三层节点。我需要画一条连接节点的虚线,并在最右边的节点 ($m_1$、$j$ 和 $i$) 旁边放置一个层标签 - 层 1、2 和 3。

\documentclass[]{report}
\usepackage{tikz}
\usetikzlibrary{arrows, automata, arrows.meta, positioning, calc}

\begin{document}

\begin{figure}[h]   
\centering  
\begin{tikzpicture}[
> = stealth, % arrow head style
shorten > = 1pt, % don't touch arrow head to node
auto,
node distance = 3cm, % distance between nodes
semithick % line style
]

\tikzstyle{every state}=[
draw = black,
thick,
fill = white,
minimum size = 4mm
]
\node [dashed, state] (a) {$i$};
\node[state] (b) at ($ (a) + (45:1.5) $) {$j$};
\node[dashed, state] (c) at ($ (a) + (135:1.5) $) {$k$};
\node[state] (d) at ($ (c) + (45:2.1) $) {$m_1$};
\node[dashed, state] (e) at ($ (c) + (90:1.5) $) {$m_2$};
\node[state] (d1) at ($ (c) + (135:2.1) $) {$m_3$};

\path[->] (d) edge node {} (c);
\path[->] (d1) edge node {} (c);
\path[dashed,->] (e) edge node {} (c);
\path[->] (b) edge node {} (a);
\path[dashed, ->] (c) edge node {} (a);
\end{tikzpicture}
\label{fig:assembly-eg}
\end{figure}

\end{document}

答案1

欢迎!类似这样?您可以使用 来local bounding box对齐层标签。另请注意 已\tikzstyle弃用。

\documentclass[]{report}
\usepackage{tikz}
\usetikzlibrary{automata, arrows.meta, positioning, calc}

\begin{document}

\begin{figure}[h]   
\centering  
\begin{tikzpicture}[
> = stealth, % arrow head style
shorten > = 1pt, % don't touch arrow head to node
auto,
node distance = 3cm, % distance between nodes
semithick % line style
]
\begin{scope}[local bounding box=tree,every state/.style={draw = black,thick,
    fill = white,minimum size = 4mm}]
  \node [dashed, state] (a) {$i$};
  \node[state] (b) at ($ (a) + (45:1.5) $) {$j$};
  \node[dashed, state] (c) at ($ (a) + (135:1.5) $) {$k$};
  \node[state] (d) at ($ (c) + (45:2.1) $) {$m_1$};
  \node[dashed, state] (e) at ($ (c) + (90:1.5) $) {$m_2$};
  \node[state] (d1) at ($ (c) + (135:2.1) $) {$m_3$};
  \path ([xshift=1em]tree.east); %<-increase local bounding box
\end{scope}
\draw[dotted] (d1) -- (e) -- (d) -- (d-|tree.east) node[right]{1};
\draw[dotted] (c) -- (b) -- (b-|tree.east) node[right]{2};
\draw[dotted] (a) -- (a-|tree.east) node[right]{3};
\path[->] (d) edge node {} (c);
\path[->] (d1) edge node {} (c);
\path[dashed,->] (e) edge node {} (c);
\path[->] (b) edge node {} (a);
\path[dashed, ->] (c) edge node {} (a);
\end{tikzpicture}
\label{fig:assembly-eg}
\end{figure}

\end{document}

在此处输入图片描述

相关内容