我正在尝试突出显示矩阵中的行/列/对角线。我遇到了这个例子,看起来非常干净。不幸的是,它似乎导致矩阵中的间距出现一些问题,我不知道为什么。下面是 MWE 和输出:
\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}
\tikzmark 命令在做什么,有什么可以做的来保留标准矩阵间距?
答案1
这是一个带有tikzmark
库的版本(以及eso-pic
用于在背景上突出显示的包)。
\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}
答案2
改编自@Zarko 的精彩答案矩阵元素周围的矩形框使用nicematrix
包,正如非常好的用户 @Schrödinger's cat 在您的评论中所建议的那样。如果您想要有更多圆角,请增加参数rounded corners=2pt
。我添加了 MWE 和屏幕截图:
\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}