由图形和其他对象组成的 tikz 图形

由图形和其他对象组成的 tikz 图形

我用 tiKz 包画了一个图。

\begin{figure}[htp]
\centering
\begin{tikzpicture}
[shorten >=1pt,node distance=2cm,on grid,>=stealth',initial text=,
every state/.style={draw=blue!50,very thick,fill=blue!20},
accepting/.style=accepting by arrow]
\node[state] (E) {$E$};
\node[state] (A) [above right=of E] {$A$};
\node[state] (B) [below=of A] {$B$};
\node[state] (C) [below=of B] {$C$};
\node[state] (D) [below=of C] {$D$};
\path[->] (E) edge node [left] {\textit{label one}} (A);
\path[->] (B) edge node [right] {\textit{label two}} (A);
\path[->] (C) edge node [right] {\textit{label three}} (B);
\path[->] (D) edge node [right] {\textit{other label}} (C);
\end{tikzpicture}

我将插入四个矩形,其中的文本与图形的四个节点对齐。你知道我该怎么做吗?

编辑: 抱歉,我之前的问题不够准确。我会以这种方式插入带有与节点对齐的文本的矩形(我用 MS Power Point 绘制)。

在此处输入图片描述

你能帮助我吗?

答案1

一个可能的解决方案是:

\documentclass[border=3mm,tikz,]{standalone}
\usetikzlibrary{arrows,automata,
                chains,
                positioning,
                }
\begin{document}
    \begin{tikzpicture}[
                 > = stealth',
         shorten > = 1pt,
     node distance = 13mm and 0mm,% deleted "on grid" 
             auto,% added
      start chain = 1 going below,
      start chain = 2 going right,
every label/.style = {font=\small, align=left},% added
every state/.style = {draw=blue!50,very thick,fill=blue!20,
                      on chain=1},%node contents={$#1$},
%    initial text=,
%  accepting/.style = accepting by arrow
          X/.style = {rectangle, draw=#1, fill=#1!30, on chain=2}
                        ]
\node (A) [state] {$A$};
\node (B) [state] {$B$};
\node (C) [state] {$C$};
\node (D) [state] {$D$};
\node (E) [state,below left=20mm and 20 mm of A] {$E$};
    \begin{scope}[font=\itshape]
\path[swap,->]
            (D) edge node  {label two}   (C)
            (C) edge node  {label three} (B)
            (B) edge node  {other label} (A);
\draw[->]   (E) edge node  {label one}   (A);
    \end{scope}
%
\node [X=blue,right=11mm of A] {Text};
\node [X=green]   {Relative to};
\node [X=red]     {A};
%
\node [X=blue,right=11mm of B]     {Text};
\node [X=green]   {Relative to node B};
%
\node [X=blue,right=11mm of C]     {Text};
\node [X=green]   {Relative to node C};
%
\node (d1) [X=blue,right=11mm of D]     {Text};
\node (d2) [X=green,right=of d1.east]         {Relative to node D};
    \end{tikzpicture}
\end{document}

使用此选项,auto边缘标签的位置会更好。为了位于右侧,我添加了第一条路径(用于状态 D、C、B、A 之间的箭头)选项,swap该选项会将标签移动到所需的一侧(如果没有此选项,它们将位于左侧)。

在此处输入图片描述

编辑:我通过省略选项略微改进了(我的观点……)on grid。这允许我使用chains库来处理状态节点右侧的节点。这样代码就变得相当短了。此外,在注释中添加更多节点现在也更简单了。

相关内容