1

I'm creating a block diagram and I'm using relative positioning. My code is here. The specified distance between the blocks is 2 cm. But this is the distance between the center of block1 and the left border of block2. It's not very convenient. Tell me please:

1) How can I set distance between centers of blocks?

2) It’s possible set block position by setting length of connecting arrow?

\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}

enter image description here

1
  • Welcome! You commented out \usetikzlibrary{positioning}. If you use it, your problem is solved. You can then say \node [block] (block2) [right = 2.0cm of block1] {BLOCK2};. The arrow length can be adjusted with shorten.
    – user194703
    Feb 18, 2020 at 19:30

1 Answer 1

2

Welcome! You commented out \usetikzlibrary{positioning}. If you use it, your problem is solved. You can then say

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

The arrow length can be adjusted with 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}

enter image description here

Please note that arrows.meta is superior to the arrows library. Note also that if you want to draw more nodes along this direction, the chains library can be handy.

ADDENDUM: As Torbjørn T. points out, I did not read the question carefully enough. If you want to specify the distance between the centers of the block, add the on grid option.

\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}

enter image description here

6
  • 1
    This answers the OPs second question, you could mention on grid as well, which covers the first one. Feb 18, 2020 at 19:38
  • @TorbjørnT. Thanks! I clearly missed that. Would you like to write an answer, I'll be more than happy to remove mine.
    – user194703
    Feb 18, 2020 at 19:41
  • I see no point in a second answer, when a quick edit to yours will do. Feb 18, 2020 at 19:42
  • @TorbjørnT. Thanks again! Done.
    – user194703
    Feb 18, 2020 at 19:49
  • Thanks! I also got a way to set the distance between the centers of the blocks: \node [block,node distance=3cm,right of = block1] (block2) {BLOCK2}; Uncommening \usetikzlibrary{positioning}
    – Dmitro
    Feb 18, 2020 at 20:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .