我指定一个矩形的角,跨越矩阵的某个子矩阵,并希望这个矩形成为一个节点。最小的例子是:
\documentclass{standalone}
\usepackage{tikz,pgf}
\usetikzlibrary{scopes,arrows,calc,shapes.misc,shapes.arrows,chains,matrix,positioning,decorations.pathmorphing,shapes}
\begin{document}
\newcommand{\bbrect}[2]{\draw (B-#1-2.north west) rectangle (B-#2-2.south east)}
\begin{tikzpicture}
\ttfamily
\matrix (B) [matrix of nodes, ampersand replacement = \&] {
\hline
{0} \& {12}\\
{1} \& {7}\\
{2} \& {2}\\
{3} \& {2}\\
{} \& {2}\\
{} \& {2}\\
{} \& {2}\\
{} \& {2}\\
};
\bbrect{1}{1};
\bbrect{2}{2};
\bbrect{3}{8};
\end{tikzpicture}
\end{document}
如何实现这一点?
答案1
您可以使用fit
库。指定选项rectangle
、draw
跨越矩形的点以及inner sep=0
紧密拟合:
\newcommand{\bbrect}[2]{%
\node[rectangle, draw, fit=(B-#1-2.north west) (B-#2-2.south east),
inner sep=0pt] {}}
\hline
使用您的代码,这会将所需的矩形添加到: