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: \begin{equation} A = \begin{bmatrix} a_{11} & 0 & 0 \\ 0 & a_{22} & 0 \\ 0 & 0 & a_{33}\\ \end{bmatrix} \end{equation} Highlighting: \begin{equation} A = \begin{bmatrix} \tikzmark{top}{a_{11}} & 0 & 0 \\ 0 & a_{22} & 0 \\ 0 & 0 & \tikzmark{bottom}{a_{33}}\\ \end{bmatrix} \end{equation} \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?



tikzmarknot in the optimal way. Use the library of that name and its\tikzmarknodecommand. You can also use thenicematrixpackage.