将箭头标签定位在距终点或起点的绝对距离处

将箭头标签定位在距终点或起点的绝对距离处

我正在用

\begin{tikzpicture}
\tikzset{
    operatorNode/.style={
    every node/.style={minimum size=1cm,draw,rectangle}
    }, 
    variableNode/.style={
    every node/.style={minimum size=1cm,draw,circle}
    }
}

    \begin{scope}[operatorNode]
        \node (x) at (0,0) {$4$};
    \end{scope}   
    \begin{scope}[variableNode]
        \node (z1) at (4,0) {$z$};
    \node (z2) at (8,-2) {$z$};   
    \end{scope}   
\draw[->] (x) -- (z1) node [near start, above] {$1$};
\draw[->] (x) -- (z2) node [near start, above] {$2$};
\end{tikzpicture}

这产生了在此处输入图片描述

但是我有多个长度可变的箭头,因此“近起点”选项会导致不同箭头的标签位置不同。发生这种情况的原因是“近起点”将标签置于相对位置。

我想要的是:在此处输入图片描述

是否也可以将标签放置在距箭头起点(或终点)的绝对距离处?

答案1

看看以下解决方案是否可以接受:

\documentclass[tikz, margin=3mm]{standalone}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}[
lbl/.style = {inner sep=2pt, node font=\footnotesize,
              above right=0pt and 10 pt of #1}% set distance according to your wish
                    ]

    \begin{scope}[every node/.style={minimum size=2cm,draw}]
        \node (v1) at (0,3) {$4$};
        \node (z1) at (4,3) {$z$};
        \node (v2) at (0,0) {$4$};
        \node (z2) at (8,0) {$z$};
     \end{scope}
\draw[->] (v1) node [lbl=v1.east] {$1$} -- (z1);
\draw[->] (v2) node [lbl=v2.east] {$2$} -- (z2);
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

decorations.markings库允许您将东西放在路径的绝对位置。

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{decorations.markings}
\begin{document}
\begin{tikzpicture}[
    operatorNode/.style={
    every node/.style={minimum size=1cm,draw,rectangle}
    }, 
    variableNode/.style={
    every node/.style={minimum size=1cm,draw,circle}
    },at absolute position/.style n args={2}{postaction={decorate,
    decoration={markings,
    mark=at position #1 with {#2}}}}
]

    \begin{scope}[operatorNode]
        \node (x) at (0,0) {$4$};
    \end{scope}   
    \begin{scope}[variableNode]
        \node (z1) at (4,0) {$z$};
    \node (z2) at (8,-2) {$z$};   
    \end{scope}   
\draw[->,at absolute position={1cm}{\node [above] {$1$};}] (x) -- (z1)  ;
\draw[->,at absolute position={2cm}{\node [above] {$2$};}] (x) -- (z2) ;
\end{tikzpicture}
\end{document}

在此处输入图片描述

图书馆calc也允许你这样做。

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\tikzset{
    operatorNode/.style={
    every node/.style={minimum size=1cm,draw,rectangle}
    }, 
    variableNode/.style={
    every node/.style={minimum size=1cm,draw,circle}
    }
}

    \begin{scope}[operatorNode]
        \node (x) at (0,0) {$4$};
    \end{scope}   
    \begin{scope}[variableNode]
        \node (z1) at (4,0) {$z$};
    \node (z2) at (8,-2) {$z$};   
    \end{scope}   
\draw[->] (x) -- (z1) coordinate[pos=0] (aux0) coordinate[pos=1] (aux1);
\path ($(aux0)!1cm!(aux1)$) node [above] {$1$};
\draw[->] (x) -- (z2) coordinate[pos=0] (aux0) coordinate[pos=1] (aux1);
\path ($(aux0)!2cm!(aux1)$) node [above] {$2$};
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容