我想围绕大矩阵的某些子矩阵绘制框架,如下问题所述:突出显示矩阵中的元素。
我尝试了一些提供的解决方案并最终使用了这个:
\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz}
\usepackage{color}
\definecolor{orange}{RGB}{255,127,0}
\usetikzlibrary{arrows,matrix,positioning}
\usepackage{xfrac}
\begin{document}
\begin{tikzpicture}
\matrix [matrix of math nodes,left delimiter=(,right delimiter=)] (m)
{
1 & 0 & 0 & 0 & 0 & 0 & 0\\
0 & 1 & 0 & 0 & 0 & 0 & 0\\
0 & \frac{1}{2} & \frac{1}{2} & 0 & 0 & 0 & 0\\
0 & \frac{1}{4} & \frac{7}{12} & \frac{1}{6} & 0 & 0 & 0\\
0 & 0 & \frac{2}{3} & \frac{1}{3} & 0 & 0 & 0\\
0 & 0 & \frac{1}{3} & \frac{2}{3} & 0 & 0 & 0\\
0 & 0 & \frac{1}{6} & \frac{2}{3} & \frac{1}{6} & 0 & 0\\
0 & 0 & 0 & \frac{2}{3} & \frac{1}{3} & 0 & 0\\
0 & 0 & 0 & \frac{1}{3} & \frac{2}{3} & 0 & 0\\
0 & 0 & 0 & \frac{1}{6} & \frac{7}{12} & \frac{1}{4} & 0\\
0 & 0 & 0 & 0 & \frac{1}{2} & \frac{1}{2} & 0\\
0 & 0 & 0 & 0 & 0 & 1 & 0\\
0 & 0 & 0 & 0 & 0 & 0 & 1\\
};
\draw[color=orange,line width=1pt] (m-1-1.north west) -- (m-1-4.north east) -- (m-4-4.south east) -- (m-4-1.south west) -- cycle;
\draw[color=orange,line width=1pt] (m-4-2.north west) -- (m-4-5.north east) -- (m-7-5.south east) -- (m-7-2.south west) -- cycle;
\draw[color=orange,line width=1pt] (m-7-3.north west) -- (m-7-6.north east) -- (m-10-6.south east) -- (m-10-3.south west) -- cycle;
\draw[color=orange,line width=1pt] (m-10-4.north west) -- (m-10-7.north east) -- (m-13-7.south east) -- (m-13-4.south west) -- cycle;
\end{tikzpicture}
\end{document}
问题是,当某一行中同时存在普通数字和分数时,框架会出现如下偏差:
我该如何修复这个问题?我尝试用 替换每个\frac
(\sfrac
这就是我添加包的原因xfrac
),但这使问题更加严重。
答案1
随着fit
。(更新)我添加了一种新风格rec
。
\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz}
\usepackage{color}
\definecolor{orange}{RGB}{255,127,0}
\usetikzlibrary{arrows,matrix,positioning,fit}
\usepackage{xfrac}
\begin{document}
\begin{tikzpicture}[rec/.style={draw=orange,thick,inner sep=0}]
\matrix [matrix of math nodes,left delimiter=(,right delimiter=)] (m)
{
1 & 0 & 0 & 0 & 0 & 0 & 0\\
0 & 1 & 0 & 0 & 0 & 0 & 0\\
0 & \sfrac{1}{2} & \sfrac{1}{2} & 0 & 0 & 0 & 0\\
0 & \sfrac{1}{4} & \sfrac{7}{12} & \sfrac{1}{6} & 0 & 0 & 0\\
0 & 0 & \sfrac{2}{3} & \sfrac{1}{3} & 0 & 0 & 0\\
0 & 0 & \sfrac{1}{3} & \sfrac{2}{3} & 0 & 0 & 0\\
0 & 0 & \sfrac{1}{6} & \sfrac{2}{3} & \sfrac{1}{6} & 0 & 0\\
0 & 0 & 0 & \sfrac{2}{3} & \sfrac{1}{3} & 0 & 0\\
0 & 0 & 0 & \sfrac{1}{3} & \sfrac{2}{3} & 0 & 0\\
0 & 0 & 0 & \sfrac{1}{6} & \sfrac{7}{12} & \sfrac{1}{4} & 0\\
0 & 0 & 0 & 0 & \sfrac{1}{2} & \sfrac{1}{2} & 0\\
0 & 0 & 0 & 0 & 0 & 1 & 0\\
0 & 0 & 0 & 0 & 0 & 0 & 1\\
};
\node[fit=(m-1-1)(m-4-4), rec] {};
\node[fit=(m-4-2)(m-7-5), rec] {};
\node[fit=(m-7-3)(m-10-6), rec] {};
\node[fit=(m-10-4)(m-13-7),rec] {};
\end{tikzpicture}
\end{document}
答案2
测试版软件包matrixcells
早餐吃这种东西。这是您的代码,略有改动 - 我很想将其设置为原始代码和此代码之间的“发现差异”。
\documentclass{article}
%\url{http://tex.stackexchange.com/q/53938/86}
\usepackage{amsmath}
\usepackage{tikz}
\usepackage{matrixcells}
\usepackage{color}
\definecolor{orange}{RGB}{255,127,0}
\usetikzlibrary{arrows,matrix,positioning}
\usepackage{xfrac}
\begin{document}
\begin{tikzpicture}
\matrix [matrix of math nodes,left delimiter=(,right delimiter=),label cells] (m)
{
1 & 0 & 0 & 0 & 0 & 0 & 0\\
0 & 1 & 0 & 0 & 0 & 0 & 0\\
0 & \frac{1}{2} & \frac{1}{2} & 0 & 0 & 0 & 0\\
0 & \frac{1}{4} & \frac{7}{12} & \frac{1}{6} & 0 & 0 & 0\\
0 & 0 & \frac{2}{3} & \frac{1}{3} & 0 & 0 & 0\\
0 & 0 & \frac{1}{3} & \frac{2}{3} & 0 & 0 & 0\\
0 & 0 & \frac{1}{6} & \frac{2}{3} & \frac{1}{6} & 0 & 0\\
0 & 0 & 0 & \frac{2}{3} & \frac{1}{3} & 0 & 0\\
0 & 0 & 0 & \frac{1}{3} & \frac{2}{3} & 0 & 0\\
0 & 0 & 0 & \frac{1}{6} & \frac{7}{12} & \frac{1}{4} & 0\\
0 & 0 & 0 & 0 & \frac{1}{2} & \frac{1}{2} & 0\\
0 & 0 & 0 & 0 & 0 & 1 & 0\\
0 & 0 & 0 & 0 & 0 & 0 & 1\\
};
\draw[color=orange,line width=1pt] (m-cell-1-1.north west) -- (m-cell-1-4.north east) -- (m-cell-4-4.south east) -- (m-cell-4-1.south west) -- cycle;
\draw[color=orange,line width=1pt] (m-cell-4-2.north west) -- (m-cell-4-5.north east) -- (m-cell-7-5.south east) -- (m-cell-7-2.south west) -- cycle;
\draw[color=orange,line width=1pt] (m-cell-7-3.north west) -- (m-cell-7-6.north east) -- (m-cell-10-6.south east) -- (m-cell-10-3.south west) -- cycle;
\draw[color=orange,line width=1pt] (m-cell-10-4.north west) -- (m-cell-10-7.north east) -- (m-cell-13-7.south east) -- (m-cell-13-4.south west) -- cycle;
\end{tikzpicture}
\end{document}
结果如下:
matrixcells 包将矩形节点放置在 TikZ 矩阵的每个单元上,这样它们就可以平铺矩阵。这确保了它们的边缘对齐等等。因此,它们比单个单元本身更适合勾勒矩阵的区域。例如,如果其中一个非角节点恰好特别高,那么 Altermundus 的代码将无法适应这种情况,而上面的代码可以。以下是 Altermundus 的代码生成橙色线条和我的代码生成绿色线条的比较。
答案3
提供的两个答案都有一定的局限性。如以下 MWE 的变体所示:
\documentclass[tikz]{standalone}
%\url{https://tex.stackexchange.com/q/53938/86}
\usepackage{amsmath}
\usepackage{matrixcells}
\usetikzlibrary{matrix, fit}
\usepackage{xfrac}
\begin{document}
\begin{tikzpicture}
\matrix [matrix of math nodes,left delimiter=(,right delimiter=),label cells, nodes={anchor=north}] (m)
{
1 & 0 & 0 & 0 & 0 & 0 & 0\\
0 & 1 & 0 & 0 & 0 & 0 & 0\\
0 & \frac{1}{2} & \frac{1}{2} & 0 & 0 & 0 & 0\\
0 & \frac{1}{4} & \frac{7}{12} & \frac{1}{6} & 0 & 0 & 0\\
1000000 & 0 & \frac{2}{3} & \frac{1}{3} & 0 & 0 & 0\\
0 & 0 & \frac{1}{3} & \frac{2}{3} & 0 & 0 & 0\\
0 & 0 & \frac{1}{6} & \frac{2}{3} & \frac{1}{6} & 0 & 0\\
0 & 0 & 0 & \frac{2}{3} & \frac{1}{3} & 0 & 0\\
0 & 0 & 0 & \frac{1}{3} & \frac{2}{3} & 0 & 0\\
0 & 0 & 0 & \frac{1}{6} & \frac{7}{12} & \frac{1}{4} & 0\\
0 & 0 & 0 & 0 & \frac{1}{2} & \frac{1}{2} & 0\\
0 & 0 & 0 & 0 & 0 & 1 & 0\\
0 & 0 & 0 & 0 & 0 & 0 & 1\\
};
\draw[color=orange,line width=1pt] (m-cell-1-1.north west) -- (m-cell-1-4.north east) -- (m-cell-4-4.south east) -- (m-cell-4-1.south west) -- cycle;
\draw[color=orange,line width=1pt] (m-cell-4-2.north west) -- (m-cell-4-5.north east) -- (m-cell-7-5.south east) -- (m-cell-7-2.south west) -- cycle;
\draw[color=orange,line width=1pt] (m-cell-7-3.north west) -- (m-cell-7-6.north east) -- (m-cell-10-6.south east) -- (m-cell-10-3.south west) -- cycle;
\draw[color=orange,line width=1pt] (m-cell-10-4.north west) -- (m-cell-10-7.north east) -- (m-cell-13-7.south east) -- (m-cell-13-4.south west) -- cycle;
\node[draw=green, fit=(m-1-1)(m-4-4)] {};
\node[draw=green, fit=(m-4-2)(m-7-5)] {};
\node[draw=green, fit=(m-7-3)(m-10-6)] {};
\node[draw=green, fit=(m-10-4)(m-13-7)] {};
\end{tikzpicture}
\end{document}
绿色框对应@AlainMatthes 的答案,橙色框对应@AndrewStacey 的答案。
由于锚点的改变,橙色轮廓线偏离了(完整解释见这个答案)。
绿色轮廓不准确,因为轮廓的尺寸不能仅由其包含的节点确定。这是 @AndrewStacey 关于在 中包含哪些节点的评论的扩展fit
。
正如所提到的同样的答案,我已经修复了锚点问题matrixcells
了矩阵.骨架。以下示例以橙色显示正确的轮廓:
\documentclass[tikz]{standalone}
%\url{https://tex.stackexchange.com/q/53938/86}
\usepackage{amsmath}
\usetikzlibrary{matrix.skeleton}
\usepackage{xfrac}
\begin{document}
\begin{tikzpicture}
\matrix [matrix of math nodes,left delimiter=(,right delimiter=),label skeleton, nodes={anchor=north}] (m)
{
1 & 0 & 0 & 0 & 0 & 0 & 0\\
0 & 1 & 0 & 0 & 0 & 0 & 0\\
0 & \frac{1}{2} & \frac{1}{2} & 0 & 0 & 0 & 0\\
0 & \frac{1}{4} & \frac{7}{12} & \frac{1}{6} & 0 & 0 & 0\\
1000000 & 0 & \frac{2}{3} & \frac{1}{3} & 0 & 0 & 0\\
0 & 0 & \frac{1}{3} & \frac{2}{3} & 0 & 0 & 0\\
0 & 0 & \frac{1}{6} & \frac{2}{3} & \frac{1}{6} & 0 & 0\\
0 & 0 & 0 & \frac{2}{3} & \frac{1}{3} & 0 & 0\\
0 & 0 & 0 & \frac{1}{3} & \frac{2}{3} & 0 & 0\\
0 & 0 & 0 & \frac{1}{6} & \frac{7}{12} & \frac{1}{4} & 0\\
0 & 0 & 0 & 0 & \frac{1}{2} & \frac{1}{2} & 0\\
0 & 0 & 0 & 0 & 0 & 1 & 0\\
0 & 0 & 0 & 0 & 0 & 0 & 1\\
};
\node[draw=orange, fit=(m-cell-1-1)(m-cell-4-4)] {};
\node[draw=orange, fit=(m-cell-4-2)(m-cell-7-5)] {};
\node[draw=orange, fit=(m-cell-7-3)(m-cell-10-6)] {};
\node[draw=orange, fit=(m-cell-10-4)(m-cell-13-7)] {};
\node[draw=green, fit=(m-1-1)(m-4-4)] {};
\node[draw=green, fit=(m-4-2)(m-7-5)] {};
\node[draw=green, fit=(m-7-3)(m-10-6)] {};
\node[draw=green, fit=(m-10-4)(m-13-7)] {};
\end{tikzpicture}
\end{document}