TeX - LaTeX Asked by JoeDub on April 7, 2021
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?
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}
Correct answer by user194703 on April 7, 2021
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,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}
Answered by Sebastiano on April 7, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP