我有一个 3x3 eqnarray
。有没有办法用背景色突出显示对角线,也许使用 tikz?我使用 tikz 椭圆来突出显示方程项,但将椭圆拉伸到整个对角线会使它在末端太细;我也不知道如何在没有反复试验角度值的情况下旋转它。
答案1
有以下情况nicematrix
:
\documentclass{article}
\usepackage{nicematrix,tikz}
\usetikzlibrary{fit}
\begin{document}
\[
\begin{bNiceArray}{>{\strut}cccc}[margin,extra-margin = 1pt]
1 & 2 & 3 & 4 \\
1 & 2 & 3 & 4 \\
1 & 2 & 3 & 4 \\
1 & 2 & 3 & 4 \\
\CodeAfter
\begin{tikzpicture}
\node [draw=red, rounded corners=2pt, inner ysep = 0pt,
rotate fit=-38, fit = (1-1) (4-4) ] {} ;
\end{tikzpicture}
\end{bNiceArray}
\]
\end{document}
您需要多次编译(因为nicematrix
在后台使用 PGF/Tikz 节点)。
答案2
您可以使用以下内容,但我不得不承认我没有完全明白您的问题。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
%\usetikzlibrary{shapes} %Uncomment if you want additional shapes
\begin{document}
\begin{tikzpicture}
\matrix (m) [matrix of math nodes]{
| [draw,fill= yellow] | a=b & b &c\\
d &|[draw,fill= yellow]| e^2=m^2c^4 &f\\
g &h &|[draw,fill= yellow]|F = \frac{d}{dt}{(m\vec{v})}\\
};
\end{tikzpicture}
\end{document}
这导致
我希望它有帮助。
编辑:如果您还包括该shapes
库,您可以将突出显示的形状更改为椭圆等。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix,shapes}
\begin{document}
\begin{tikzpicture}
\matrix (m) [matrix of math nodes,inner sep=0cm]{
| [draw,regular polygon,regular polygon sides=3,fill= yellow] | a=b&F(i\omega) = \int^{\infty}_{-\infty}f(t)e^{-i\omega t}dt &c\\
%Second row
d&|[draw,regular polygon,fill= red]| e^2=m^2c^4 &f\\
%Third row
\nabla^2(\vec{r}\times \nabla\psi) - \frac{1}{c^2}\frac{\partial^2 (\vec{r}\times \nabla\psi)}{\partial t^2}&h&|[diamond,fill= blue!20]|F = \frac{d}{dt}{(m\vec{v})}\\
};
\end{tikzpicture}
\end{document}
这使
顺便说一句,对于矩阵条目的蹩脚之处,我深表歉意。
答案3
从你的问题中无法清楚看出你是想要一条横跨整个对角线的条纹,还是想要单独突出显示每个对角线项。percusse 追求的是后者,可以通过使用 percusse 的答案中的一些代码来简化使用 TiKZ 数学节点矩阵模拟 amsmath 矩阵的障碍。这将 amsmath 矩阵命令替换为 TikZ 版本。它实际上与 percusse 的代码相同,但隐藏了很多混乱的部分。实现中的一个变化是我们通过测试行号和列号来应用样式,而不是在每个单元格上指定它。前言可能看起来有点令人生畏,所以我先把结果放出来:
代码本身是:
\documentclass{article}
%\url{https://tex.stackexchange.com/q/26866/86}
\usepackage{tikz}
\usetikzlibrary{matrix}
\newlength\mtxrowsep \setlength\mtxrowsep{1.5ex}
\newlength\mtxcolsep \setlength\mtxcolsep{2\arraycolsep}
\tikzset{
ams/.style={
baseline=-.7ex,
every delimiter/.style={yshift=-1pt},
every left delimiter/.style={xshift=2pt},
every right delimiter/.style={xshift=-2pt},
every node/.style={inner sep=0pt},
},
ams matrix/.style={
inner sep=1pt,
column sep=\mtxcolsep,
row sep=\mtxrowsep,
% ampersand replacement=\&,
matrix of math nodes,
},
bmatrix/.style={
ams,
every matrix/.style={
ams matrix,
left delimiter={[},
right delimiter={]},
}
},
Bmatrix/.style={
ams,
every matrix/.style={
ams matrix,
left delimiter={\lbrace},
right delimiter={\rbrace},
}
},
pmatrix/.style={
ams,
every matrix/.style={
ams matrix,
left delimiter={(},
right delimiter={)},
}
},
vmatrix/.style={
ams,
every matrix/.style={
ams matrix,
left delimiter={|},
right delimiter={|},
}
},
Vmatrix/.style={
ams,
every matrix/.style={
ams matrix,
left delimiter={\|},
right delimiter={\|},
}
},
highlight diagonal/.style={
nodes={highlight diagonal nodes}
},
highlight diagonal nodes/.code={
\ifnum\pgfmatrixcurrentrow=\pgfmatrixcurrentcolumn
\tikzset{fill=yellow}
\fi
}
}
\let\matamp=&
\catcode`\&=13
\makeatletter
\def&{\iftikz@is@matrix
\pgfmatrixnextcell
\else
\matamp
\fi}
\makeatother
\newenvironment{tikzbmatrix}[1][]{%
\begin{tikzpicture}[bmatrix]
\matrix[#1] \bgroup}
{\\\egroup;\end{tikzpicture}}
\begin{document}
\begin{tikzbmatrix}[highlight diagonal,nodes={inner sep=2pt}]
a=b & b & c\\
d & e^2=m^2c^4 & f\\
g & h & F = \frac{d}{dt}{(m\vec{v})}
\end{tikzbmatrix}
\end{document}
您必须想象所有这些乱七八糟的东西都隐藏在一个漂亮整洁的包装中!(实际上它比必要的要长,因为它包含了每种 AMS 矩阵类型的代码。)
为了得到另一种解释,我们需要一种在文本下方绘制线条的方法。幸运的是,已经有一些东西可以在其下方绘制:矩阵节点形状。所以我们只需找到一个从左上角到右下角绘制一条线的形状。由于没有,我们定义一个。(这个strike out
很接近,但还不够接近。)我们希望荧光笔笔触很宽,但设置矩阵的线宽也会为我们不想要的单元格设置它,所以我们必须覆盖它。除此之外,它很简单。
(它的位置并不完美,但这可能与细胞的细长形状有关。这是可以纠正的,但可能需要根据具体情况进行一些调整。)
这是代码。再次将序言视为一个很好的包装。
\documentclass[border=10]{standalone}
%\url{https://tex.stackexchange.com/q/26866/86}
\usepackage{tikz}
\usetikzlibrary{matrix,shapes.misc}
\newlength\mtxrowsep \setlength\mtxrowsep{1.5ex}
\newlength\mtxcolsep \setlength\mtxcolsep{2\arraycolsep}
\makeatletter
\pgfdeclareshape{strike under}
{
\inheritsavedanchors[from=rectangle] % this is nearly a rectangle
\inheritanchorborder[from=rectangle]
\inheritanchor[from=rectangle]{north}
\inheritanchor[from=rectangle]{north west}
\inheritanchor[from=rectangle]{north east}
\inheritanchor[from=rectangle]{center}
\inheritanchor[from=rectangle]{west}
\inheritanchor[from=rectangle]{east}
\inheritanchor[from=rectangle]{mid}
\inheritanchor[from=rectangle]{mid west}
\inheritanchor[from=rectangle]{mid east}
\inheritanchor[from=rectangle]{base}
\inheritanchor[from=rectangle]{base west}
\inheritanchor[from=rectangle]{base east}
\inheritanchor[from=rectangle]{south}
\inheritanchor[from=rectangle]{south west}
\inheritanchor[from=rectangle]{south east}
\backgroundpath{
\pgfpathmoveto{\northeast}
\pgfpathmoveto{\southwest}
\southwest
\pgf@xa=\pgf@x
\northeast
\pgf@ya=\pgf@y
\pgfpathmoveto{\pgfqpoint{\pgf@xa}{\pgf@ya}}
\southwest
\pgf@ya=\pgf@y
\northeast
\pgf@xa=\pgf@x
\pgfpathlineto{\pgfqpoint{\pgf@xa}{\pgf@ya}}
}
}
\makeatother
\tikzset{
ams/.style={
baseline=-.7ex,
every delimiter/.style={yshift=-1pt},
every left delimiter/.style={xshift=2pt},
every right delimiter/.style={xshift=-2pt},
every node/.style={inner sep=0pt},
},
ams matrix/.style={
inner sep=1pt,
column sep=\mtxcolsep,
row sep=\mtxrowsep,
% ampersand replacement=\&,
matrix of math nodes,
},
bmatrix/.style={
ams,
every matrix/.style={
ams matrix,
left delimiter={[},
right delimiter={]},
}
},
Bmatrix/.style={
ams,
every matrix/.style={
ams matrix,
left delimiter={\lbrace},
right delimiter={\rbrace},
}
},
pmatrix/.style={
ams,
every matrix/.style={
ams matrix,
left delimiter={(},
right delimiter={)},
}
},
vmatrix/.style={
ams,
every matrix/.style={
ams matrix,
left delimiter={|},
right delimiter={|},
}
},
Vmatrix/.style={
ams,
every matrix/.style={
ams matrix,
left delimiter={\|},
right delimiter={\|},
}
},
highlight diagonal/.style={
nodes={highlight diagonal nodes}
},
highlight diagonal nodes/.code={
\ifnum\pgfmatrixcurrentrow=\pgfmatrixcurrentcolumn
\tikzset{fill=yellow}
\fi
},
highlighter width/.initial={5mm}
}
\let\matamp=&
\catcode`\&=13
\makeatletter
\def&{\iftikz@is@matrix
\pgfmatrixnextcell
\else
\matamp
\fi}
\makeatother
\newenvironment{tikzbmatrix}[1][]{%
\begin{tikzpicture}[bmatrix]
\matrix[#1] \bgroup}
{\\\egroup;\end{tikzpicture}}
\begin{document}
\begin{tikzbmatrix}[draw=yellow,strike under,line width=1cm,nodes={thin}]
a=b & b & c\\
d & e^2=m^2c^4 & f\\
g & h & F = \frac{d}{dt}{(m\vec{v})}
\end{tikzbmatrix}
\end{document}
答案4
也许可以使用matrix of math nodes
,例如这里.(对我的一个问题的回答)。