将文本放入循环内

将文本放入循环内

我怎样才能添加与短语“基于项目”上方箭头所指区域内出现的相同文本,{$\mathrm{sim}(u_{a},u_{j})$}但在“基于用户的相似性测量”文本旁边箭头所指区域内?

代码

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{matrix}
\begin{document}
    \begin{tikzpicture}[font=\footnotesize,b/.style={text=blue,font=\bfseries}]
    \matrix(m)[matrix of nodes,
    nodes={minimum size=5ex,anchor=center,draw},
    row 1/.style={nodes={draw=none,fill=none}},
    column 5/.style={nodes={fill=gray!30}},
    column 6/.style={nodes={fill=gray!30}},
    row 4/.style={nodes={fill=orange!30}},
    column 1/.style={nodes={draw=none,fill=none}},
    row sep=-\pgflinewidth,
    column sep={-\pgflinewidth},
    nodes in empty cells,
    ]{
        & $i_{1}$& $i_{2}$ & $i_{3}$ & \dots &$ i_{k}$    & $ i_{a}$  & \dots &$ i_{n}$ \\ 
        $u_{1}$ & & 5 &   & 2 & 4    &    &  &  \\ 
        $u_{2}$ & & 5 & 4 &  & 5 &   & 4  &\\ 
        |[fill=none]|\dots & &   &   &  &   & 1 & 1 & 2   \\ 
        $u_{j}$ & 3 &  & |[b]| ? &  & |[b]| ? & 3 & &  0 \\ 
        $u_{a}$ & & |[b]| ? & 2 &  & 4 &  &|[b]| ? &  \\ 
        \dots& & |[b]| ? & 2 &  & 4 &  &|[b]| ? &  \\ 
        $u_{6}$ & & |[b]| ? & 2 &  & 4 &  &|[b]| ? &  \\
    };
    \draw[-latex](m-6-9.east)--+(1,0)node[above right]{User-based similarity measure}|-(m-5-9.east);
    \node[left]at(m-4-1.west){Active User};
    \draw[latex-] (m-1-6) -- ++ (-1,1) node[above left,align=left]
    {Item preference score is\\ predicted for active user};
    \draw[-latex]([xshift=-2pt]m-8-6.south east)--++(0,-1) -| 
    ([xshift=2pt]m-8-5.south west)
    node[pos=0.25,below]{Item-based}
    node[pos=0.25,above,font=\tiny]{$\mathrm{sim}(u_{a},u_{j})$};
    \end{tikzpicture}
\end{document}

在此处输入图片描述

答案1

由于循环是用语法构建的|-,因此使用此类路径上节点的特定位置就足够了。要知道:

  • 0.5 是直角
  • .25 是第一段的一半。

出于美观原因,我把文本分成两行。

\draw[-latex,align=center](m-6-9.east)--+(1.8,0)node[above right]{User-based\\ similarity measure}|-(m-5-9.east)node[pos=.25,left]{$\mathrm{sim}(u_{a},u_{j})$};

截屏

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{matrix}
\begin{document}
    \begin{tikzpicture}[font=\footnotesize,b/.style={text=blue,font=\bfseries}]
    \matrix(m)[matrix of nodes,
    nodes={minimum size=5ex,anchor=center,draw},
    row 1/.style={nodes={draw=none,fill=none}},
    column 5/.style={nodes={fill=gray!30}},
    column 6/.style={nodes={fill=gray!30}},
    row 4/.style={nodes={fill=orange!30}},
    column 1/.style={nodes={draw=none,fill=none}},
    row sep=-\pgflinewidth,
    column sep={-\pgflinewidth},
    nodes in empty cells,
    ]{
        & $i_{1}$& $i_{2}$ & $i_{3}$ & \dots &$ i_{k}$    & $ i_{a}$  & \dots &$ i_{n}$ \\ 
        $u_{1}$ & & 5 &   & 2 & 4    &    &  &  \\ 
        $u_{2}$ & & 5 & 4 &  & 5 &   & 4  &\\ 
        |[fill=none]|\dots & &   &   &  &   & 1 & 1 & 2   \\ 
        $u_{j}$ & 3 &  & |[b]| ? &  & |[b]| ? & 3 & &  0 \\ 
        $u_{a}$ & & |[b]| ? & 2 &  & 4 &  &|[b]| ? &  \\ 
        \dots& & |[b]| ? & 2 &  & 4 &  &|[b]| ? &  \\ 
        $u_{6}$ & & |[b]| ? & 2 &  & 4 &  &|[b]| ? &  \\
    };
    \draw[-latex,align=center](m-6-9.east)--+(1.8,0)node[above right]{User-based\\ similarity measure}|-(m-5-9.east)node[pos=.25,left]{$\mathrm{sim}(u_{a},u_{j})$};
    \node[left]at(m-4-1.west){Active User};
    \draw[latex-] (m-1-6) -- ++ (-1,1) node[above left,align=left]
    {Item preference score is\\ predicted for active user};
    \draw[-latex]([xshift=-2pt]m-8-6.south east)--++(0,-1) -| 
    ([xshift=2pt]m-8-5.south west)
    node[pos=0.25,below]{Item-based}
    node[pos=0.25,above,font=\tiny]{$\mathrm{sim}(u_{a},u_{j})$};
    \end{tikzpicture}
\end{document}

答案2

这或多或少重复了产生代码的答案。它的设计方式是,如果您添加更多文本,箭头循环将调整其大小。

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{matrix}
\begin{document}
    \begin{tikzpicture}[font=\footnotesize,b/.style={text=blue,font=\bfseries}]
    \matrix(m)[matrix of nodes,
    nodes={minimum size=5ex,anchor=center,draw},
    row 1/.style={nodes={draw=none,fill=none}},
    column 5/.style={nodes={fill=gray!30}},
    column 6/.style={nodes={fill=gray!30}},
    row 4/.style={nodes={fill=orange!30}},
    column 1/.style={nodes={draw=none,fill=none}},
    row sep=-\pgflinewidth,
    column sep={-\pgflinewidth},
    nodes in empty cells,
    ]{
        & $i_{1}$& $i_{2}$ & $i_{3}$ & \dots &$ i_{k}$    & $ i_{a}$  & \dots &$ i_{n}$ \\ 
        $u_{1}$ & & 5 &   & 2 & 4    &    &  &  \\ 
        $u_{2}$ & & 5 & 4 &  & 5 &   & 4  &\\ 
        |[fill=none]|\dots & &   &   &  &   & 1 & 1 & 2   \\ 
        $u_{j}$ & 3 &  & |[b]| ? &  & |[b]| ? & 3 & &  0 \\ 
        $u_{a}$ & & |[b]| ? & 2 &  & 4 &  &|[b]| ? &  \\ 
        \dots& & |[b]| ? & 2 &  & 4 &  &|[b]| ? &  \\ 
        $u_{6}$ & & |[b]| ? & 2 &  & 4 &  &|[b]| ? &  \\
    };
    \node[left]at(m-4-1.west){Active User};
    \path (m-6-9.south east) -- (m-5-9.north east) 
     node[midway,right,font=\tiny] (ub) {$\mathrm{sim}(u_{a},u_{j})$};
    \draw[-latex](m-6-9.east) --
    ([xshift=2pt]ub.east|-m-6-9.east) |-(m-5-9.east)
    node[pos=0.25,right,align=left]  {User-based si-\\milarity measure};
    ;
    \draw[latex-] (m-1-6.north) -- ++ (-1,1) node[above left,align=left]
    {Item preference score is\\ predicted for active user};
    \draw[-latex]([xshift=-2pt]m-8-6.south east)--++(0,-1) -| 
    ([xshift=2pt]m-8-5.south west)
    node[pos=0.25,below]{Item-based}
    node[pos=0.25,above,font=\tiny]{$\mathrm{sim}(u_{a},u_{j})$};
    \end{tikzpicture}
\end{document}

在此处输入图片描述

要查看此自动调整,请尝试

\path (m-6-9.south east) -- (m-5-9.north east) 
 node[midway,right] (ub) {$\mathrm{sim}(u_{a},u_{j})$};

要得到

在此处输入图片描述

没有硬编码的距离。

相关内容