# Coloured half lines in a matrix

I was wondering how I could create this matrix in LaTeX:

I know how to do the matrix part but my question is the lines, how can I make them stop in (1,1), (2,2) and (3,3)?

• I'd give nicematrix a try, since it is build using tikz this should not be a problem. May 31 at 11:50
• It would help people if you could edit into your question some code that produces the parts you know how to do, it is fine if you don't know how to add the colour lines (that's what this site is for) but it helps people help you when there is a starting point beginning with \documentclass{...} and ending with \end{document} May 31 at 12:06

Here is a possibility with nicematrix and tikz.

\documentclass{article}

\usepackage{nicematrix, tikz}

\begin{document}

$\NiceMatrixOptions{cell-space-limits = 2pt} \begin{pNiceMatrix}[first-col] \color{red}\Delta_1 & \frac{\partial^2f}{\partial x_1^2} & \frac{\partial^2f}{\partial x_1^2} & \frac{\partial^2f}{\partial x_1^2}\\ \color{green}\Delta_2 & \frac{\partial^2f}{\partial x_1^2} & \frac{\partial^2f}{\partial x_1^2} & \frac{\partial^2f}{\partial x_1^2}\\ \color{blue}\Delta_3 & \frac{\partial^2f}{\partial x_1^2} & \frac{\partial^2f}{\partial x_1^2} & \frac{\partial^2f}{\partial x_1^2} \CodeAfter \tikz{ \draw[red, shorten >=-3mm] (1-|2) -- (2-|2) -- (2-|1); \draw[green, shorten >=-3mm] (1-|3) -- (3-|3) -- (3-|1); \draw[blue, shorten >=-3mm] (1-|4) -- (4-|4) -- (4-|1); } \end{pNiceMatrix}$

\end{document}

• Thanks, this is wonderful !!! May 31 at 12:15
• And how did it make it look squared, before my matrix was more of a rectangle, i think it is related to NIceMatrix i will have a look May 31 at 12:20
• \NiceMatrixOptions{cell-space-limits = 2pt} adjusts vertical space between rows. May 31 at 12:27