如何在 Tikz 中将一个节点相对于另外两个节点定位

如何在 Tikz 中将一个节点相对于另外两个节点定位

我对 Tikz 非常陌生,正在尝试绘制一个包含 5 个节点的图表。我的代码如下:

\usepackage{tikz}
\usetikzlibrary{automata, arrows, positioning}

\begin{document}

\begin{center}
\begin{tikzpicture}
\tikzset{node distance = 3 cm, every state/.style = {semithick}, every edge/.style = {draw, ->, > = stealth', semithick}}

\node[state] (2) {2};
\node[state, right = of 2] (3) {3};
\node[state, below right = of 2] (1) {1};
\node[state, below left = of 1] (4) {4};
\node[state, below right = of 1] (5) {5};

\end{tikzpicture}
\end{center}

\end{document}

这里的问题是,我希望节点 1 正好位于节点 2 和 3 的正下方和中间,因此我尝试编写类似 的代码\node[state, below right = of 2 and below left = of 3] (1) {1}。但是,这不起作用。

我该如何修改我的代码来实现这一点?

答案1

使用库在和calc之间定义一个点,称为32a

在此处输入图片描述

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{automata, arrows, positioning, calc}

\begin{document}

\begin{center}
\begin{tikzpicture}
\tikzset{node distance = 3 cm, every state/.style = {semithick}, every edge/.style = {draw, ->, > = stealth', semithick}}

\node[state] (2) {2};
\node[state, right = of 2] (3) {3};
\coordinate(a) at ($(2)!0.5!(3)$);
\node[state, below= of a] (1) {1};
\node[state, below left = of 1] (4) {4};
\node[state, below right = of 1] (5) {5};

\end{tikzpicture}
\end{center}

\end{document}

相关内容