我需要将 dAB 和 d12 按照此屏幕截图中显示的方式放置。我尝试自己做,但每当我移动 dAB 或 d12 时,箭头都会随之移动。我还想增加箭头尖端的大小,就像屏幕截图中显示的那样。实际的 MWE 看起来比屏幕截图中的 MWE 更大。我怎样才能使它看起来大小相同?矩阵中每个元素之间的距离更短,箭头看起来更粗。
我提供的 MWE 由薛定谔的猫
\documentclass{standalone}
\usepackage{tikz}
\usepackage{contour}
\contourlength{1pt}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}[font=\sffamily]
\def\mylength{4em}
\matrix[matrix of nodes,column sep=3em,row sep=\mylength,inner sep=0.5*\mylength,
cells={nodes={inner sep=2pt}}](mat){
& A & B \\
1 & $r_{1A}$ & $r_{1B}$\\
2 & $r_{2A}$ & $r_{2B}$\\};
\foreach \X in {1,2}{
\path (mat-\X-3.south) -- (mat-\the\numexpr\X+1\relax-3.north)
coordinate[midway] (auxH-\X)
(mat-2-\X.east) -- (mat-2-\the\numexpr\X+1\relax.west)
coordinate[midway] (auxV-\X);
\draw (mat.west|-auxH-\X) -- (mat.east|-auxH-\X);
\draw (mat.north-|auxV-\X) -- (mat.south-|auxV-\X);}
\draw (mat.south west) -- (mat.south east);
\draw[stealth-stealth] (mat-2-2.north east) -- (mat-2-3.north west)
node[midway,above,scale=0.7] (dAB) {\contour{white}{$d_{AB}$}};
\draw[-stealth] (mat-3-2.south east) -- (mat-3-3.south west);
\draw[-stealth] (mat-2-3) -- (mat-3-3) coordinate[pos=1/3](aux);
\draw[-stealth,densely dotted] (dAB.south) to[out=0,in=80] ++ (1em,-1em)
to[out=-100,in=90]
([xshift=3pt]dAB.south|-mat-3-3);
\draw[stealth-stealth] (mat-2-2.south east) -- (mat-3-2.east)
node[pos=0.5,right,scale=0.7] (d12) {\contour{white}{$d_{12}$}};
\draw[-stealth,densely dotted] (d12.north) to[out=10,in=170] ++ (2em,1em)
to[out=-10,in=180] (aux);
\node[above] at (auxV-2|-mat.north){Movies};
\node[left] at (auxH-2-|mat.west){Users};
\end{tikzpicture}
\end{document}
答案1
midway
正如 @cfr 所说,您可以使用或将东西放在中间pos=0.5
。 箭头的粗细可以用 控制arrows.meta
。 我还添加了第二个版本,其中箭头实际上在节点之间运行而没有移位。
\documentclass{standalone}
\usepackage{tikz}
\usepackage{contour}
\contourlength{1pt}
\usetikzlibrary{matrix,arrows.meta,bending}
\begin{document}
\begin{tikzpicture}[font=\sffamily]
\def\mylength{4em}
\matrix[matrix of nodes,column sep=3em,row sep=\mylength,inner sep=0.5*\mylength,
cells={nodes={inner sep=2pt}}](mat){
& A & B \\
1 & $r_{1A}$ & $r_{1B}$\\
2 & $r_{2A}$ & $r_{2B}$\\};
\foreach \X in {1,2}{
\path (mat-\X-3.south) -- (mat-\the\numexpr\X+1\relax-3.north)
coordinate[midway] (auxH-\X)
(mat-2-\X.east) -- (mat-2-\the\numexpr\X+1\relax.west)
coordinate[midway] (auxV-\X);
\draw (mat.west|-auxH-\X) -- (mat.east|-auxH-\X);
\draw (mat.north-|auxV-\X) -- (mat.south-|auxV-\X);}
\draw (mat.south west) -- (mat.south east);
\begin{scope}[thick,>={Stealth[width=6pt,bend]}]
\draw[<->] (mat-2-2.north east) -- (mat-2-3.north west)
node[midway,above,scale=0.7] (dAB) {\contour{white}{$d_{AB}$}};
\draw[->] (mat-3-2.south east) -- (mat-3-3.south west);
\draw[->] (mat-2-3) -- (mat-3-3) coordinate[pos=1/3](aux);
\draw[->,densely dotted] (dAB.south) to[out=0,in=80] ++ (1em,-1em)
to[out=-100,in=90]
([xshift=3pt]dAB.south|-mat-3-3);
\draw[<->] (mat-2-2.south east) -- (mat-3-2.east)
node[pos=0.5,right,scale=0.5] (d12) {\contour{white}{$d_{12}$}};
\draw[->,densely dotted] (d12.north) to[out=10,in=170] ++ (2em,1em)
to[out=-10,in=180] (aux);
\end{scope}
\node[above] at (auxV-2|-mat.north){Movies};
\node[left] at (auxH-2-|mat.west){Users};
\end{tikzpicture}
\begin{tikzpicture}[font=\sffamily]
\def\mylength{4em}
\matrix[matrix of nodes,column sep=3em,row sep=\mylength,inner sep=0.5*\mylength,
cells={nodes={inner sep=2pt}}](mat){
& A & B \\
1 & $r_{1A}$ & $r_{1B}$\\
2 & $r_{2A}$ & $r_{2B}$\\};
\foreach \X in {1,2}{
\path (mat-\X-3.south) -- (mat-\the\numexpr\X+1\relax-3.north)
coordinate[midway] (auxH-\X)
(mat-2-\X.east) -- (mat-2-\the\numexpr\X+1\relax.west)
coordinate[midway] (auxV-\X);
\draw (mat.west|-auxH-\X) -- (mat.east|-auxH-\X);
\draw (mat.north-|auxV-\X) -- (mat.south-|auxV-\X);}
\draw (mat.south west) -- (mat.south east);
\begin{scope}[thick,>={Stealth[width=6pt,bend]}]
\draw[<->] (mat-2-2) -- (mat-2-3)
node[midway,above,scale=0.7] (dAB) {\contour{white}{$d_{AB}$}};
\draw[->] (mat-3-2) -- (mat-3-3);
\draw[->] (mat-2-3) -- (mat-3-3) coordinate[pos=1/3](aux);
\draw[->,densely dotted] (dAB.south) to[out=0,in=80] ++ (1em,-1em)
to[out=-100,in=90]
([xshift=3pt]dAB.south|-mat-3-3);
\draw[<->] (mat-2-2) -- (mat-3-2)
node[pos=0.5,right,scale=0.5] (d12) {\contour{white}{$d_{12}$}};
\draw[->,densely dotted] (d12.north) to[out=10,in=170] ++ (2em,1em)
to[out=-10,in=180] (aux);
\end{scope}
\node[above] at (auxV-2|-mat.north){Movies};
\node[left] at (auxH-2-|mat.west){Users};
\end{tikzpicture}
\end{document}
这里还有两条建议:
\documentclass{standalone}
\usepackage{tikz}
\usepackage{contour}
\contourlength{1pt}
\usetikzlibrary{matrix,arrows.meta,bending}
\begin{document}
\begin{tikzpicture}[font=\sffamily]
\def\mylength{4em}
\matrix[matrix of nodes,column sep=3em,row sep=\mylength,inner sep=0.5*\mylength,
cells={nodes={inner sep=2pt}}](mat){
& A & & B \\
1 & $r_{1A}$ & & $r_{1B}$\\
2 & $r_{2A}$ & & $r_{2B}$\\};
\foreach \X/\Y in {1/2,2/4}{
\path (mat-\X-4.south) -- (mat-\the\numexpr\X+1\relax-4.north)
coordinate[midway] (auxH-\X)
(mat-2-\X.east) -- (mat-2-\Y.west)
coordinate[midway] (auxV-\X);
\draw (mat.west|-auxH-\X) -- (mat.east|-auxH-\X);
\draw (mat.north-|auxV-\X) -- (mat.south-|auxV-\X);}
\draw (mat.south west) -- (mat.south east);
\begin{scope}[thick,>={Stealth[width=6pt,bend]}]
\draw[<->] (mat-2-2.north east) -- (mat-2-4.north west)
node[pos=0.7,above] (dAB) {\contour{white}{$d_{AB}$}};
\draw[->] (mat-3-2.south east) -- (mat-3-4.south west);
\draw[->] (mat-2-4) -- (mat-3-4) coordinate[pos=1/3](aux);
\draw[->,densely dotted] (dAB.south) to[out=0,in=80] ++ (1em,-1em)
to[out=-100,in=90]
([xshift=3pt]dAB.south|-mat-3-4);
\draw[<->] (mat-2-2.south east) -- (mat-3-2.east)
node[pos=0.7,right] (d12) {\contour{white}{$d_{12}$}};
\draw[->,densely dotted] ([yshift=-1ex]mat-2-2.south east) to[out=10,in=170] ++ (2em,1em)
to[out=-10,in=180] (aux);
\end{scope}
\node[above] at (auxV-2|-mat.north){Movies};
\node[left] at (auxH-2-|mat.west){Users};
\end{tikzpicture}
\begin{tikzpicture}[font=\sffamily]
\def\mylength{4em}
\matrix[matrix of nodes,column sep=3em,row sep=\mylength,inner sep=0.5*\mylength,
cells={nodes={inner sep=2pt}}](mat){
& A & & B \\
1 & $r_{1A}$ & & $r_{1B}$\\
2 & $r_{2A}$ & & $r_{2B}$\\};
\foreach \X/\Y in {1/2,2/4}{
\path (mat-\X-4.south) -- (mat-\the\numexpr\X+1\relax-4.north)
coordinate[midway] (auxH-\X)
(mat-2-\X.east) -- (mat-2-\Y.west)
coordinate[midway] (auxV-\X);
\draw (mat.west|-auxH-\X) -- (mat.east|-auxH-\X);
\draw (mat.north-|auxV-\X) -- (mat.south-|auxV-\X);}
\draw (mat.south west) -- (mat.south east);
\begin{scope}[thick,>={Stealth[width=6pt,bend]}]
\draw[<->] (mat-2-2) -- (mat-2-4)
node[pos=0.7,above] (dAB) {\contour{white}{$d_{AB}$}};
\draw[->] (mat-3-2) -- (mat-3-4);
\draw[->] (mat-2-4) -- (mat-3-4) coordinate[pos=1/3](aux);
\draw[->,densely dotted] (dAB.south) to[out=0,in=80] ++ (1em,-1em)
to[out=-100,in=90]
([xshift=3pt]dAB.south|-mat-3-4);
\draw[<->] (mat-2-2) -- (mat-3-2)
node[pos=0.7,right] (d12) {\contour{white}{$d_{12}$}};
\draw[->,densely dotted] ([yshift=-2ex]mat-2-2.south east) to[out=10,in=170] ++ (2em,1em)
to[out=-10,in=180] (aux);
\end{scope}
\node[above] at (auxV-2|-mat.north){Movies};
\node[left] at (auxH-2-|mat.west){Users};
\end{tikzpicture}
\end{document}
答案2
您可以使用将边缘标签定位在边缘上pos=<fraction of edge length>
(如@cfr 在其评论中所述)。只需对您的 MWE 进行少许修改(例如使用quaotes
边缘标签包,可能猜测错误,矩阵中节点之间的线应该在哪里等),MWE 就可以:
\documentclass[tikz, margin=3mm]{standalone}
\usetikzlibrary{arrows.meta,
calc, matrix,
quotes}
\usepackage{contour}
\contourlength{1pt}
\begin{document}
\begin{tikzpicture}[
> = Stealth,
font = \sffamily,
every edge quotes/.style = {font=\sffamily\scriptsize,inner sep=1pt, auto}
]
\def\mylength{4em}
\matrix (m) [matrix of nodes,
nodes in empty cells,
column sep=\mylength,
row sep=\mylength,
inner sep=0.5*\mylength,
cells={nodes={minimum size=1.2em, anchor=center, inner sep=1pt}},
]
{
& A & B \\
1 & $r_{1A}$ & $r_{1B}$ \\
2 & $r_{2A}$ & $r_{2B}$ \\
};
\foreach \i in {1,2}
{
\path (m-\i-3.south) -- (m-\the\numexpr\i+1\relax-3.north)
coordinate[midway] (H-\i)
(m-2-\i.east) -- (m-2-\the\numexpr\i+1\relax.west)
coordinate[midway] (V-\i);
\draw[shorten >=-1ex] (m.north -| V-\i) -- (m.south -| V-\i);
\draw (m.west |- H-\i) -- (m.east |- H-\i);
}
\draw (m.south west) -- (m.south east);
%
\node[above] at (V-2|-m.north) {Movies};
\node[left] at (H-2-|m.west) {Users};
%
\draw[<->] (m-2-2.north east)
to[pos=0.7,"$d_\mathit{AB}$"{name=dAB}]
(m-2-3.north west);
\draw[<->] (m-2-2.south east)
to[pos=0.2, "$d_{12}$"{name=d12}]
(m-3-2.north east);
%
\draw[->] (m-3-2.east) -- (m-3-3.west);
\draw[->] (m-2-3.south) -- (m-3-3.north) coordinate[pos=0.3] (aux);
%
\draw[->,densely dotted]
(dAB.south)
.. controls +(2em,-0.5em) and +(0,2em) ..
($(m-3-2.east)!0.6!(m-3-3.west)$);
\draw[->,densely dotted]
(d12.north east)
.. controls +(2em,1em) and +(-1em,1em) ..
(aux);
\end{tikzpicture}
\end{document}