# Drawing some specific shapes on a matrix using Tikz

I am new to using Tikz and, I have a matrix which is the sum of three matrices L, D, U. To illustrate these parts, I would like to generate the following image using Tikz:

How can I do that, any help with this would be greatly appreciated!

MWE:

\documentclass{report}
\usepackage{amsmath}
\begin{document}
$M=L+D+U = \begin{bmatrix} a & b & c\\ d & e & f\\ g & h & i \end{bmatrix}$
\end{document}

• – Sigur Aug 11 '18 at 17:16
• Could you please add an MWE with your matrix, without the shapes? – CarLaTeX Aug 11 '18 at 17:19

## 1 Answer

Here is a proposal. It is essentially quite straightforward in that you simply create a TikZ matrix in the usual way, and then draw the coloured lines using the coordinates of the nodes. To ensure the green line doesn't lie over of the red and blue lines, I've adjusted the coordinates it uses by 0.5mm, this is controlled by a variable I've saved as \offset.

Additionally, I've set a text width for the nodes and used align=center. This helps with the placement of the outer square brackets around the matrix.

## Code

\documentclass[margin=0.5cm]{standalone}

\usepackage{tikz}
\usetikzlibrary{matrix,decorations.pathreplacing}
\pgfkeys{tikz/mymatrixenv/.style={decoration=brace,every left delimiter/.style={xshift=4pt},every right delimiter/.style={xshift=-4pt}}}
\pgfkeys{tikz/mymatrix/.style={matrix of math nodes,left delimiter=[,right delimiter={]},inner sep=1pt,row sep=0em,column sep=0em,nodes={inner sep=6pt}}}

\begin{document}

\begin{tikzpicture}[baseline=0cm,mymatrixenv]
\matrix [mymatrix,text width=0.6em,align=center] (m)
{
a & b & c \\
d & e & f \\
g & h & i \\
};
\pgfmathsetmacro{\offset}{0.5mm}
\draw [thick,blue] (m-1-1.west) |- (m-3-3.south) -- cycle;
\draw [thick,red] (m-1-1.north) -| (m-3-3.east) -- cycle;
\draw [thick,green,rounded corners=1mm] ([yshift=\offset]m-1-1.west) -- ([xshift=-\offset]m-1-1.north) -- ([yshift=-\offset]m-3-3.east) -- ([xshift=\offset]m-3-3.south) -- cycle;
\end{tikzpicture}

\end{document}


As proposed by @Sigur, the triangles could also be rounded:

• Would be nice to use rounded corners also on triangles. – Sigur Aug 11 '18 at 17:43