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?

3
  • 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. Commented Jan 23, 2020 at 23:07
  • Thanks, I wasn't aware of the \tikzmark package or the \tikzmarknode command. Commented Jan 23, 2020 at 23:41
  • 1
    See tex.stackexchange.com/questions/522507/… Commented Jan 23, 2020 at 23:43

2 Answers 2

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

0
2

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,tikz} \usetikzlibrary{fit} \begin{document} \[ \begin{bNiceArray}{>{\strut}ccc}[margin] a_{11} & 0 & 0 \\ 0 & a_{22} & 0 \\ 0 & 0 & a_{33} \CodeAfter \tikz \node [fill=gray, rounded corners=2pt, opacity=0.2, rotate fit=-31, inner xsep=1.2pt, inner ysep = -0.8pt, fit = (1-1) (3-3)] {} ; \end{bNiceArray} \] \end{document} 
2
  • 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 :-) Commented Jan 23, 2020 at 23:59
  • @Zarko I read your mind a often I read your and other answers. Today I have bought the crystal ball :-) :-) :-) :-) :-) Commented Jan 24, 2020 at 0:04

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.