我有一个矩阵,如以下代码片段所示:
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage{blkarray}
\begin{document}
\begin{equation}
\begin{blockarray}{ccccccccccc}
& \O & 12 & 12,34 & 13 & 13,24 & 14 & 14,23 & 23 & 24 & 34 \\
\begin{block}{c[cccccccccc]}
01 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 1 & 1 & 1 \\
01,23 & 1 & 1 & 0 & 1 & 0 & 0 & 0 & 1 & 1 & 1 \\
01,24 & 1 & 1 & 0 & 0 & 0 & 1 & 0 & 1 & 1 & 1 \\
01,34 & 1 & 0 & 0 & 1 & 0 & 1 & 0 & 1 & 1 & 1 \\
\end{block}
\end{blockarray}
\end{equation}
\end{document}
我希望矩阵的前 7 列周围有一个绿色框,其余 3 列周围有一个红色框。我尝试实施给出的解决方案这帖子,但我认为在 TikZ 内部使用 blkarray 会导致问题。
还有其他方法吗?或者有没有可以使用的 TikZ 的方法?
答案1
和tikz
...
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{backgrounds, fit, matrix}
\begin{document}
\begin{equation}
\begin{tikzpicture}[baseline,
every label/.append style={label distance=6pt, inner sep=0pt}
]
\matrix (m) [matrix of nodes,
nodes={minimum width=2em, minimum height=3ex,
text depth=2pt, inner xsep=1pt, anchor=base},
column sep=4pt,
row sep=0pt,
]
{|[label=left:01,
label=$\emptyset$]| 1 & |[label=12]| 1
& |[label={12,34}]| 0
& |[label=13]| 0
& |[label={13,24}]| 0
& |[label=14]| 0
& |[label={14,23}]| 0
& |[label=23]| 1
& |[label=24]| 1
& |[label=34]| 1 \\
|[label=left:{01,23}]| 1 & 1 & 0 & 1 & 0 & 0 & 0 & 1 & 1 & 1 \\
|[label=left:{01,24}]| 1 & 1 & 0 & 0 & 0 & 1 & 0 & 1 & 1 & 1 \\
|[label=left:{01,34}]| 1 & 0 & 0 & 1 & 0 & 1 & 0 & 1 & 1 & 1 \\
};
\draw (m-1-1.north west) + ( 3pt,0) -| (m-4-1.south west) -- + ( 3pt,0);
\draw (m-1-9.north east) + (-3pt,0) -| (m-4-9.south east) -- + (-3pt,0);
%
\node[draw=red, inner sep=1pt, fit={([xshift=5pt] m-1-1.north west) (m-4-6)}] {};
\node[draw=green, inner sep=1pt, fit={(m-1-7) ([xshift=-5pt] m-4-9.south east)}] {};
\end{tikzpicture}
\end{equation}
\end{document}
答案2
使用 很简单pstricks
:将框架的对立顶点定义为\pnode
矩阵中相关位置的空节点(命令),然后使用 命令\psframe
。我还对您的矩阵进行了一些改进:
\documentclass[svgnames]{article}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage{amssymb}
\usepackage{blkarray, array, bigstrut, makebox}
\usepackage{pst-node}
\usepackage{auto-pst-pdf}
\begin{document}
\begin{postscript}
\psset{linejoin=1}
\begin{equation}\setlength{\BAextrarowheight}{3pt}\bigstrutjot = 4pt
\begin{blockarray}{cr*{9}{c}}
& \varnothing & 12 & \makebox*{12}{12{,}34} & 13 & \makebox*{12}{13{,}24} & 14 & \makebox*{12}{14{,}23} & 23 & 24 & 34 \\
\begin{block}{c[r*{9}{c}]}
01 &\enspace \pnode[-1ex, 2.1ex]{A} 1 & 0 & 0 & 0 & 0 & 0 & 0 & \pnode[-1ex, 2.1ex]{C} 1 & 1 & 1 \bigstrut[t]\\
01{,}23 & 1 & 1 & 0 & 1 & 0 & 0 & 0 & 1 & 1 & 1 \\
01{,}24 & 1 & 1 & 0 & 0 & 0 & 1 & 0 & 1 & 1 & 1 \\
01{,}34 & 1 & 0 & 0 & 1 & 0 & 1 & 0\pnode[1ex, -0.6ex]{B} & 1 & 1 & 1 \pnode[1ex, -0.6ex]{D}\bigstrut[b]\enspace \\
\end{block}
\end{blockarray}
\end{equation}
\psframe[linecolor=IndianRed](A)(B)
\psframe[linecolor=DarkSeaGreen](C)(D)
\end{postscript}
\end{document}
答案3
根据这些示例的选项:在 tikz 中突出显示矩阵中的元素,彩色上三角矩阵。
结果:
梅威瑟:
\documentclass[tikz,border=14pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix,shapes,decorations.pathreplacing,fit,backgrounds}
\begin{document}
\begin{tikzpicture}[
%Global config
Highlight/.style={
draw,
fill,
rounded corners=2pt,
fill opacity=0.05,
},
Delimiter/.style={
left delimiter=[,
right delimiter=],
inner xsep=-1pt,
%inner ysep=-1pt,
}
]
\matrix[matrix of nodes,align=center,nodes in empty cells,] at (0,0) (M){ % Matrix contents
& \O & 12 & 12,34 & 13 & 13,24 & 14 & 14,23 & 23 & 24 & 34 \\
01 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 1 & 1 & 1 \\
01,23 & 1 & 1 & 0 & 1 & 0 & 0 & 0 & 1 & 1 & 1 \\
01,24 & 1 & 1 & 0 & 0 & 0 & 1 & 0 & 1 & 1 & 1 \\
01,34 & 1 & 0 & 0 & 1 & 0 & 1 & 0 & 1 & 1 & 1 \\
};
\begin{scope}[on background layer]
%FOR MATRIX M
%To delimit internal braces
\node[Delimiter,fit=(M-2-2)(M-5-11)](Internal){};
%To delimit internal area groups
\draw[Highlight,red](M-5-2.south west) rectangle (M-2-8.north -| M-1-8.east);
\draw[Highlight,green](M-5-9.south west) rectangle (M-2-11.north east);
\end{scope}
\end{tikzpicture}
\end{document}