我刚刚读了一本 David C. Lay 写的《线性代数及其应用 4E》。这本书是(应该是)LaTeX 排版的。矩阵和方程式非常棒,请看以下 3 个示例:
它太漂亮了,所以我想在我的论文中获得同样的效果。这是我的代码:
\usepackage{amsmath}
\usepackage{colortbl}
\newcommand\y{\cellcolor{clight2}}
\definecolor{clight2}{RGB}{212, 237, 244}%
%%%
\[A=\left[
\begin{array}{ccccc}
\y 0 & -3 & -6 & 4 & 9 \\
-1 & \y -2 & -1 & 3 & 1 \\
-2 & -3 & 0 & \y 3 & -1 \\
1 & 4 & 5 & -9 & -7
\end{array}
\right]\]
\[A=\left[\begin{array}{cccccc}
\rowcolor{clight2}
3 & -9 & 12 & -9 & 6 & 15\\
0 & 2 & -4 & 4 & 2 & -6 \\
0 & 3 & -6 & 6 & 4 & -5
\end{array}
\right]\]
结果如下:
但是如何添加这些箭头?
答案1
使用命令
\tikznode[..options..]{..label..}{..contents..}
标记箭头应指向的内容。要添加箭头和文本,请使用
\begin{tikzpicture}[remember picture,overlay]
... tikz code using the labels defined by \tikznode ...
\end{tikzpicture}
\tikznode
在序言中将命令定义为
\usepackage{tikz}
\newcommand\tikznode[3][]%
{\tikz[remember picture,baseline=(#2.base)]
\node[minimum size=0pt,inner sep=0pt,#1](#2){#3};%
}
您必须至少运行两次 LaTeX,直到有关位置的信息传播到各处。
\documentclass{article}
\usepackage{tikz}
\usepackage{amsmath}
\usepackage{colortbl}
\newcommand\y{\cellcolor{clight2}}
\definecolor{clight2}{RGB}{212, 237, 244}%
\newcommand\tikznode[3][]%
{\tikz[remember picture,baseline=(#2.base)]
\node[minimum size=0pt,inner sep=0pt,#1](#2){#3};%
}
\tikzset{>=stealth}
\renewcommand\vec[1]{\mathbf{#1}}
\begin{document}
%%%
\[A=\left[
\begin{array}{rrrrr}
\y \tikznode{pp1}{$0$} & -3 & -6 & 4 & 9 \\
-1 & \y -\tikznode{pp2}{$2$} & -1 & 3 & 1 \\
-2 & -3 & 0 & \y \tikznode{pp3}{$3$} & -1 \\
\tikznode{pc1}{$1$} & \tikznode{pc2}{$4$} & 5 & -\tikznode{pc3}{$9$} & -7
\end{array}
\right]\]
\vspace{3ex}
\[A=\left[\begin{array}{rrrrrr}
\rowcolor{clight2}
3 & -9 & 12 & -9 & 6 & 15\\
0 & \tikznode{piv}{$2$} & -4 & 4 & 2 & -6 \\
0 & \tikznode{npc}{$3$} & -6 & 6 & 4 & -5
\end{array}
\right]\]
\vspace{3ex}
\[\tikznode{u1v1}{$(u_1+v_1)$}\tikznode{a1}{$\vec a_1$}
+ \tikznode{u2v2}{$(u_2+v_2)$}\tikznode{a2}{$\vec a_2$}
\]
\begin{tikzpicture}[remember picture,overlay,cyan,rounded corners]
% explicit coordinates are relative to the end of arrow,
% they do not accumulate (note the single + preceding the coords)
% "Pivot positions"
\draw[<-,shorten <=1pt] (pp1)
-- +(0.4,0)% short line to the right
|- +(4,0.4)% short up and long right
coordinate (pp)% remember position for other arrows
node[right] {Pivot positions};
\draw[<-,shorten <=1pt] (pp2)
-- +(0.4,0)% short line to the right
|- (pp);% up and right to pp
\draw[<-,shorten <=1pt] (pp3)
-- +(0.4,0)% short line to the right
|- (pp);% up and right to pp
% "Pivot columns"
\draw[<-,shorten <=2pt] (pc1)
-- +(0,-0.5)% short line down
coordinate (pc1')% remember position for computing next coord
-- (pc1'-|pp)% horizontal line to position right of pc1' and below of pp
coordinate (pcs)% remember position for other arrows
node[right] {Pivot columns};
\draw[<-,shorten <=2pt] (pc2)
|- (pcs);% down and right to pcs
\draw[<-,shorten <=2pt] (pc3)
|- (pcs);% down and right to pcs
% "Pivot"
\draw[<-,shorten <=1pt] (piv)
-- +(0.4,0)% short line to the right
|- +(1,0.8)% up and right
node[right] {Pivot};
% "New Pivot column"
\draw[<-,shorten <=2pt] (npc)
|- +(1,-0.5)% down and right
node[right] {New Pivot column};
% "Entries in u+v"
\draw[<-] (u1v1)
|- +(4,0.5)% short up and long right
coordinate (uv)% remember position for other arrow and other label
node[right]{Entries in $\vec u+\vec v$};
\draw[<-] (u2v2)
|- (uv);% up and right to uv
% "Columns of A"
\draw[<-] (a1)
-- +(0,-0.5)% short line down
coordinate (a1')% name position for computing next coord
-- (a1'-|uv)% horizontal line to position right of a1' and below of uv
coordinate (a)% remember position for other arrow
node[right]{Columns of $A$};
\draw[<-] (a2)
|- (a);% down and right to a
\end{tikzpicture}
\end{document}
如果您喜欢像原始那样的尖角,请删除该选项rounded corners
。
答案2
对于带有矩阵的图片,您可以使用 创建的 Tikz 节点用 Tikz 绘制{bNiceMatrix}
箭头nicematrix
。
\documentclass{article}
\usepackage{nicematrix,tikz}
\begin{document}
$A =
\begin{bNiceMatrix}[margin,r]
\CodeBefore
\cellcolor{cyan!15}{1-1,2-2,3-4}
\Body
0 & -3 & -6 & 4 & 9 \\
-1 & -2 & -1 & 3 & 1 \\
-2 & -3 & 0 & 3 & -1 \\
1 & 4 & 5 & -9 & -7 \\
\CodeAfter
\begin{tikzpicture} [cyan,<-]
\draw (1-1.east) -| ([xshift=1mm,yshift=2mm]1-|2)
-- ([yshift=2mm]1-|last) -- ++(5mm,0) node [right] {Pivot positions} ;
\draw (2-2.east) -| ([xshift=1mm,yshift=2mm]1-|3) ;
\draw (3-4.east) -| ([xshift=1mm,yshift=2mm]1-|5) ;
\draw (4-1.south) ++(0,-2pt) |- ([yshift=-2mm]5-|last) -- ++(5mm,0) node [right] {Pivot columns} ;
\draw (4-2.south) ++(0,-2pt) |- ([yshift=-2mm]5-|4-2) ;
\draw (4-4.south) ++(0,-2pt) |- ([yshift=-2mm]5-|4-4) ;
\end{tikzpicture}
\end{bNiceMatrix}$
\end{document}
您需要多次编译(因为nicematrix
在后台使用 PGF/Tikz 节点)。
答案3
现有的答案主要关注问题的矩阵部分。在这个答案中,我将重点关注方程式。虽然这是 gernot 的答案,但它缺少一个关键特征:相对于其余数学的间距。
为此,我使用tikzmark
TikZ 库。Tikzmark 自 2007 年首次亮相以来已经发生了很大的变化。http://tex.stackexchange.com/a/316/86(2010 年),其初始形式与 gernot 在其答案中使用的命令非常相似\tikznode
。在随后的十年中,它分为两部分:\tikzmark
,重点关注记住一个位置,和\tikzmarknode
,它基于原始命令,但添加了额外功能,例如检测当前数学模式。
为了获得正确的间距,我们需要两者。我们使用\tikzmarknode
来标记方程的相关部分。然后在稍后tikzpicture
我们将注释节点放置在相对于方程的正确位置。我们还使用\tikzmark
(可以在tikzpicture
环境内部和外部使用)来记住此 中的某些位置tikzpicture
。然后使用这些位置较早设置注释线的间距。
这里有一个微妙之处。这一切都依赖于写入文件aux
并在下次运行时重新读入的页面片段的位置。因此,每次,位置实际上都是一个编译过期。这可能会导致文本片段跳来跳去,永远不会稳定下来。特别是,如果的位置A
取决于以前的的位置B
,以及B
以前的的位置,A
那么它可能会陷入循环。解决方案是构建一个相对定位层。这样它就不再B
依赖于 的先前位置A
,而是依赖于 相对于某个其他点的先前位置A
,从而避免循环。
代码如下:
\documentclass{article}
%\url{https://tex.stackexchange.com/q/361375/86}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{tikzmark,calc,arrows.meta}
\let\vec=\mathbf
\begin{document}
\begin{align*}
A(\vec{u} + \vec{v}) &=
\begin{bmatrix} \vec{a}_1 & \vec{a}_2 & \vec{a}_3 \end{bmatrix}
\begin{bmatrix} u_1 + v_1 \\ u_2 + v_2 \\ u_3 + v_3 \end{bmatrix} \\
%
% The various `\tikzmarknode`s here allow us to refer back to these
% terms
&= \tikzmarknode{uv1}{(u_1 + v_1)} \tikzmarknode{a1}{\vec{a}_1} +
\tikzmarknode{uv2}{(u_2 + v_2)} \tikzmarknode{a2}{\vec{a}_2} +
\tikzmarknode{uv3}{(u_3 + v_2)} \tikzmarknode{a3}{\vec{a}_3}
% This is a bit like a vertical strut; it ensures that there is enough
% vertical space for the annotations
\tikz[remember picture,baseline={(0,0)}] {\path let \p1=(pic cs:upper), \p2=(pic cs:lower), \p3=(pic cs:middle) in (0,\y1-\y3) (0,\y2-\y3);}
\\
%
&= (u_1 \vec{a}_1 + u_2 \vec{a}_2 + u_3 \vec{a}_3) +
(v_1 \vec{a}_1 + v_2 \vec{a}_2 + v_3 \vec{a}_3) \\
%
&= A \vec{u} + A \vec{v}
\end{align*}
\begin{tikzpicture}[remember picture, overlay,>=Latex, cyan]
% locate the annotations
\node[above right] (entries) at ($(a3.north east)+(1,.5)$) {Entries in \(\vec{u} + \vec{v}\)};
\node[below right] (columns) at ($(a3.south east)+(1,-.5)$) {Columns of \(A\)};
% Draw the lines
\draw[<-,rounded corners] (uv1.north) |- (entries);
\draw[<-,rounded corners] (uv2.north) |- (entries);
\draw[<-,rounded corners] (uv3.north) |- (entries);
\draw[<-,rounded corners] (a1.south) |- (columns);
\draw[<-,rounded corners] (a2.south) |- (columns);
\draw[<-,rounded corners] (a3.south) |- (columns);
% These three are used mark the extent of the annotations
% Although a3.base is in the same place as the tikzmark a3, these
% three tikzmarks need to be all defined in the same tikzpicture
% so that their *relative* positions are always the same
\tikzmark{upper}{(entries.north)}
\tikzmark{lower}{(columns.south)}
\tikzmark{middle}{(a3.base)}
\end{tikzpicture}
\end{document}
结果如下: