tikz中相对定位节点之间的距离

tikz中相对定位节点之间的距离

我正在创建一个框图,我使用的是相对定位。我的代码在这里。块之间的指定距离是 2 厘米。但这是块 1 的中心和块 2 的左边框之间的距离。这不太方便。请告诉我:

1)如何设置块中心之间的距离?

2)是否可以通过设置连接箭头的长度来设置块的位置?

\documentclass[tikz,14pt,border=5pt]{standalone}
\usepackage[T2A]{fontenc}
\usepackage[utf8]{inputenc}

\tikzset{font={\fontsize{14pt}{17}\selectfont}}
\usetikzlibrary{shapes,arrows}
%\usetikzlibrary{positioning}

\begin{document}

\begin{tikzpicture}[auto,>=latex',
 block/.style = {draw, shape=rectangle, minimum height=3em, minimum width=3em, line width=1pt},
branch/.style={fill,shape=circle,minimum size=5pt,inner sep=0pt}]

\draw[help lines] (0,0) grid (5,-2);    

%Positioning Blocks  
\node [block] (block1) at (0,0) {BLOCK1};  
\node [block] (block2) [right = 2.0cm] at (block1) {BLOCK2};

%Conecting Blocks
\begin{scope}[line width=1pt, >=triangle 45]      
\draw[->] (block1) -- (block2);        
\end{scope}

\draw[fill,red] (0,0) circle [radius=0.05];
\draw[fill,red] (2,0) circle [radius=0.05];
\draw[<->,red] (0,-1.5) -- node [above] {2cm} (2,-1.5);  

\end{tikzpicture}

\end{document}

在此处输入图片描述

答案1

欢迎!你注释掉了\usetikzlibrary{positioning}。如果你使用它,你的问题就解决了。然后你可以说

\node [block] (block2) [right = 2.0cm of block1] {BLOCK2}; 

箭的长度可以用 来调整shorten

\documentclass[tikz,14pt,border=5pt]{standalone}
\usepackage[T2A]{fontenc}
\usepackage[utf8]{inputenc}

\tikzset{font={\fontsize{14pt}{17}\selectfont}}
\usetikzlibrary{arrows,positioning}

\begin{document}
\begin{tikzpicture}[auto,>=latex',
 block/.style = {draw, shape=rectangle, minimum height=3em, minimum width=3em, line width=1pt},
branch/.style={fill,shape=circle,minimum size=5pt,inner sep=0pt}]

\draw[help lines] (0,0) grid (5,-2);    

%Positioning Blocks  
\node [block] (block1) at (0,0) {BLOCK1};  
\node [block] (block2) [right = 2.0cm of block1] {BLOCK2};

%Conecting Blocks
\begin{scope}[line width=1pt, >=triangle 45,shorten >=2pt, shorten <=1pt]      
\draw[->] (block1) -- (block2);        
\end{scope}

\end{tikzpicture}

\end{document}

在此处输入图片描述

请注意,arrows.meta优于arrows库。还请注意,如果您想沿此方向绘制更多节点,chains库会很方便。

附录: 作为Torbjørn T. 指出,我没有仔细阅读问题。如果要指定块中心之间的距离,请添加选项on grid

\documentclass[tikz,14pt,border=5pt]{standalone}
\usepackage[T2A]{fontenc}
\usepackage[utf8]{inputenc}

\tikzset{font={\fontsize{14pt}{17}\selectfont}}
\usetikzlibrary{arrows,positioning}

\begin{document}
\begin{tikzpicture}[auto,>=latex',
 block/.style = {draw, shape=rectangle, minimum height=3em, minimum width=3em, line width=1pt},
branch/.style={fill,shape=circle,minimum size=5pt,inner sep=0pt},
on grid]

\draw[help lines] (0,0) grid (5,-2);    

%Positioning Blocks  
\node [block] (block1) at (0,0) {BLOCK1};  
\node [block] (block2) [right = 3.0cm of block1] {BLOCK2};

%Conecting Blocks
\begin{scope}[line width=1pt, >=triangle 45,shorten >=2pt, shorten <=1pt]      
\draw[->] (block1) -- (block2);        
\end{scope}

\end{tikzpicture}

\end{document}

在此处输入图片描述

相关内容