# Highlighting rows/columns/diagonals in matrix

I'm trying to highlight rows/columns/diagonals in a matrix. I came across this example, which looks very clean. Unfortunately, it seems to cause some issue with spacing in the matrix, and I can't figure out why. Below is a MWE and the output:

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

\newcommand{\tikzmark}[2]{
\tikz[overlay,remember picture,baseline]
\node[anchor=base] (#1) {$#2$};
}

\begin{document}

No highlighting:
$$A = \begin{bmatrix} a_{11} & 0 & 0 \\ 0 & a_{22} & 0 \\ 0 & 0 & a_{33}\\ \end{bmatrix}$$

Highlighting:
$$A = \begin{bmatrix} \tikzmark{top}{a_{11}} & 0 & 0 \\ 0 & a_{22} & 0 \\ 0 & 0 & \tikzmark{bottom}{a_{33}}\\ \end{bmatrix}$$
\begin{tikzpicture}[overlay,remember picture]
\draw[opacity=.4,line width=3mm,line cap=round] (top.center) -- (bottom.center);
\end{tikzpicture}

\end{document}


What is the \tikzmark command doing, and is there anything that can be done to preserve the standard matrix spacing?

• You are using tikzmark not in the optimal way. Use the library of that name and its \tikzmarknode command. You can also use the nicematrix package. – user194703 Jan 23 '20 at 23:07
• Thanks, I wasn't aware of the \tikzmark package or the \tikzmarknode command. – JoeDub Jan 23 '20 at 23:41
• – Zarko Jan 23 '20 at 23:43

Here is a version with the tikzmark library (and the eso-pic package to get the highlighting on the background).

\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{tikzmark,calc}
\usepackage{eso-pic}
\begin{document}

No highlighting:
$$A = \begin{bmatrix} a_{11} & 0 & 0 \\ 0 & a_{22} & 0 \\ 0 & 0 & a_{33}\\ \end{bmatrix}$$

Highlighting:
$$A = \begin{bmatrix} \tikzmarknode[circle]{top}{a_{11}} & 0 & 0 \\ 0 & a_{22} & 0 \\ 0 & 0 & \tikzmarknode[circle]{bottom}{a_{33}}\\ \end{bmatrix}$$
\begin{tikzpicture}[overlay,remember picture]
\draw let  \p1=($(top.north)-(top.center)$),
\p2=($(bottom.north)-(bottom.center)$), \n1={2*max(\y1,\y2)-2pt} in
[opacity=.4,line width=\n1,line cap=round,
shorten >=-\y2/3,shorten <=-\y1/3] (top.center) -- (bottom.center);
\end{tikzpicture}}
\end{document}


• This is brilliant; thanks! – JoeDub Jan 23 '20 at 23:46

An adapt with variants of the nice answer of @Zarko Rectangle box around elements of matrix using nicematrix package as also suggested by the very good user @Schrödinger's cat in your comment. If you want to have more rounded corners increase the parameter rounded corners=2pt. I add a MWE and a screenshot:

\documentclass[12pt]{article}
\usepackage{nicematrix}
\begin{document}
$\begin{bNiceArray}{>{\strut}CCC}% [ margin, code-after = {\begin{tikzpicture} \node [fill=gray, rounded corners=2pt, opacity=0.2, inner xsep = 1pt, inner ysep = -.6pt, rotate fit=-27, fit = (1-1) (3-3) ] {} ; \end{tikzpicture}} ] a_{11} & 0 & 0 \\ 0 & a_{22} & 0 \\ 0 & 0 & a_{33} \end{bNiceArray}$
\end{document}

• +1! Somewhere in my brain was, that I no so long ago wrote this answer ... but I was not able to remember, when nor question title ... now I will store this my answer together with your to my barn of interesting solutions :-) – Zarko Jan 23 '20 at 23:59
• @Zarko I read your mind a often I read your and other answers. Today I have bought the crystal ball :-) :-) :-) :-) :-) – Sebastiano Jan 24 '20 at 0:04