2

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}

MWE output

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

  • 1
    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
  • 1
    See tex.stackexchange.com/questions/522507/… – Zarko Jan 23 '20 at 23:43
2

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:
\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}
    \tikzmarknode[circle]{top}{a_{11}} & 0 & 0 \\
    0 & a_{22} & 0 \\
    0 & 0 & \tikzmarknode[circle]{bottom}{a_{33}}\\
  \end{bmatrix}
\end{equation}
\AddToShipoutPictureBG*{%
\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}

enter image description here

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

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:

enter image description here

\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
    +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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.