在 Tikz 矩阵中围绕项目创建框

在 Tikz 矩阵中围绕项目创建框

我有以下 Tikz 图片:

   \begin{center}
      \begin{tikzpicture}[>=stealth,sloped]
        \matrix (tree) [%
          matrix of nodes,
          minimum size=1cm,
          column sep=2.5cm,
          row sep=1cm,
        ]
        {
            $v_1$ &        & $v_3$ & $f_2$ & $v_5$ \\
                  &  $f_1$ &       &       &       \\
            $v_2$ &        & $v_4$ &       &       \\
        };
        \draw[->] (tree-1-1) -- (tree-2-2) node [near end,above, rounded corners] {$1$};
        \draw[->] (tree-3-1) -- (tree-2-2) node [near end,below] {$2$};
        \draw[->] (tree-2-2) -- (tree-1-3) node [near start,above] {$1$};
        \draw[->] (tree-2-2) -- (tree-3-3) node [near start,below] {$2$};
        \draw[->] (tree-1-3) -- (tree-1-4) node [near end,above] {$1$};
        \draw[->] (tree-1-4) -- (tree-1-5) node [near start,above] {$1$};
      \end{tikzpicture}
    \end{center}

是否可以在 和 周围添加方框$v_i$$f_j$例如圆圈?我希望为$v_i$和添加不同的方框$f_j$,因为它们具有不同的含义。

顺便说一下,我是 Tikz 的新手。

谨致问候,Jens

答案1

如果你想让所有节点都有相同的“盒子”

nodes={draw,circle}如果您希望所有节点都在圆形“框”内,可以添加选项:

\documentclass[tikz]{standalone}
\usetikzlibrary{matrix}
\begin{document}
    \begin{tikzpicture}[>=stealth,sloped]
        \matrix (tree) [%
          matrix of nodes,
          minimum size=1cm,
          column sep=2.5cm,
          row sep=1cm,
          nodes={draw,circle}
        ]
        {
            $v_1$ &        & $v_3$ & $f_2$ & $v_5$\\
                   &  $f_1$ &       &       &      \\
            $v_2$  &        & $v_4$ &       &      \\
        };
        \draw[->] (tree-1-1) -- (tree-2-2) node [near end,above, rounded corners] {$1$};
        \draw[->] (tree-3-1) -- (tree-2-2) node [near end,below] {$2$};
        \draw[->] (tree-2-2) -- (tree-1-3) node [near start,above] {$1$};
        \draw[->] (tree-2-2) -- (tree-3-3) node [near start,below] {$2$};
        \draw[->] (tree-1-3) -- (tree-1-4) node [near end,above] {$1$};
        \draw[->] (tree-1-4) -- (tree-1-5) node [near start,above] {$1$};
    \end{tikzpicture}
\end{document}

在此处输入图片描述

所有功劳归于克皮姆,这个很好的建议是根据作者的要求在这个答案中展示的。

如果你想为不同类型的节点设置不同的样式

使用matrix仍然可以,但是比较复杂。我提出一个常规解决方案:

\documentclass[tikz]{standalone}
\begin{document}
    \begin{tikzpicture}[>=stealth,sloped]
        \begin{scope}[every node/.style={minimum size=1cm,draw,circle}]
            \node (v1) at (0,0) {$v_1$};
            \node (v2) at (0,-2) {$v_2$};
            \node (v3) at (5,0) {$v_3$};
            \node (v4) at (5,-2) {$v_4$};
            \node (v5) at (10,0) {$v_5$};
        \end{scope}
        \begin{scope}[every node/.style={minimum size=1cm,draw,rectangle}]
            \node (f1) at (2.5,-1) {$f_1$};
            \node (f2) at (7.5,0) {$f_2$};
        \end{scope}
        \draw[->] (v1) -- (f1) node [near end,above, rounded corners] {$1$};
        \draw[->] (v2) -- (f1) node [near end,below] {$2$};
        \draw[->] (f1) -- (v3) node [near start,above] {$1$};
        \draw[->] (f1) -- (v4) node [near start,below] {$2$};
        \draw[->] (v3) -- (f2) node [near end,above] {$1$};
        \draw[->] (f2) -- (v5) node [near start,above] {$1$};
    \end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容