[Tex/LaTex] How to insert the diagonal symbol of a matrix in latex

matrices

It is common in literature to encounter the diagonal symbol when referring to matrices. For instance, if matrix A is diagonal, it can be denoted as [\A] where the "\" symbol is crossing the letter "A" from the upper left to the lower right corner.

Is there any way to do this in LaTeX (maybe by the use of any particular package)?

An example of what I am intending to do can be seen on the picture below.

enter image description here

Best Answer

Here's one option using the tikzmark library to define a command (using the optional argument one can control the vertical size):

enter image description here

The code:

\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{tikzmark}

\newcounter{mydiagm}

\newcommand\DiagMat[2][1]{
\stepcounter{mydiagm}
\begin{bmatrix}
\tikzmark{start-\themydiagm}\rule{1cm}{0pt}\\[#1\normalbaselineskip]\hfill\tikzmark{end-\themydiagm}
\end{bmatrix}
\tikz[remember picture,overlay]
\draw ([yshift=1ex]pic cs:start-\themydiagm) -- node[fill=white,inner sep=2pt] {$#2$} (pic cs:end-\themydiagm);%
}
\begin{document}

\begin{equation*}
\DiagMat{c^{2}}\frac{\partial C_{l}}{\partial C_{T}}
\end{equation*}

\begin{equation*}
\DiagMat[2]{ce}
\begin{bmatrix}
a \\ b \\ c \\ d
\end{bmatrix}
\end{equation*}

\end{document}

The code needs two or three runs for the rule to reach its final position.