我拿了一个代码并修改了它。但是,我无法正确更改它。输出是顶部矩阵,而所需的矩阵在下面(绿色上三角)。如何才能正确完成?
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix,fit}
\begin{document}
\pgfdeclarelayer{background}
\pgfsetlayers{background,main}
\begin{tikzpicture}[baseline={([yshift=-1ex]current bounding box.center)}]
\matrix[matrix of math nodes,left delimiter={[},right delimiter={]},row sep=10pt,column sep=10pt] (m) {
1 &3 &-8 & 5\\
2 &0 &1& 7\\
7 &9 &1& 2\\
7 &9 &1& 2\\
};
\begin{pgfonlayer}{background}
\node[inner sep=3pt,fit=(m-1-1)] (1) {};
\node[inner sep=3pt,fit=(m-1-2) (m-2-3) (m-3-4)] (2) {};
\node[inner sep=3pt,fit=(m-4-4)] (3) {};
\draw[rounded corners,dotted,fill=green!80!white,inner sep=3pt,fill opacity=0.1] (1.north west) -- (2.north east) |- (3.south west) |- (2.south west) |- (1.south west) -- cycle;
\end{pgfonlayer}
\end{tikzpicture}
\end{document}
答案1
我不知道为什么我无法编译你的代码,在我看来,它里面有一些罕见的字符(inputtenc Error: Unicode character ...),但是我使用另一个框架。
这里有 MWE 的三个例子:绿色表示使用矩阵中定义的节点;红色表示根据您发送的代码,您可以在其中看到错误;蓝色表示如何通过您发送的代码方法获得您想要的东西......
\documentclass[tikz,border=14pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix,shapes,decorations.pathreplacing,fit,backgrounds}
\begin{document}
\begin{tikzpicture}[
%Styles
Matrix/.style={
matrix of nodes,
text height=2.5ex,
text depth=0.75ex,
text width=3.25ex,
align=center,
left delimiter={[},
right delimiter={]},
column sep=0pt,
row sep=0pt,
%nodes={draw=black!10}, % Uncoment to see the square nodes.
nodes in empty cells,
},
DA/.style={
fill=green!10,
dotted,
line width=0.7pt,
rounded corners=5pt,
}
]
\matrix[Matrix] at (0,0) (M){ % Matrix contents
1 & 3 & -8 & 5 \\
2 & 0 & 1 & 7 \\
7 & 9 & 1 & 2 \\
7 & 9 & 1 & 2 \\
};
\matrix[Matrix,nodes={draw=black!10}] at (4,0) (M1){ % Matrix contents
1 & 3 & -8 & 5 \\
2 & 0 & 1 & 7 \\
7 & 9 & 1 & 2 \\
7 & 9 & 1 & 2 \\
};
\matrix[Matrix] at (8,0) (M2){ % Matrix contents
1 & 3 & -8 & 5 \\
2 & 0 & 1 & 7 \\
7 & 9 & 1 & 2 \\
7 & 9 & 1 & 2 \\
};
\begin{scope}[on background layer] %Allows to draw in background layer.
%FOR MATRIX M
%To delimit area groups using matrix nodes
\draw[DA](M-1-1.north west)
-| (M-4-4.south east)
-- (M-4-4.south west)
|- (M-3-3.south west)
|- (M-2-2.south west)
|- (M-1-1.south west)
-- cycle;
% Method for your example
% In red some mistakes
% Fit creates squared nodes
%\node[styles](ID){Text};
\node[inner sep=2pt,draw,red,fit=(M1-1-1)](fit1){};
\node[inner sep=2pt,draw,red,fit=(M1-1-2)(M1-2-3)(M1-3-4)](fit2){};
\node[inner sep=2pt,draw,red,fit=(M1-4-4)](fit3){};
%If you trace the draw:
\draw[DA,fill=red,fill opacity=0.2](fit1.north west)
-- (fit2.north east)
|- (fit3.south west)
|- (fit2.south west)
|- (fit1.south west)
-- cycle;
% Correcting using the method you use
\node[inner sep=2pt,draw,blue,fit=(M2-1-1)](n1){};
\node[inner sep=2pt,draw,blue,fit=(M2-2-2)](n2){};
\node[inner sep=2pt,draw,blue,fit=(M2-3-3)](n3){};
\node[inner sep=2pt,draw,blue,fit=(M2-4-4)](n4){};
%If you trace the draw:
\draw[DA,fill=blue,fill opacity=0.2](n1.north west)
-| (n4.south east)
|- (n4.south west)
|- (n3.south west)
|- (n2.south west)
|- (n1.south west)
-- cycle;
\end{scope}
\end{tikzpicture}
\end{document}