9

I'm trying to draw a matrix like this one in the picture with extremely low success.

enter image description here

The code of what I've tried so far is the following

\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}

which results in this output

enter image description here

I have the feeling that this attempt will not end up in anything decent, and moreover for some reason I don't manage to put the braces.

Do you have some suggestions on how I can proceed?

  • 1
    Do you need this to be a matrix? I think it is easier to draw with just plain tikz. – Jasper Habicht Jun 13 '18 at 16:27
11

You can use the points relative to the nodes to draw the sectors, even leaving space for the delimiters to be seen at different distances, you can uncomment line 30 to see the nodes that delimit the coordinates relative to the nodes. I do not know if it is the most practical but it is an adaptation that comes to resemble what you want.

enter image description here

MWE:

\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}
5

You already load the relevant libraries and even have a style that does what you want. You only need to use them.

\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}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.