3

I need to put dAB and d12 as they appear in this screenshot. I tried to do it by myself but whenever I move dAB or d12, the arrows move along with it. I would also like to increase the size of the tip of the arrow, as it appears in the screenshot. The actual MWE looks bigger than the one in the screenshot. How can I make it look the same size? The distance between each element of the matrix is shorter and the arrows look thicker. enter image description here

The MWE I am providing was created by the Schrödinger's cat

\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}

enter image description here

  • 1
    pos=0.5 sets the position to halfway. Set it to whatever value you want (between 0 and 1). Similarly, substitute pos=<value> for midway in the other case. – cfr Oct 19 at 22:04
6

As @cfr's saying, you can put stuff in the middle by using midway or pos=0.5. The thickness of the arrows can be controlled with arrows.meta. I also added a second version where the arrows really run between the nodes without shifts.

\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}

enter image description here

Here are two more suggestions:

\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}

enter image description here

  • 1
    @NaveganTeX Yes, I added some suggestions. – Schrödinger's cat Oct 19 at 23:07
  • For the sake of completeness, I would add $r_{2\text{A}}$ since those letter are not in math mode. – NaveganTeX Oct 19 at 23:19
3

You can position the edge labels on the edge with pos=<fraction of edge length> (as mentioned @cfr in her comment). With little modifications to your MWE (such as using the quaotes package for edge labels, probably incorrect guessing, where the lines between nodes in matrix should be, etc) the MWE can be:

\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}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.