Tikz 边缘标签未位于中间

Tikz 边缘标签未位于中间

我的边缘标签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}

相关内容