如何将一个节点定位在另外两个节点的中心?

如何将一个节点定位在另外两个节点的中心?

我有两个节点,想将第三个节点置于它们上方的中心位置。在下面的示例中,节点 Z 的垂直位置很好。但是,我想将节点 Z 进一步向右移动,使其相对于底部的两个节点居中。是否有类似的插值命令above=of x!0.5!y或其他简单的方法可以做到这一点?

例子:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning,arrows.meta,fit,calc,backgrounds,shapes.geometric}
\begin{document}
\begin{tikzpicture}
\node[circle, draw=black] (x) {$x$};
\node[circle, draw=black, right=of x] (y) {$y$};
\node[circle, draw=black, above=of x] (z) {$z$};
\path (x) edge[->, sloped, anchor=south] node {left} (z);
\path (z) edge[->, sloped, anchor=south] node {right} (y);
\path (x) edge[->, sloped, anchor=north] node {bottom} (y);
\end{tikzpicture}
\end{document}

输出:

三个节点的三角形

我知道相关问题tikz 中心节点位于其他 2 个节点下方但答案似乎表明right above=x这实际上并没有将节点置于 X 和 Y 之间。

答案1

像这样:

在此处输入图片描述

\documentclass[tikz, margin=3mm]{standalone}
\usetikzlibrary{arrows.meta,
                calc,
                positioning,
                quotes}

\begin{document}
    \begin{tikzpicture}[
node distance = 13mm,
     C/.style = {%C: as circle
                 circle, draw, minimum size=1.5em, inner sep=2pt},
every edge/.style = {draw, -Straight Barb},
every edge quotes/.style = {auto, font=\footnotesize, inner sep=1pt, sloped}
                        ]
\node[C] (x) {$x$};
\node[C, right=of x] (y) {$y$};
\node[C, above=of $(x.north)!0.5!(y.north)$] (z) {$z$};
\path   (x) edge["left"]    (z)
        (z) edge["right"]   (y)
        (x) edge["bottom"]  (y);
    \end{tikzpicture}
\end{document}

答案2

另一种带有on grid选项的方法。

截屏

\documentclass[border=5mm,tikz]{standalone}

\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}
\begin{scope}[on grid,node distance=2cm]
\draw[help lines](-1,-1) grid(3,3);%<-- comment this line to hide the grid
\node[circle, draw=black] (x) {$x$};
\node[circle, draw=black, right=of x] (y) {$y$};
\node[circle, draw=black, above right=2cm and 1cm of x] (z) {$z$};
\end{scope}

\path (x) edge[->, sloped, anchor=south] node {left} (z);
\path (z) edge[->, sloped, anchor=south] node {right} (y);
\path (x) edge[->, sloped, anchor=north] node {bottom} (y);

\end{tikzpicture}
\end{document}

相关内容