我有两个问题需要用以下 TiKz 代码解决。
- 我想使用相对坐标,例如获得更宽的蓝色矩形。我尝试过但
++
没有成功。 - 我的所有样式都有通用定义。有没有办法使用一种可供我所有样式使用的子样式?
我该如何解决这个问题?
\documentclass[12pt]{amsart}
\usepackage{tikz}
\usetikzlibrary{arrows, matrix, positioning}
\tikzset{
% Good spacing hack (the ghost mode)
gs/.style={
rectangle,
thick,
text width=3em,
align=center,
draw=white,
rounded corners,
minimum height=2em
}, % Focus effect
fe/.style={
rectangle,
thick,
rounded corners,
minimum height=2em,
text width=8em,
align=center,
draw=red,
text=magenta
},
% Focus effect Bis
feb/.style={
rectangle,
thick,
rounded corners,
minimum height=2em,
text width=8em,
align=center,
draw=blue,
text=violet
},
% Long focus effect
lfe/.style={
rectangle,
thick,
rounded corners,
minimum height=2em,
text width=12em,
align=center,
draw=red,
text=magenta
},
% Long focus effect bis
lfeb/.style={
rectangle,
thick,
rounded corners,
minimum height=2em,
text width=12em,
align=center,
draw=blue,
text=violet
},
}
\begin{document}
\begin{tikzpicture}
\matrix[matrix of nodes,
row sep = 1em,
column sep = 1.5em,
](mat){
& |[gs]|$a$ & |[gs]|$X$ \\
|[gs]|$q$ & |[gs]|$b$ & |[gs]|$Y$ \\
& |[gs]|$r$ & |[gs]|$Z$ \\
};
\draw[fe] (mat-1-2.north west) -- (mat-1-3.north east)
-- (mat-2-3.south east) -- (mat-2-2.south west)
-- (mat-1-2.north west);
\draw[red,-triangle 60] (mat-1-3.north east) -- ++ (0,-2.5em) |- ++ (3em,0)
node[pos=2.05, lfe] (f){$aY - bX = d$};
\draw[feb] (mat-2-2.north west) -- (mat-2-3.north east)
-- (mat-3-3.south east) -- (mat-3-2.south west)
-- (mat-2-2.north west);
\draw[blue,-triangle 60] (mat-2-3.north east) -- ++ (0,-2.5em) |- ++ (3em,0)
node[pos=2.05, lfeb] (f){$bZ - rY = e$};
\end{tikzpicture}
\end{document}
答案1
您可以直接用 绘制矩形fit
。要使它们变大,您可以增加inner sep
。
\documentclass[12pt]{amsart}
\usepackage{tikz}
\usetikzlibrary{arrows, matrix, positioning,fit}
\tikzset{
% Good spacing hack (the ghost mode)
gs/.style={
rectangle,
thick,
text width=3em,
align=center,
draw=white,
rounded corners,
minimum height=2em
}, %
rc/.style={rectangle,
thick,rounded corners,draw},
lfe/.style={rc,
text width=12em,
align=center,
text=magenta
},
% Long focus effect bis
lfeb/.style={rc,
text width=12em,
align=center,
text=violet
},
}
\begin{document}
\begin{tikzpicture}
\matrix[matrix of nodes,nodes=gs,
row sep = 1em,
column sep = 1.5em,
](mat){
& $a$ & $X$ \\
$q$ & $b$ & $Y$ \\
& $r$ & $Z$ \\
};
\node[fit=(mat-1-2) (mat-2-3),inner xsep=1em,rc,red] (F1){};
\draw[red,-triangle 60] (F1.east) -- ++ (2.5em,0)
node[right, lfe] (f){$aY - bX = d$};
\node[inner xsep=2em,fit=(mat-2-2) (mat-3-3),rc,blue] (F2){};
\draw[blue,-triangle 60] (F2.east) -- ++ (2.5em,0)
node[right,lfeb] (f){$bZ - rY = e$};
\end{tikzpicture}
\end{document}