矩阵和 TikZ:定位表格

矩阵和 TikZ:定位表格

这个想法是完全复制这个图形。我试着找到文本块的定位:有没有更优雅的方法?

在此处输入图片描述

\documentclass[border=10pt]{standalone}
\usepackage{tikz} 
\usetikzlibrary{matrix,decorations.pathreplacing,calc,fit,backgrounds,positioning}

\tikzset{myyellow/.style={
fill=yellow!90, draw opacity=0,
draw=yellow!90, fill opacity=0.3,
}}

\begin{document}

\begin{tikzpicture}[thick,baseline=(A.center),>=stealth,every left  delimiter/.append style={name=rd},]
\matrix [matrix of math nodes,left delimiter=(,right delimiter=)] (A) at  (0,0) { 
a_{1,1} & a_{1,2} & \dots & a_{1,j} & \dots & a_{1,n} \\
a_{2,1} & a_{2,2} & \dots & a_{2,j} & \dots & a_{2,n} \\
\vdots &         &       & \vdots &       & \vdots \\
|[red]|a_{i,1} & |[green]|a_{i,2} & \dots & |[blue]|a_{i,j} & \dots &   |[violet]|a_{i,n} \\
\vdots &         &       & \vdots &       & \vdots \\
a_{n,1} & a_{n,2} & \dots & a_{n,j} & \dots & a_{n,n} \\
};

\matrix [matrix of math nodes,left delimiter=(,right delimiter=)] (P) at (6.5,0) { 
  p_1 \\
  p_2 \\
  \vdots  \\
  p_i=\textcolor{red}{a_{i,1}}\times\textcolor{red}{b_1}+\textcolor{green} {a_{i,2}}\times \textcolor{green}{b_2}+ \dots + \textcolor{violet}{a_{i,n}}\times  \textcolor{violet}{b_n} \\
  \vdots \\
  p_n \\
  };

 \matrix [matrix of math nodes,left delimiter=(,right delimiter=),yshift=2cm] (B) at (P.north) { 
 |[red]|b_1\\
 |[green]|b_2\\
 \vdots\\
 |[blue]|b_j\\
 \vdots\\
 |[violet]|b_n\\
 };

\begin{scope}[on background layer]
\node [fit=(A-4-1) (A-4-6),myyellow,inner xsep=0pt,inner ysep=0pt]{};
\end{scope}
\draw[<->,red](A-4-1) --(A-4-1.east |- B-1-1.center)--(rd.west|-B-1-1.center);
\draw[<->,green](A-4-2)--(A-4-2.east |- B-2-1.center)--(rd.west|-B-2-1.center);
\draw[<->,blue](A-4-4)-- (A-4-4.east |- B-4-1.center)--(rd.west|-B-4-1.center) ;
\draw[<->,violet](A-4-6)-- (A-4-6.north east |- B-6-1.center)-- (rd.west|-B-6-1.center) ;

\node[xshift=-2cm,font=\it] at (P-2-1.west){
\begin{tabular}{l}
  on ajoute\\
  les produits
\end{tabular}
};
\node[xshift=-1.75cm,font=\it] at (A.east |-B-3-1.center){
\begin{tabular}{l}
  on multiplie\\
  ces coefficients\\
\end{tabular}
};

\node[xshift=-0.7cm] at (A.west){$A=$};
\node[xshift=-0.7cm] at (B.west){$B=$};
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案1

如果你的问题是是否有可能消除所有的转变,答案是肯定的。

\documentclass[border=10pt]{standalone}
\usepackage{tikz} 
\usetikzlibrary{matrix,decorations.pathreplacing,calc,fit,backgrounds,positioning}

\tikzset{myyellow/.style={
fill=yellow!90,fill opacity=0.3}}

\begin{document}

\begin{tikzpicture}[thick,baseline=(A.center),>=stealth,every left  delimiter/.append style={name=rd},]
\matrix [matrix of math nodes,left delimiter=(,right delimiter=)] (A)  { 
a_{1,1} & a_{1,2} & \dots & a_{1,j} & \dots & a_{1,n} \\
a_{2,1} & a_{2,2} & \dots & a_{2,j} & \dots & a_{2,n} \\
\vdots &         &       & \vdots &       & \vdots \\
|[red]|a_{i,1} & |[green]|a_{i,2} & \dots & |[blue]|a_{i,j} & \dots &   |[violet]|a_{i,n} \\
\vdots &         &       & \vdots &       & \vdots \\
a_{n,1} & a_{n,2} & \dots & a_{n,j} & \dots & a_{n,n} \\
};

\matrix [matrix of math nodes,left delimiter=(,right delimiter=),right=2em of A]
 (P) { 
  p_1 \\
  p_2 \\
  \vdots  \\
  p_i=\textcolor{red}{a_{i,1}}\times\textcolor{red}{b_1}+\textcolor{green} {a_{i,2}}\times \textcolor{green}{b_2}+ \dots + \textcolor{violet}{a_{i,n}}\times  \textcolor{violet}{b_n} \\
  \vdots \\
  p_n \\
  };

 \matrix [matrix of math nodes,left delimiter=(,right delimiter=),above=2ex of P] (B) { 
 |[red]|b_1\\
 |[green]|b_2\\
 \vdots\\
 |[blue]|b_j\\
 \vdots\\
 |[violet]|b_n\\
 };

\begin{scope}[on background layer]
\node [fit=(A-4-1) (A-4-6),myyellow,inner sep=0pt]{};
\end{scope}
\draw[<->,red](A-4-1) --(A-4-1.east |- B-1-1.center)--(rd.west|-B-1-1.center);
\draw[<->,green](A-4-2)--(A-4-2.east |- B-2-1.center)--(rd.west|-B-2-1.center);
\draw[<->,blue](A-4-4)-- (A-4-4.east |- B-4-1.center)--(rd.west|-B-4-1.center) ;
\draw[<->,violet](A-4-6)-- (A-4-6.north east |- B-6-1.center)-- (rd.west|-B-6-1.center) ;

\node[anchor=north west,font=\itshape,align=left,inner ysep=2ex] at (P.north west){
  on ajoute\\
  les produits};
\node[anchor=north west,font=\itshape,align=left] at (A-4-2.east |- B-2-1.center){
  on multiplie\\
  ces coefficients\\
};

\node[left=1em of A]{$A=$};
\node[left=1em of B]{$B=$};
\end{tikzpicture}
\end{document}

在此处输入图片描述

然而,我并不认为你采用的转变不够优雅。毕竟,你把所有东西都相对于彼此定位了。也就是说,在我看来,你的代码一开始就相当优雅。

答案2

我会这么做。

请注意,如果您设置了every left delimiter/.append style={name=rd}级别tikzpicture,则每个左分隔符节点都会具有相同的名称。

如果inner xsep=inner ysep您可以简单地使用inner sep

此外,您不需要tabular节点文本中的,您可以使用align=...

此外,请注意使用midwayanchorright/left/above/below = ... of ..." (library定位)而不是固定坐标。

\documentclass[border=10pt]{standalone}
\usepackage{tikz} 
\usetikzlibrary{matrix,arrows.meta,fit,backgrounds,positioning}

\tikzset{myyellow/.style={
fill=yellow!90, draw opacity=0,
draw=yellow!90, fill opacity=0.3,
}}

\begin{document}

\begin{tikzpicture}[thick,baseline=(A.center),>=Stealth]
\begin{scope}[every left delimiter/.append style={name=rdA}]
\matrix[matrix of math nodes,left delimiter=(,right delimiter=),
column sep=20pt] (A) at  (0,0) { 
a_{1,1} & a_{1,2} & \dots & a_{1,j} & \dots & a_{1,n} \\
a_{2,1} & a_{2,2} & \dots & a_{2,j} & \dots & a_{2,n} \\
\vdots &         &       & \vdots &       & \vdots \\
|[red]|a_{i,1} & |[green]|a_{i,2} & \dots & |[blue]|a_{i,j} & \dots &   |[violet]|a_{i,n} \\
\vdots &         &       & \vdots &       & \vdots \\
a_{n,1} & a_{n,2} & \dots & a_{n,j} & \dots & a_{n,n} \\
};
\end{scope}
\matrix [matrix of math nodes,left delimiter=(,right delimiter=), right=20pt of A] (P) { 
  p_1 \\
  p_2 \\
  \vdots  \\
  p_i=\textcolor{red}{a_{i,1}}\times\textcolor{red}{b_1}+\textcolor{green} {a_{i,2}}\times \textcolor{green}{b_2}+ \dots + \textcolor{violet}{a_{i,n}}\times  \textcolor{violet}{b_n} \\
  \vdots \\
  p_n \\
  };
\begin{scope}[every left delimiter/.append style={name=rdB}]
 \matrix [matrix of math nodes,left delimiter=(,right delimiter=), above=4pt of P.north] (B) { 
 |[red]|b_1\\
 |[green]|b_2\\
 \vdots\\
 |[blue]|b_j\\
 \vdots\\
 |[violet]|b_n\\
 };
\end{scope}
\begin{scope}[on background layer]
\node [fit=(A-4-1) (A-4-6),myyellow,inner sep=0pt]{};
\end{scope}
\node[left =-2pt of rdA] {$A=$};
\node[left =-2pt of rdB] {$B=$};
\draw[<->,red](A-4-1.east) -- ++(10pt,0) |- (rdB.west|-B-1-1.center);
\draw[<->,green](A-4-2.east) -- ++(10pt,0) |-(rdB.west|-B-2-1.center) node[midway, below right, align=left, black, font=\itshape] {on multiplie\\
  ces coefficients};
\draw[<->,blue](A-4-4.east) -- ++(10pt,0) |- (rdB.west|-B-4-1.center);
\draw[<->,violet](A-4-6.west) -- ++ (-10pt,0) |- (rdB.west|-B-6-1.center);
\node[font=\itshape, anchor=north west, align=left] at (P.north west){
  on ajoute\\
  les produits
};
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案3

此解决方案将中点置于距起点 80 度的直线上。代码有点繁琐,但结果在我看来更漂亮。

演示

\documentclass[border=10pt]{standalone}
\usepackage{tikz} 
\usetikzlibrary{matrix,decorations.pathreplacing,calc,fit,backgrounds,positioning,intersections}

\tikzset{myyellow/.style={
fill=yellow!90, draw opacity=0,
draw=yellow!90, fill opacity=0.3,
}}

\begin{document}

\begin{tikzpicture}[thick,baseline=(A.center),>=stealth,every left  delimiter/.append style={name=rd},]
\matrix [matrix of math nodes,left delimiter=(,right delimiter=)] (A) at  (0,0) { 
a_{1,1} & a_{1,2} & \dots & a_{1,j} & \dots & a_{1,n} \\
a_{2,1} & a_{2,2} & \dots & a_{2,j} & \dots & a_{2,n} \\
\vdots &         &       & \vdots &       & \vdots \\
|[red]|a_{i,1} & |[green]|a_{i,2} & \dots & |[blue]|a_{i,j} & \dots &   |[violet]|a_{i,n} \\
\vdots &         &       & \vdots &       & \vdots \\
a_{n,1} & a_{n,2} & \dots & a_{n,j} & \dots & a_{n,n} \\
};

\matrix [matrix of math nodes,left delimiter=(,right delimiter=)] (P) at (6.5,0) { 
  p_1 \\
  p_2 \\
  \vdots  \\
  p_i=\textcolor{red}{a_{i,1}}\times\textcolor{red}{b_1}+\textcolor{green} {a_{i,2}}\times \textcolor{green}{b_2}+ \dots + \textcolor{violet}{a_{i,n}}\times  \textcolor{violet}{b_n} \\
  \vdots \\
  p_n \\
  };

 \matrix [matrix of math nodes,left delimiter=(,right delimiter=),yshift=2cm] (B) at (P.north) { 
 |[red]|b_1\\
 |[green]|b_2\\
 \vdots\\
 |[blue]|b_j\\
 \vdots\\
 |[violet]|b_n\\
 };

\begin{scope}[on background layer]
\node [fit=(A-4-1) (A-4-6),myyellow,inner xsep=0pt,inner ysep=0pt]{};
\end{scope}
\begin{scope}% safety
  \clip (current bounding box.south west) rectangle (current bounding box.north east);
  \path[name path=a1] (A-4-1) -- ++(80: 10);
  \path[name path=b1] (B-1-1.center) -- ++(180:10);
  \path[name intersections={of=a1 and b1}]
    (intersection-1) coordinate (c1);
  \path[name path=a2] (A-4-2) -- ++(80: 10);
  \path[name path=b2] (B-2-1.center) -- ++(180:10);
  \path[name intersections={of=a2 and b2}]
    (intersection-1) coordinate (c2);
  \path[name path=a3] (A-4-4) -- ++(80: 10);
  \path[name path=b3] (B-4-1.center) -- ++(180:10);
  \path[name intersections={of=a3 and b3}]
    (intersection-1) coordinate (c3);
  \path[name path=a4] (A-4-6) -- ++(80: 10);
  \path[name path=b4] (B-6-1.center) -- ++(180:10);
  \path[name intersections={of=a4 and b4}]
    (intersection-1) coordinate (c4);  
\end{scope}
\draw[<->,red](A-4-1) --(c1)--(rd.west|-B-1-1.center);
\draw[<->,green](A-4-2)--(c2)--(rd.west|-B-2-1.center);
\draw[<->,blue](A-4-4)-- (c3)--(rd.west|-B-4-1.center) ;
\draw[<->,violet](A-4-6)-- (c4)-- (rd.west|-B-6-1.center);

\node[xshift=-2cm,font=\it] at (P-2-1.west){
\begin{tabular}{l}
  on ajoute\\
  les produits
\end{tabular}
};
\node[above,font=\it] at (c3){
\begin{tabular}{l}
  on multiplie\\
  ces coefficients\\
\end{tabular}
};

\node[xshift=-0.7cm] at (A.west){$A=$};
\node[xshift=-0.7cm] at (B.west){$B=$};
\end{tikzpicture}
\end{document}

相关内容