在 TikZ 中画一条线来包围一些物体

在 TikZ 中画一条线来包围一些物体

给定一个圆数组,我想画一条紧紧围绕这些圆的线。一张图片胜过千言万语:

在此处输入图片描述

生成圆形数组的代码如下:

\documentclass[border=4pt]{standalone}
\usepackage[dvipsnames]{xcolor}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
  [every node/.style={draw=LimeGreen, fill=LimeGreen!15,
    circle, inner sep=.1111em, minimum size=1.2222em},
   thick]

  \node [] (01) at (0,0) {1};
  \node [left] (02) at (01.west) {};
  \node [left] (03) at (02.west) {};
  \node [left] (04) at (03.west) {};

  \node [above] (05) at (01.north) {};
  \node [above] (06) at (02.north) {};
  \node [above] (07) at (03.north) {3};
  \node [above] (08) at (04.north) {};

  \node [below] (09) at (01.south) {};
  \node [below] (10) at (02.south) {};
  \node [below] (11) at (03.south) {};
  \node [below] (12) at (04.south) {};
\end{tikzpicture}
\end{document}

答案1

这是一种可能的方法,非常符合@cfr 所建议的,但是用local bounding box而不是fit

\documentclass[border=4pt]{standalone}
\usepackage[dvipsnames]{xcolor}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
  [every node/.style={draw=LimeGreen, fill=LimeGreen!15,
    circle, inner sep=.1111em, minimum size=1.2222em},
   thick]
  \begin{scope}[local bounding box=circs]
  \node [] (01) at (0,0) {1};
  \node [left] (02) at (01.west) {};
  \node [left] (03) at (02.west) {};
  \node [left] (04) at (03.west) {};

  \node [above] (05) at (01.north) {};
  \node [above] (06) at (02.north) {};
  \node [above] (07) at (03.north) {3};
  \node [above] (08) at (04.north) {};

  \node [below] (09) at (01.south) {};
  \node [below] (10) at (02.south) {};
  \node [below] (11) at (03.south) {};
  \node [below] (12) at (04.south) {};
  \end{scope}
  \draw[rounded corners={(1.2222em+2*\pgflinewidth)/2}] ([xshift=-\pgflinewidth]circs.north west) 
  |- ([yshift=-\pgflinewidth]circs.south) -| ([xshift=\pgflinewidth]circs.north east);
\end{tikzpicture}
\end{document}

在此处输入图片描述

题外话:在我看来,你最好使用matrix of nodesIMHO。

\documentclass[border=4pt]{standalone}
\usepackage[dvipsnames]{xcolor}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}[thick]
  \matrix[matrix of nodes,nodes in empty cells,inner sep=0pt,outer
  sep=\pgflinewidth/2,
  cells={nodes={draw=LimeGreen, fill=LimeGreen!15,execute at begin
  node=\vphantom{1},
    circle, inner sep=.1111em, minimum size=1.2222em}}] (circs)
    { & 3 & & \\
      &   & & 1 \\
      & & & \\};
  \draw[rounded corners={(1.2222em+2*\pgflinewidth)/2}] (circs.north west) 
  |- (circs.south) -| (circs.north east);
\end{tikzpicture}
\end{document}

在此处输入图片描述

或者使用任意线宽(用 进行设置)my line width/.initial=...

\documentclass[border=4pt]{standalone}
\usepackage[dvipsnames]{xcolor}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}[thick,my line width/.initial=1.5pt]
  \matrix[matrix of nodes,nodes in empty cells,inner sep=0pt,outer
  sep=\pgfkeysvalueof{/tikz/my line width}/2,
  cells={nodes={draw=LimeGreen, fill=LimeGreen!15,execute at begin
  node=\vphantom{1},
    circle, inner sep=.1111em, minimum size=1.2222em}}] (circs)
    { & 3 & & \\
      &   & & 1 \\
      & & & \\};
  \draw[line width=\pgfkeysvalueof{/tikz/my line width},rounded corners={(1.2222em+0.8pt+\pgfkeysvalueof{/tikz/my line width})/2}] (circs.north west) 
  |- (circs.south) -| (circs.north east) ;
  % the line width of thick is 0.8pt
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容