如何在节点子集周围绘制边框?

如何在节点子集周围绘制边框?

我需要这样的图表: 在此处输入图片描述

我已经自动化了“黑色”部分,但不知道如何处理红色边框;我现在的版本是这样的: 在此处输入图片描述

这是我编写的乳胶“代码”,如下所示,

\documentclass[border=1mm,tikz,preview]{standalone}
\usetikzlibrary{fit,backgrounds}

\newcount\prevnode

\def\EDynkin#1{
\prevnode=0
\node (1) at (2,0) [circle,draw,radius=1em] {};
\node (2) at (0,1) [circle,draw,radius=1em] {};
\node (3) at (1,0) [circle,draw,radius=1em] {};
\node (4) at (0,0.5) [circle,draw,radius=1em] {};
\node (5) at (-1,0) [circle,draw,radius=1em] {};
\node (6) at (-1,0.5) [circle,draw,radius=1em] {};
\draw (1) -- (3) -- (4)-- (6);
\draw (2) -- (4);
\draw (2) -- (3);
\draw (2) -- (6);
\draw (3) -- (5);
\draw (1) -- (2);

\foreach\kthweight[count=\k] in {#1}{
\ifnum\k=1\node at (2,0) {\scriptsize\kthweight};\fi
\ifnum\k=2\node at (0,1) {\scriptsize\kthweight};\fi
\ifnum\k>6
\node (\k) at (4-\k,0) [circle,draw,radius=1em] {};
\draw (\k) -- (\the\prevnode);
\fi
\ifnum\k>2\node at (4-\k,0) {\scriptsize\kthweight};\fi
\global\advance\prevnode by1
}
}

\begin{document}

\begin{tikzpicture}[scale=3]
\EDynkin{6,3,5,4,2,1}
\node (box) [draw=red,rounded corners,fit = (3) (4) (2)] {};
\node (box) [draw=red,rounded corners,fit = (1) ] {};
\node (box) [draw=red,rounded corners,fit = (5) (6)] {};
\end{tikzpicture}



\end{document}

有人知道怎么解决吗?如何在旁边添加正方形标题。例如,M1、M2、M3。非常感谢。

答案1

只是猜测你的问题是什么......以下 MWE 尝试重现你的草图,其中顶点使用包含数字的圆圈:

\documentclass[border=1mm,tikz,preview]{standalone}
\usetikzlibrary{backgrounds,
                fit,
                positioning}

\begin{document}

\begin{tikzpicture}[%scale=3 
  V/.style = {circle, draw,radius=1em, inner sep=2pt},
FIT/.style = {draw=red,rounded corners,fit = #1}
                    ]
% vertices
\node (1) [V] {1};
\node (4) [V, below right=of 1] {4};
\node (2) [V, below left =of 4] {2};
\node (3) [V, above right=of 4] {3};
\node (5) [V, below right=of 4] {5};
\node (6) [V, right=of 3] {6};
% groups of vertices,m red rectangled
\node[FIT = (1) (2)] {};
\node[FIT = (3) (4) (5)] {};
\node[FIT = (6) (5.south -| 6)] {};
% arrows for vertices conections
\draw   (1) -- (2) -- (5) -- (4) -- (1)
        (1) -- (3) -- (6) -- (5) -- (3) -- (4);
    
    \end{tikzpicture}
\end{document}

在此处输入图片描述

如果问题在于绘制连接顶点的命令的定义,那么将绘制顶点及其之间的连接的代码移动到您的新命令(抱歉,我不明白它的逻辑)。

附录: 考虑一下你的评论:

\documentclass[border=1mm,tikz,preview]{standalone}
\usetikzlibrary{backgrounds,
                fit,
                positioning}

\begin{document}

\begin{tikzpicture}[%scale=3 
dot/.style = {circle, fill, radius=2pt, inner sep=0pt, outer sep=0pt,
              label={[font=\scriptsize]#1}},
FIT/.style = {draw=red, rounded corners, inner sep=2ex, fit = #1},
every label/.append style = {fill=white, inner sep=1pt}
                    ]
% vertices
\node (1) [dot=left:M1] {1};
\node (4) [dot=left:M4, below right=of 1] {4};
\node (2) [dot=left:M2, below left =of 4] {2};
\node (3) [dot=:M3, above right=of 4] {3};
\node (5) [dot=below:M1, below right=of 4] {5};
\node (6) [dot=right:M1, right=of 3] {6};
% groups of vertices,m red rectangled
\scoped[on background layer]
{
\node[FIT = (1) (2)] {};
\node[FIT = (3) (4) (5)] {};
\node[FIT = (6) (5.south -| 6)] {};
}
% arrows for vertices conections
\draw   (1) -- (2) -- (5) -- (4) -- (1)
        (1) -- (3) -- (6) -- (5) -- (3) -- (4);
    
    \end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

我不明白你的目的是什么。通常,我会首先绘制内容为数字且形状为带边框的圆圈的节点,然后连接这些节点并最终使用库绘制红色框fit

\documentclass[border=1mm, tikz]{standalone}
\usetikzlibrary{fit}

\begin{document}
\begin{tikzpicture}[scale=3]

\node (1) at (0,1) [circle, draw] {1};
\node (2) at (0,0) [circle, draw] {2};
\node (3) at (2,1) [circle, draw] {3};
\node (4) at (1,0.5) [circle, draw] {4};
\node (5) at (2,0) [circle, draw] {5};
\node (6) at (4,1) [circle, draw] {6};

\draw (1) -- (2) -- (5) -- (6) -- (3) -- (1)
      (1) -- (4) -- (5) -- (3) -- (4);

\node[draw = red, rounded corners, fit = (1) (2)] {};
\node[draw = red, rounded corners, fit = (3) (4) (5)] {};
\node[draw = red, rounded corners, fit = (6)] {};

\end{tikzpicture}
\end{document}

在此处输入图片描述

也许您想使事情自动化,但我无法从您的代码中真正了解到应该以何种方式实现事情的自动化。

可以像这样将标签添加到红色框中:

\documentclass[border=1mm, tikz]{standalone}
\usetikzlibrary{fit}

\begin{document}
\begin{tikzpicture}[scale=3]

\node (1) at (0,1) [circle, draw] {1};
\node (2) at (0,0) [circle, draw, radius=1em] {2};
\node (3) at (2,1) [circle, draw, radius=1em] {3};
\node (4) at (1,0.5) [circle, draw, radius=1em] {4};
\node (5) at (2,0) [circle, draw, radius=1em] {5};
\node (6) at (4,1) [circle, draw, radius=1em] {6};

\draw (1) -- (2) -- (5) -- (6) -- (3) -- (1)
      (1) -- (4) -- (5) -- (3) -- (4);

\node[draw = red, rounded corners, fit = (1) (2), label={left:M1}] {};
\node[draw = red, rounded corners, fit = (3) (4) (5), label={[red]center:M2}] {};
\node[draw = red, rounded corners, fit = (6), label={below:M3}] {};

\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容