TikZ:绘制三角块矩阵

TikZ:绘制三角块矩阵

我试图绘制像图中这样的矩阵,但成功率极低。

在此处输入图片描述

到目前为止我尝试过的代码如下

\documentclass[letterpaper,11pt]{article}

\usepackage{tikz}
\usepackage{ifthen}
\usetikzlibrary{matrix}
\usetikzlibrary{decorations.pathreplacing}
\begin{document}
\begin{tikzpicture}
\matrix[matrix of math nodes,
left delimiter=(,
right delimiter=),
nodes in empty cells] (m)
{
    &   &   &   &   &  \\
    &   &   &   &   &  \\
    &   &   &   &   &  \\
    &   &   &   &   &  \\
    &   &   &   &   &  \\
    &   &   &   &   &  \\
};
\fill[yellow!30] (m.north west) |- (m-6-6.south east);
\fill[yellow!30] (m.north west) -| (m-6-6.south east);
\fill[red!30] (m.north west) -| (m-4-4.south east);
\fill[red!30] (m.north west) |- (m-4-4.south east);
\draw[line width=3mm,white]  (m.north west) -- (m-6-6.south east);

\draw[thick,-] (m-1-1.north west) -- (m-6-6.south east);
\node [fill=white, outer sep=0,inner sep=1,minimum size=3] at (m-1-1.north west) {0};
\node [fill=white, outer sep=0,inner sep=1,minimum size=10] at  (m-6-6.south east) {0};
\tikzstyle{myedgestyle} = [decorate, decoration={brace}](m-1-1.north west) -- (m-1-6.north east);

\end{tikzpicture}
\end{document}

输出结果如下:

在此处输入图片描述

我感觉这次尝试不会有任何好的结果,而且由于某种原因,我没有戴上牙套。

对于我该如何进行您有什么建议吗?

答案1

您可以使用相对于节点的点来绘制扇区,甚至留出空间让分隔符在不同距离处可见,您可以取消注释第 30 行以查看相对于节点界定坐标的节点。我不知道这是否是最实用的,但它是一种适应,可以达到您想要的效果。

在此处输入图片描述

梅威瑟:

\documentclass[tikz,border=14pt]{standalone}
\usepackage{tikz}  
\usetikzlibrary{matrix,shapes,decorations.pathreplacing}

\begin{document}
    \begin{tikzpicture}[
        %Global config
        >=latex,
        line width=1pt,
        %Styles
        Brace/.style={
            decorate,
            decoration={
                brace,
                raise=-7pt
            }
        }
    ]

    \matrix[% General option for all nodes
        matrix of nodes,
        text height=2.5ex,
        text depth=0.75ex,
        text width=3.25ex,
        align=center,
        left delimiter=(,
        right delimiter= ),
        column sep=5pt,
        row sep=5pt,
        %nodes={draw=black!10}, % Uncoment to see the square nodes.
        nodes in empty cells,
    ] at (0,0) (M){ % Matrix contents  
    &   \huge 0&   &   &   &   &  \\
    &   &   &   &   &   &  \\
    &   &   &   &   &   &  \\
    &   &   &   &   &   &  \\
    &   &   &   &   &   &  \\
    &   &   &   &   &   & \huge 0 \\
    };
% Drawing the sectors using matrix coordinate names.    
    \draw[thick] (M-1-2) -- (M-6-7);
    \draw[thick,fill=red!30,draw] (M-1-3.center)
    -- (M-4-6.center)
    -- (M-1-6.center)
    -- cycle;
    \draw[thick,fill=red!30,draw] (M-2-2.center)
    -- (M-5-2.center)
    -- (M-5-5.center)
    -- cycle;
    \draw[thick,fill=yellow!30,draw](M-1-6.east)
    -- (M-1-7.center)
    -- (M-5-7.center)
    -- (M-4-6.south east)
    -- cycle;
    \draw[thick,fill=yellow!30,draw](M-5-2.south)
    -- (M-5-5.south east)
    -- (M-6-6.center)
    -- (M-6-2.center)
    -- cycle;
% Drawing the braces.   
    \draw[Brace] (M-1-3.north)
    -- (M-1-6.north)
    node[midway,above]{$x-y$};
    \draw[Brace] (M-1-6.north east)
    -- (M-1-7.north)
    node[midway,above]{$y$};

    \draw[Brace] (M-5-2.west)
    -- (M-2-2.west)
    node[midway,left]{$x-y$};

    \draw[Brace] (M-6-2.west)
    -- (M-5-2.south west)
    node[midway,left]{$y$};
% Labeling the sectors  
    \node at (M-4-3){\sf A};
    \node at (M-2-5){\sf A};
    \draw (M-3-7)++(-8pt,0) node {\sf B};
    \draw (M-6-4)++(0,8pt) node {\sf B};

    \end{tikzpicture}
\end{document}

答案2

您已经加载了相关的库,甚至已经有了可以实现所需功能的样式。您只需要使用它们。

\documentclass[letterpaper,11pt]{article}

\usepackage{tikz}
\usepackage{ifthen}
\usetikzlibrary{matrix}
\usetikzlibrary{decorations.pathreplacing}
\begin{document}
\begin{tikzpicture}
\matrix[matrix of math nodes,
left delimiter=(,
right delimiter=),
nodes in empty cells] (m)
{
    &   &   &   &   &  \\
    &   &   &   &   &  \\
    &   &   &   &   &  \\
    &   &   &   &   &  \\
    &   &   &   &   &  \\
    &   &   &   &   &  \\
};
\fill[yellow!30] (m.north west) |- (m-6-6.south east);
\fill[yellow!30] (m.north west) -| (m-6-6.south east);
\fill[red!30] (m.north west) -| (m-4-4.south east);
\fill[red!30] (m.north west) |- (m-4-4.south east);
\draw[line width=3mm,white]  (m.north west) -- (m-6-6.south east);

\draw[thick,-] (m-1-1.north west) -- (m-6-6.south east);
\node [fill=white, outer sep=0,inner sep=1,minimum size=3] at (m-1-1.north west) {0};
\node [fill=white, outer sep=0,inner sep=1,minimum size=10] at  (m-6-6.south east) {0};
\tikzset{myedgestyle/.style={decorate, decoration={brace,raise=4pt}}}
\draw[myedgestyle] (m-1-2.north west) -- (m-1-4.north east)
node[midway,above=3pt,font=\tiny]{$x-y$};
\draw[myedgestyle] (m-1-5.north west) -- (m-1-6.north east)
node[midway,above=3pt,font=\tiny]{$y$};
\draw[myedgestyle] ([xshift=-8pt]m-6-1.south west) -- ([xshift=-8pt]m-5-1.north west)
node[midway,left=3pt,font=\tiny]{$y$};
\draw[myedgestyle] ([xshift=-8pt]m-4-1.south west) -- ([xshift=-8pt]m-1-1.north west)
node[midway,left=3pt,font=\tiny]{$x-y$};
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容