如何在 TikZ 中绘制容器框?如何使其具有圆角?

如何在 TikZ 中绘制容器框?如何使其具有圆角?

我想要画这样的东西:

在此处输入图片描述

这是我能画出来的:

在此处输入图片描述

这是我的 TikZ 代码:

\documentclass{article}
\usepackage{verbatim}
\usepackage[active,tightpage,floats]{preview}
\setlength\PreviewBorder{30pt}%
\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}

\begin{figure}[ht]
  \centering
  \begin{tikzpicture}
    \coordinate (Origin)   at (0,0);
    \coordinate (XAxisMin) at (0,0);
    \coordinate (XAxisMax) at (5.5,0);
    \coordinate (YAxisMin) at (0,0);
    \coordinate (YAxisMax) at (0,4.5);
    \draw [thick, gray,-latex] (XAxisMin) -- (XAxisMax);% Draw x axis
    \draw [thin, gray,-latex] (YAxisMin) -- (YAxisMax);% Draw y axis


    \foreach \x in {1,2,...,10}{% Two indices running over each
      \foreach \y in {1,2,...,8}{% node on the grid we have drawn 
        \node[thick,draw,circle,inner sep=2pt,fill=gray!60] at (0.5*\x,0.5*\y) {};
            % Places a dot at those points
      }
    }

  \end{tikzpicture}
  \caption{Single tile of computation (highlighted)}
  \label{figure:single}
\end{figure}
\end{document}

答案1

您可以通过输入节点选项来命名圆圈name=circle-\x-\y,然后使用该fit库在区域的角圆圈周围放置一个矩形节点。要让矩形出现在圆圈后面,您可以使用 PGF 的分层功能:

\documentclass[border=5mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc, fit, shapes.geometric}
\pgfdeclarelayer{background}
\pgfdeclarelayer{foreground}
\pgfsetlayers{background,main,foreground}


\begin{document}

  \begin{tikzpicture}
    \coordinate (Origin)   at (0,0);
    \coordinate (XAxisMin) at (0,0);
    \coordinate (XAxisMax) at (5.5,0);
    \coordinate (YAxisMin) at (0,0);
    \coordinate (YAxisMax) at (0,4.5);
    \draw [thick, gray,-latex] (XAxisMin) -- (XAxisMax);% Draw x axis
    \draw [thin, gray,-latex] (YAxisMin) -- (YAxisMax);% Draw y axis


    \foreach \x in {1,2,...,10}{% Two indices running over each
      \foreach \y in {1,2,...,8}{% node on the grid we have drawn 
        \node[thick,draw,circle,inner sep=2pt,fill=gray!60, name=circle-\x-\y] at (0.5*\x,0.5*\y) {};
            % Places a dot at those points
      }
    }
    \begin{pgfonlayer}{background}
    \node [draw, fill=cyan!25, rectangle, rounded corners, fit={(circle-1-5) (circle-10-8)}] {};
    \end{pgfonlayer}
  \end{tikzpicture}
\end{document}

答案2

这是另一个解决方案。它与 Jake 的解决方案有一些共同的特点,例如节点有一个名称,并且突出显示的区域被放入background。但是,这个解决方案没有利用fit库,而是直接使用节点的锚点。请注意,为了获得美观的外观,节点应该有一个outer sep值。

代码:

\documentclass{article}
\usepackage{verbatim}
\usepackage[active,tightpage,floats]{preview}
\setlength\PreviewBorder{30pt}%
\usepackage{tikz}
\usetikzlibrary{calc,backgrounds}

\begin{document}

\begin{figure}[ht]
  \centering
  \begin{tikzpicture}
    \coordinate (Origin)   at (0,0);
    \coordinate (XAxisMin) at (0,0);
    \coordinate (XAxisMax) at (5.5,0);
    \coordinate (YAxisMin) at (0,0);
    \coordinate (YAxisMax) at (0,4.5);
    \draw [thick, gray,-latex] (XAxisMin) -- (XAxisMax);% Draw x axis
    \draw [thin, gray,-latex] (YAxisMin) -- (YAxisMax);% Draw y axis


    \foreach \x in {1,2,...,10}{% Two indices running over each
      \foreach \y in {1,2,...,8}{% node on the grid we have drawn 
        \node[thick,draw,circle,inner sep=2pt,fill=gray!60,outer sep=5pt] (\x-\y) at (0.5*\x,0.5*\y) {};
            % Places a dot at those points
      }
    }

    \begin{scope}[on background layer]
     \draw[rounded corners,fill=green!50!lime!30]
       (1-5.south west)rectangle(10-8.north east);
    \end{scope}

  \end{tikzpicture}
  \caption{Single tile of computation (highlighted)}
  \label{figure:single}
\end{figure}
\end{document}

结果:

在此处输入图片描述

相关内容