答案1
这是使用 的 TikZ 提案fit
。我将代码包装在一个名为 的命令中mymatrixbox
。它需要 4 个输入,这些输入使用单元格坐标定义框的边界(它还需要 1 个可选输入,允许您对默认样式进行一些控制)。
它的工作方式是这样的,假设你想从 (1,1) 到 (3,1) 画一个框,然后使用\mymatrixbox{1}{1}{3}{1}
。要围绕单个单元格画一个框,只需重复单元格坐标 - 例如\matrixbox{2}{3}{2}{3}
。如果我想围绕整个矩阵画一个框,那么代码就是\mymatrixbox{1}{1}{3}{3}
。
\documentclass[margin=0.5cm]{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix,fit}
\pgfkeys{tikz/mymatrix/.style={matrix of math nodes,inner sep=0pt,row sep=0em,column sep=0em,nodes={inner sep=6pt}}}
\newcommand*\mymatrixbox[5][]{\node [fit= (m-#2-#3) (m-#4-#5)] [draw=blue,thick,dashed,rounded corners,inner sep=-2pt,#1] {};}
\begin{document}
\begin{tikzpicture}[baseline=0cm]
\matrix [mymatrix] (m)
{
1 & 0 & 0 \\
0 & 1 & 0 \\
0 & 0 & 1 \\
};
\mymatrixbox{1}{1}{3}{1}
\mymatrixbox{3}{2}{3}{3}
\mymatrixbox[red]{2}{3}{2}{3}
\end{tikzpicture}
\end{document}
答案2
一个简单的解决方案pstricks
和一个简单的matrix
环境amsmath
:
\documentclass[11pt, svgnames]{article}
\usepackage{amssymb, amsmath, array}
\usepackage{pst-node}
\usepackage{auto-pst-pdf}
\begin{document}
\begin{equation*}
\begin{postscript}
\psset{nodesep=5pt, boxsize=0.7em, linearc=.1, framearc=0.2, linestyle=dashed, dash= 4pt 2pt, linecolor=LightBlue}
\setlength{\arraycolsep}{6pt}
\setlength{\extrarowheight}{3pt}
\begin{matrix}
\Rnode{A}{1}& 0 & 0 \\
0 &1 & \rnode{E}{0} \\
\Rnode{B}{0}& \Rnode{C}{0} & \Rnode{D}{1}
\end{matrix}
\ncbox{A}{B}
\ncbox{C}{D}
\fnode[framesize = 0.56 0.50](E){F}
\end{postscript}
\end{equation*}
\end{document}
答案3
只是为了完整性:稍微修改后的版本Milo 的回答很棒它适用于任意矩阵,即具有Bernhards 的回答很好而不必经历通常的“如何编译包含 pstricks 的文档,我能否确保结果不依赖于编译器?”问题。也就是说,您不必将矩阵写为tikzpicture
。您需要付出的代价是您必须事先“标记”要装箱的元素\tikznode
。
\documentclass{article}
\usepackage{amsmath,bbm}
\usepackage{tikz}
\usetikzlibrary{fit}
\newcommand{\tikznode}[2]{\relax
\ifmmode%
\tikz[remember picture,baseline=(#1.base),inner sep=0pt] \node (#1) {$#2$};
\else
\tikz[remember picture,baseline=(#1.base),inner sep=0pt] \node (#1) {#2};%
\fi}
\tikzset{box around/.style={
draw,rounded corners,
inner sep=2pt,outer sep=0pt,
node contents={},fit=#1
},
}
\begin{document}
\begin{equation}
\mathbbm{1}_3=\begin{pmatrix}
\tikznode{m-1-1}{1} & 0 & 0\\
\tikznode{m-2-1}{0} & 1 & \tikznode{m-2-3}{0}\\
\tikznode{m-3-1}{0} & \tikznode{m-3-2}{0} & \tikznode{m-3-3}{1}
\end{pmatrix}\;.
\end{equation}
\tikz[overlay,remember picture]{%
\node[blue,dash pattern=on 2pt off 1.25pt, thick,box around=(m-1-1)(m-3-1)];
\node[red,dash pattern=on 2pt off 1.25pt, thick,box around=(m-2-3)];
\node[purple,dash pattern=on 2pt off 1.25pt, thick,box around=(m-3-2)(m-3-3)];}
\end{document}
当然,您也可以将其包装成宏,即以下产生相同的输出。
\documentclass{article}
\usepackage{amsmath,bbm}
\usepackage{tikz}
\usetikzlibrary{fit}
\newcommand{\tikznode}[2]{\relax
\ifmmode%
\tikz[remember picture,baseline=(#1.base),inner sep=0pt] \node (#1) {$#2$};
\else
\tikz[remember picture,baseline=(#1.base),inner sep=0pt] \node (#1) {#2};%
\fi}
\tikzset{box around/.style={
draw,rounded corners,
inner sep=2pt,outer sep=0pt,
node contents={},fit=#1
},
}
\newcommand{\BoxAround}[2][]{
\tikz[overlay,remember picture]{%
\node[blue,dash pattern=on 2pt off 1.25pt,thick,#1,box around=#2];}}
\begin{document}
\begin{equation}
\mathbbm{1}_3=\begin{pmatrix}
\tikznode{m-1-1}{1} & 0 & 0\\
\tikznode{m-2-1}{0} & 1 & \tikznode{m-2-3}{0}\\
\tikznode{m-3-1}{0} & \tikznode{m-3-2}{0} & \tikznode{m-3-3}{1}
\end{pmatrix}\;.
\end{equation}
\BoxAround{(m-1-1)(m-3-1)}
\BoxAround[red]{(m-2-3)}
\BoxAround[purple]{(m-3-2)(m-3-3)}
\end{document}
答案4
另一个解决方案是nicematrix
。
\documentclass{article}
\usepackage{nicematrix,tikz}
\begin{document}
\tikzset{every path/.append style = { draw, dashed, rounded corners } }
$\mathbf{I}_3 = \begin{bNiceMatrix}[margin=2pt]
\Block[tikz={offset=1pt,blue}]{3-1}{} 1 & 0 &
\Block[tikz={offset=1pt,red}]{2-1}{} 0 \\
0 & 1 & 0 \\
0 & \Block[tikz={offset=1pt,blue}]{1-2}{} 0 & 1 \\
\end{bNiceMatrix}$
\end{document}