![Tikz 边缘标签未位于中间](https://linux22.com/image/447266/Tikz%20%E8%BE%B9%E7%BC%98%E6%A0%87%E7%AD%BE%E6%9C%AA%E4%BD%8D%E4%BA%8E%E4%B8%AD%E9%97%B4.png)
我的边缘标签x2 - x1
不是位于midway
,而是位于x1
(0.5
标签下方)。这是我的简化文档,突出显示了问题:
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[node distance={30mm}, thick, main/.style = {draw, circle}]
\node[main] (1) {$x_1$};
\node[main] (2) [above right of=1] {$x_2$};
\draw (2) to [out=180,in=90,looseness=1.0] (1) [dashed] node [midway, fill=white] {0.5};
\end{tikzpicture}
\end{document}
如何将标签正确地放置0.5
在边缘的中间x1 - x2
?
答案1
请尝试以下操作:
\documentclass[border=3.141592]{standalone}
%\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning,
quotes}
\begin{document}
\begin{tikzpicture}[auto,
node distance = 30mm,
thick,
main/.style = {draw, circle},
every edge quotes/.style = {inner sep=2pt, pos=0.44}
]
\node[main] (1) {$x_1$};
\node[main,
above right=of 1] (2) {$x_2$};
\draw[dashed] (1) to [bend left=45, "0.5"] (2);
\end{tikzpicture}
\end{document}
答案2
Zarko 的回答绝对没问题,并且quotes
库对于此事也完全准确,但如果您想坚持使用原始代码,您必须知道在使用该选项时必须将节点放在路径的终点之前to
:
\documentclass[tikz,drop,border=3.14mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{decorations.markings, positioning}
\tikzset{arrow/.pic={
\draw[-latex,line width=1pt] (0,0) -- (0.1,0);%
}}%
\begin{document}
\begin{tikzpicture}[decoration = {%
markings,%
mark =%
between positions 0 and 1 step 5mm % adjust step size here
with {%
{\pic {arrow};},
postaction={decorate}}%
}%
]%
\path[decorate] (0, 0) -- (2, 0) node (n1) [pos=0.5, below = 4 pt , draw, text width=3em, minimum height=2em] {node 1};%
\end{tikzpicture}
\begin{tikzpicture}[node distance={30mm}, thick, main/.style = {draw, circle}]
\node[main] (1) {$x_1$};
\node[main] (2) [above right of=1] {$x_2$};
\draw (2) to [out=180,in=90,looseness=1.0] node [midway, above left, fill=white] {0.5} (1) [dashed] ;
\end{tikzpicture}
\end{document}