将节点定位在路径上,而不是“上方”或“下方”

将节点定位在路径上,而不是“上方”或“下方”

路径上的节点是否有第三种选择,即“在”而不是abovebelow?默认值似乎是above

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows,positioning}
\begin{document}
\begin{tikzpicture}[node distance=2cm,auto,>=latex, every node/.style={
    font=\sffamily\scriptsize
    },  
    circtext/.style={draw,circle,minimum size=8pt,inner sep=2pt},
    dot/.style={draw,circle,fill=black,minimum size=0.6mm,inner sep=0pt}
]
{
\node[circtext, fill=yellow](A) at (0,0) {A};
\node[circtext, fill=yellow, right=of A](B){B};
\draw[->] (A) -- node[dot, pos=0.5, above](C){}(B);
}
\end{tikzpicture}
\end{document}

在此处输入图片描述

如果我使用below我会得到这个:

在此处输入图片描述

但我想要路径,不在上方或下方。

答案1

即使处于auto活动状态,您也可以强制on path添加节点选项进行放置anchor=center

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows,positioning}
\begin{document}
\begin{tikzpicture}[node distance=2cm,auto,>=latex, every node/.style={
    font=\sffamily\scriptsize
    },  
    circtext/.style={draw,circle,minimum size=8pt,inner sep=2pt},
    dot/.style={draw,circle,fill=black,minimum size=0.6mm,inner sep=0pt}
]
{
\node[circtext, fill=yellow](A) at (0,0) {A};
\node[circtext, fill=yellow, right=of A](B){B};
\draw[->] (A) -- node[dot, pos=0.25](C){} 
                 node[dot, pos=0.5, anchor=center](C){}  
                 node[dot, pos=0.75, below](C){}(B);
}
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

尝试

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows,positioning}
\begin{document}
\begin{tikzpicture}[node distance=2cm,>=latex, every node/.style={
    font=\sffamily\scriptsize
    },
    circtext/.style={draw,circle,minimum size=8pt,inner sep=2pt},
    dot/.style={draw,circle,fill=black,minimum size=0.6mm,inner sep=0pt}
]
{
\node[circtext, fill=yellow](A) at (0,0) {A};
\node[circtext, fill=yellow, right=of A](B){B};
\draw[->] (A) -- node[dot](C){} (B);
}
\end{tikzpicture}
\end{document}

我只省略了选项auto,这会导致节点自动定位在路径上方或下方。

相关内容