1

I want to create a collection of larger nodes, that have smaller nodes in them. I want them to be a fixed distance from the center of the larger nodes, in such a way that they are facing towards the connecting smaller nodes.

I have tried this with some code that should do this automatically, by calculating the vectors and normalizing them (and multiplying by norm), but the smaller nodes end up on different radii for some reason:

https://i.stack.imgur.com/OYpd1.png

enter image description here

MWE:

\documentclass[aps,pra,reprint,superscriptaddress, usenames,dvipsnames]{revtex4-1}
%
\usepackage{tikz} % TikZ and PGF
\usepackage{pgfplots}
\usepackage{calc}

\def\nodexb{-0.75}% 
\def\nodeyb{2.6}% 

\def\nodexc{1.9}% 
\def\nodeyc{0.5}% 

\def\nodexf{3.5}% 
\def\nodeyf{3.1}%

\def\norm{30}%

\def\dispnbfx{(\nodexb-\nodexf)}%
\def\dispnbfy{(\nodeyb-\nodeyf)}%
\def\normbf{pow(\dispnbfx*\dispnbfx+\dispnbfy*\dispnbfy, \ratio{1}{2})}%
\def\dispbfx{\ratio{\dispnbfx}{\norm*\normbf}}%
\def\dispbfy{\ratio{\dispnbfy}{\norm*\normbf}}%

\def\dispncfx{(\nodexc-\nodexf)}%
\def\dispncfy{(\nodeyc-\nodeyf)}%
\def\normcf{pow(\dispncfx*\dispncfx+\dispncfy*\dispncfy, \ratio{1}{2})}%
\def\dispcfx{\ratio{\dispncfx}{\norm*\normcf}}%
\def\dispcfy{\ratio{\dispncfy}{\norm*\normcf}}%


\begin{document}
\begin{figure}[h]
\tikzset{main node/.style={circle,fill=blue!20,draw,minimum size=1cm,inner sep=0pt},
            }
\pgfmathsetmacro{\dis}{3}

\begin{tikzpicture}[main_node/.style={circle,draw,minimum size=4em]}, small_node/.style={circle, draw,minimum size=1em]}, myline/.style={line width = 0.2mm}]
\node[main_node] (nodeb) at (\nodexb, \nodeyb) {b};
\node[main_node] (nodec) at (\nodexc, \nodeyc) {c};
\node[main_node] (nodef) at (\nodexf, \nodeyf) {f};

\node[small_node] (membf) at ({\nodexb-\dispbfx}, {\nodeyb-\dispbfy}) {};
\node[small_node] (memfb) at ({\nodexf+\dispbfx}, {\nodeyf+\dispbfy}) {};

\node[small_node] (memcf) at ({\nodexc-\dispcfx}, {\nodeyc-\dispcfy}) {};
\node[small_node] (memfc) at ({\nodexf+\dispcfx}, {\nodeyf+\dispcfy}) {};

\draw[myline] (membf) -- (memfb);
\draw[myline] (memcf) -- (memfc);

\end{tikzpicture}
\end{figure}
\end{document}
Zarko
  • 296,517
  • 2
    Hi and welcome. Your code isnt compilable. There is this error: `! Missing number, treated as zero. \ l.42 ...t ({\nodexb-\dispbfx}, {\nodeyb-\dispbfy})` – AndréC Feb 24 '20 at 20:49
  • Please provide an sketch which will show, where you like to have small nodes. On borders of main nodes? BTW, in style definition you have spurious ] . – Zarko Feb 24 '20 at 22:01
  • What you describe has been achieved in this great answer, I think. –  Feb 24 '20 at 23:05

1 Answers1

3

You can do coordinate calculations with the calc library (not package). That way you do not need to store the coordinates in macros, you can always extract them if you have named the coordinates or nodes. Repeating actions can be stored in a pic.

\documentclass[aps,pra,reprint,superscriptaddress, usenames,dvipsnames]{revtex4-1}
%
\usepackage{tikz} % TikZ and PGF
\usetikzlibrary{calc}

\begin{document}
\begin{figure*}
\centering
\begin{tikzpicture}[main_node/.style={circle,draw,minimum size=4em}, 
    small_node/.style={circle, draw,minimum size=1em}, 
    myline/.style={line width = 0.2mm},
    pics/small nodes/.style={code={
      \tikzset{small nodes/.cd,#1}
      \def\pv##1{\pgfkeysvalueof{/tikz/small nodes/##1}}%
      \path
       let \p1=($(\pv{main1})-(\pv{main2})$),\n1={atan2(\y1,\x1)} in
       ($(\pv{main1})+(180+\n1:\pv{d})$) node[small_node,name=\pv{name1}] {}
       ($(\pv{main2})+(\n1:\pv{d})$) node[small_node,name=\pv{name2}]{}
       (\pv{name1}) edge[myline] (\pv{name2})
       ;
    }},
    small nodes/.cd,d/.initial=1.25em,
        main1/.initial=nodeb,main2/.initial=nodec,
        name1/.initial=membc,name2/.initial=memcb]
 \node[main_node] (nodeb) at (-0.75, 2.6) {b};
 \node[main_node] (nodec) at (1.9,0.5) {c};
 \node[main_node] (nodef) at (3.5,3.1) {f};
 \pic{small nodes={main1=nodeb,main2=nodec,name1=membc,name2=memcb}};
 \pic{small nodes={main1=nodeb,main2=nodef,name1=membf,name2=memfb}};
 \pic{small nodes={main1=nodec,main2=nodef,name1=memcf,name2=memfc}};
\end{tikzpicture}
\end{figure*}
\end{document}

enter image description here

The distance of the small nodes from the center is stored in the pgf key d, the names of the main nodes in main1 and main2, and the names of the nodes created in the pic in name1 and name2.