I would like to place a node 'B' 60 degrees of an arbitrary node 'A' between which there is a distance of 2cm. I can do this if node 'A' is at (0, 0), so 'B' would just be (60 : 2) from 'A', but what happens when 'A' is not at (0, 0)?
[Tex/LaTex] How to you position a node relatively to another in TikZ
nodespositioningtikz-pgf
Related Solutions
You may want to use the trees
library in tikz, which in this situation would provide, up to me, a rather easy syntax. I made a small example for you.
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning,trees,arrows}
\begin{document}
\begin{tikzpicture}[->,>=stealth',shorten <=0.5pt,
main/.style={draw,thick,rounded corners, top color=blue!20, bottom color=white,
minimum width=1cm},
child/.style={draw,thick,rounded corners, top color=red!20, bottom color=white}]
\tikzstyle{level 1}=[sibling angle=22.5]
\node[main] (a) {A} [counterclockwise from=0]
child { node[child] {1}}
child { node[child] {2}}
child { node[child] {3}}
child { node[child] {4}}
child { node[child] {5}}
child { node[child] {6}}
child { node[child] {7}}
child { node[child] {8}}
child { node[child] {9}}
;
\node[main] (ba) [below of=a] {belowA};
\path
(ba) edge (a)
;
\end{tikzpicture}
\end{document}
Which gives as result:
If all you want to do is to horizontally align the (bang1)
and (bang2)
nodes, you don't need such complicated calculations. You can use the let
syntax to get the x-coordinate of (bang1)
and the y-coordinate of (sub3)
, using something like
\path let \p1=(bang1), \p2=(sub3) in
node[bangnode,yshift=-2cm,anchor=north] (bang2) at (\x1,\y2) {Bang};
A complete example:
\documentclass[12pt]{standalone}
\usepackage{fontspec}
\usepackage{tikz}
\usetikzlibrary{arrows,positioning,shapes,backgrounds,fit,calc}
\begin{document}
\begin{tikzpicture}[node distance=3cm, auto]
\tikzset{
mynode/.style={rectangle,rounded corners,draw=black, top color=white, bottom color=red!50,very thick, inner sep=1em, minimum size=3em, text centered},
keynode/.style={rectangle,rounded corners,draw=black, top color=white, bottom color=orange!50,very thick, inner sep=1em, minimum size=3em, text centered},
bangnode/.style={shape=star, star points=20, star point ratio=1.65,draw=black, top color=white, bottom color=yellow!50,very thick, inner sep=2em, minimum size=3em, text centered,font=\bfseries},
myarrow/.style={->, >=latex', shorten >=1pt, thick},
mylabel/.style={text width=7em, text centered}
}
\node[bangnode](bang1){Bang};
\node[keynode,below left=2cm and 2cm of bang1](key){Key Message};
\node[mynode,below right of=key](sub1){Sub Topic 1};
\node[mynode,below of=sub1](sub2){Sub Topic 2};
\node[mynode,below of=sub2](sub3){Sub Topic 3};
\path let \p1=(bang1), \p2=(sub3) in
node[bangnode,yshift=-2cm,anchor=north] (bang2) at (\x1,\y2) {Bang};
%ARROWS
\draw[<->,bend left=45] (sub1)[]to(sub2);
\draw[<->,bend left=45] (sub2)to(sub3);
\draw[<->,bend left=45] (sub1)to (key);
\draw[<->,bend left=45] (sub2)to (key);
\draw[<->,bend left=45] (sub3)to (key);
\begin{pgfonlayer}{background}
\node [fill=blue!10,fit=(bang1) (bang2) (sub1)(sub2)(sub3)(key)] {};
\end{pgfonlayer}
\end{tikzpicture}
\end{document}
Another simpler option would be to use the perpendicular coordinate system, and use something like
\node[bangnode,yshift=-2cm,anchor=north] (bang2) at (bang1|-sub3) {Bang};
to place the node using the x-coordinate of (bang1)
and the y-coordinate of (sub3)
. A complete example:
\documentclass[12pt]{standalone}
\usepackage{fontspec}
\usepackage{tikz}
\usetikzlibrary{arrows,positioning,shapes,backgrounds,fit,calc}
\begin{document}
\begin{tikzpicture}[node distance=3cm, auto]
\tikzset{
mynode/.style={rectangle,rounded corners,draw=black, top color=white, bottom color=red!50,very thick, inner sep=1em, minimum size=3em, text centered},
keynode/.style={rectangle,rounded corners,draw=black, top color=white, bottom color=orange!50,very thick, inner sep=1em, minimum size=3em, text centered},
bangnode/.style={shape=star, star points=20, star point ratio=1.65,draw=black, top color=white, bottom color=yellow!50,very thick, inner sep=2em, minimum size=3em, text centered,font=\bfseries},
myarrow/.style={->, >=latex', shorten >=1pt, thick},
mylabel/.style={text width=7em, text centered}
}
\node[bangnode](bang1){Bang};
\node[keynode,below left=2cm and 2cm of bang1](key){Key Message};
\node[mynode,below right of=key](sub1){Sub Topic 1};
\node[mynode,below of=sub1](sub2){Sub Topic 2};
\node[mynode,below of=sub2](sub3){Sub Topic 3};
\node[bangnode,yshift=-2cm,anchor=north] (bang2) at (bang1|-sub3) {Bang};
%ARROWS
\draw[<->,bend left=45] (sub1)[]to(sub2);
\draw[<->,bend left=45] (sub2)to(sub3);
\draw[<->,bend left=45] (sub1)to (key);
\draw[<->,bend left=45] (sub2)to (key);
\draw[<->,bend left=45] (sub3)to (key);
\begin{pgfonlayer}{background}
\node [fill=blue!10,fit=(bang1) (bang2) (sub1)(sub2)(sub3)(key)] {};
\end{pgfonlayer}
\end{tikzpicture}
\end{document}
And, to answer the specific question, of course you can calculate distances between nodes, using veclen
and the let
syntax; a simple example:
\documentclass[12pt]{article}
\usepackage{tikz}
\usetikzlibrary{positioning,calc}
\begin{document}
\begin{tikzpicture}
\node (A) {A};
\node[below left=of A ] (B) {B};
\node[below =of B ] (C) {C};
\node[below right =of C ] (D) {D};
\draw (B) -- (D);
\draw (B) let \p1 = ($ (D) - (B) $)
in circle ({veclen(\x1,\y1)});
\end{tikzpicture}
\end{document}
In the original code:
\draw (key) let
\p1 = ($ (sub3) - (key) $);
in
circle ({veclen(\x1,\y1)});
there's a superfluous semicolon at the end of the second line; once this is removed:
\draw (key) let
\p1 = ($ (sub3) - (key) $)
in
circle ({veclen(\x1,\y1)});
the code should work.
Related Question
- [Tex/LaTex] How to position one node relative to another node at a certain angle in TikZ taking the nodes’ size into account
- [Tex/LaTex] accuratelly place a node above and left of another node
- [Tex/LaTex] How to position a node between two node
- TikZ-PGF Positioning – Placing Node Right After End of Path
Best Answer
There are multiple ways to do this. Some of them need the
calc
orpositioning
TikZ libraries (\usetikzlibrary{calc,positioning}
).The simplest way would be (using
calc
TikZ-library):You can also use the coordinate options
([<options>]<coordinate>)
to add ashift
option, which doesn't require any libraries but looks a little funny.