# With Tikz, how can you center a scope that varies in size below or next to a node?

I'm looking for a way to center the scope local bounding box=Node4-0 below Node3-0. Also I'd like to put a fixed distance between the scopes surrounded by a red outlined box, like what happens with node distance. This question is not about generating the picture below, but about making (nested) groups of nodes and move those groups around relative to other nodes/groups. I already know how to create the picture below without grouping/scoping nodes, but that's not what this question is about.

My code below is demonstrating a situation in where I'm trying to move around scopes. I'd like to mention again that my goal is not to create the picture below, but to create a group of nodes, even nesting groups, to then place them centered below each other or next to nodes/groups.

\documentclass[10pt]{article}

\usepackage{tikz}               % for generating figures with latex
\usetikzlibrary{shapes,         % shapes
arrows.meta,    % the latest arrow tip library
positioning,    % for positioning nodes in the right place
calc,           % for calculations with coordinates
fit,            % for fitting around nodes
}

\tikzstyle{terminal} = [rounded rectangle, inner sep=1em, fill=black!60, font=\sffamily\bfseries, text=white]
\tikzstyle{process} = [rectangle, draw=black, line width=0.02em, fill=black!10]
\tikzstyle{transitions} = [rectangle, fill=none, inner sep=0.25em]
\tikzstyle{group} = [draw=red, rectangle, inner sep=0, outer sep=0]

\tikzstyle{conn} = [draw, line width=0.02em, rounded corners, -{Stealth[length=0.5em,width=0.4em,inset=0em]}, font=\sffamily\itshape]
\tikzstyle{line} = [draw, line width=0.02em]

\begin{document}
\begin{tikzpicture}[auto, align=center, inner sep=0.7em, font=\sffamily, node distance=1em]

\node [terminal] (Node1-0) {Node1-0};
\node [process, below=of Node1-0] (Node2-0) {Node2-0};
\path [conn] (Node1-0.south) -- (Node2-0.north);
\node [transitions, below=of Node2-0] (Node3-0) {Node3-0};
\path [line] (Node2-0.south) -- (Node3-0.north);

% Branching from this point
\begin{scope}[local bounding box=Node4-0, shift={($(Node3-0.south)+(-10em,-1em)$)}]
% Branch 1
\begin{scope}[local bounding box=BranchNode0-1]
\coordinate(Node0-1);
\node [transitions, below=of Node0-1] (Node1-1) {Node1-1};
\path [line] (Node0-1.south) -- (Node1-1.north);
\node [process, below=of Node1-1] (Node2-1) {Node2-1};
\path [conn] (Node1-1.south) -- (Node2-1.north);
\node [transitions, below=of Node2-1] (Node3-1) {Node3-1};
\path [line] (Node2-1.south) -- (Node3-1.north);

\coordinate[below=of Node3-1](Node4-1);
\path [line] (Node3-1) -- (Node4-1);
\node [group, fit=(Node0-1) (Node1-1) (Node2-1) (Node3-1) (Node4-1)](GroupNode0-1){};
\end{scope}

% Branch 2
\begin{scope}[local bounding box=BranchNode0-2, anchor=north west, shift={($(BranchNode0-1.north east)+(3em,0)$)}]
\coordinate(Node0-2);
\node [transitions, below=of Node0-2] (Node1-2) {Node1-2};
\path [line] (Node0-2.south) -- (Node1-2.north);
\node [process, below=of Node1-2] (Node2-2) {Prevent \\ overlapping \\ of this};
\path [conn] (Node1-2.south) -- (Node2-2.north);

\coordinate[below=of Node2-2](Node3-2);
\path [line] (Node2-2.south) -- (Node3-2.north);
\node [group, fit=(Node0-2) (Node1-2) (Node2-2) (Node3-2)](GroupNode0-2){};
\end{scope}

% Branch 3
\begin{scope}[local bounding box=BranchNode0-3, anchor=north west, shift={($(BranchNode0-2.north east)+(3em,0)$)}]
\coordinate(Node0-3);
\node [transitions, below=of Node0-3] (Node1-3) {Node1-3};
\path [line] (Node0-3) -- (Node1-3);
\node [process, below=of Node1-3] (Node2-3) {Node2-3};
\path [conn] (Node1-3) -- (Node2-3);
\node [process, below=of Node2-3] (Node3-3) {Node3-3};
\path [conn] (Node2-3) -- (Node3-3);
\node [process, below=of Node3-3] (Node4-3) {Node4-3};
\path [conn] (Node3-3.south) -- (Node4-3.north);
\node [transitions, below=of Node4-3] (Node5-3) {Node5-3};
\path [line] (Node4-3.south) -- (Node5-3.north);

\coordinate[below=of Node5-3](Node6-3);
\path [line] (Node5-3) -- (Node6-3);
\node [group, fit=(Node0-3) (Node1-3) (Node2-3) (Node3-3) (Node4-3) (Node5-3) (Node6-3)](GroupNode0-3){};
\end{scope}

% Connecting branches
\path [line] (BranchNode0-1.south) |- (Node4-0.south);
\path [line] (BranchNode0-1.north) |- (Node4-0.north);
\path [line] (BranchNode0-2.south) |- (Node4-0.south);
\path [line] (BranchNode0-2.north) |- (Node4-0.north);
\path [line] (BranchNode0-3.south) |- (Node4-0.south);
\path [line] (BranchNode0-3.north) |- (Node4-0.north);

\end{scope}
\path [line] (Node3-0.south) -- (Node4-0.north);

\node [process, below=of Node4-0] (Node5-0) {Node5-0};
\path [conn] (Node4-0.south) -- (Node5-0.north);
\node [transitions, below=of Node5-0] (Node6-0) {Node6-0};
\path [line] (Node5-0.south) -- (Node6-0.north);

\node [terminal, below=of Node6-0] (Node7-0) {Node7-0};
\path [conn] (Node6-0.south) -- (Node7-0.north);

\end{tikzpicture}
\end{document}


This will generate the following picture:

• If you know the result you want, you can chage the drawing order to obtain it. In this case, would be easier to drawn Node-1-2 branch below Node3-0 and later on draw left and right branches. If you want, you could also use a matrix to manage each branc as a node. Another solution could be to star with the central part and later on add upper and lower nodes, ... – Ignasi Mar 25 '19 at 10:09
• If I'm not wrong, scopes are not nodes, therefore they can not be positioned using their inner nodes as reference. – Ignasi Mar 25 '19 at 10:11

i guess that you looking for something like the following flow chart:

which is generated by:

\documentclass{article}

\usepackage{tikz}               % for generating figures with latex
\usetikzlibrary{arrows.meta,    % the latest arrow tip library
calc,           % for calculations with coordinates
chains,
fit,            % for fitting around nodes
positioning,    % for positioning nodes in the right place
shapes,         % shapes
}
\makeatletter
\tikzset{suppress join/.code={\def\tikz@after@path{}}}% for suppress macro join
\makeatother

\begin{document}
\begin{tikzpicture}[auto,
node distance = 6mm and 3mm,
start chain = going below,
base/.style = {draw, thick, outer sep=0pt,
font=\sffamily, align=center,
on chain, join=by line},
terminal/.style = {base, draw=none, rounded rectangle,
inner sep=1em, fill=black!60,
font=\sffamily\bfseries, text=white},
process/.style = {base, fill=black!10},
transitions/.style = {base, draw=none},
group/.style = {draw=red, thick, yshift=1mm,
inner xsep=0, inner ysep=2mm, outer sep=0,
node contents={}},
line/.style = {-Triangle, semithick}
]
\node [terminal]            (Node1-0) {Node1-0};
\node [process]             (Node2-0) {Node2-0};
\node [transitions]         (Node3-0) {Node3-0};
% left branch
\node [transitions, suppress join,
below left=of Node3-0]   (Node1-1) {Node1-1};
\node [process]                 (Node2-1) {Node2-1};
\node [transitions]             (Node3-1) {Node3-1};
\node [group,
fit=(Node1-1) (Node3-1)];
% middle branch
\node [transitions, suppress join,
below=of Node3-0]        (Node1-2) {Node1-2};
\node [process]                 (Node2-2) {Prevent \\ overlapping \\ of this};
\node [group,
fit=(Node1-2) (Node2-2)];
% right branch
\node [transitions, suppress join,
below right=of Node3-0]  (Node1-3) {Node1-3};
\node [process]                 (Node2-3) {Node2-3};
\node [process]                 (Node3-3) {Node3-3};
\node [process]                 (Node4-3) {Node4-3};
\node [transitions]             (Node5-3) {Node5-3};
\node [group,
fit=(Node1-3) (Node5-3)];
%% middle branch below
\node [process, suppress join,
below=of Node2-2 |- Node5-3.south]
(Node5-0) {Node5-0};
\node [transitions]             (Node6-0) {Node6-0};
\node [terminal]                (Node7-0) {Node7-0};
% Connecting branches
\draw [line] (Node3-0) -- (Node1-2);
\draw [line] (Node2-2) -- (Node5-0);
\coordinate (aux1) at ($(Node3-0)!0.4!(Node1-2)$);
\coordinate (aux2) at ($(Node1-1 |- Node5-3)!0.5!(Node5-0)$);
\draw [line] (aux1) -| (Node1-1);
\draw [line] (aux1) -| (Node1-3);
\draw [line,-]  (Node3-1) |- (aux2)
(Node5-3) |- (aux2);
\end{tikzpicture}
\end{document}


edit: i must confess, that i didn't figured out why you use scopes in your code. as far i know scopes are not intended for purpose which you like to obtain. with it you can prescribe common features (for example line width, fill color, use of pgf layer, etc), you also can move all nodes inside scope with shift option but you cant say scope is below left from some position. this can be done only with nodes. the closest what you can do with your approach is use od shift options, where you should guess amount of shifting distance, for example:

    % Branch 1
\begin{scope}[shift={(-6em,-8em)}]
\coordinate(Node0-1);
...

% Branch 2
\begin{scope}[shift={(0em,-8em)}]
\coordinate(Node0-2);
...

% Branch 3
\begin{scope}[shift={(6em,-8em)}]
\coordinate(Node0-3);


etc. however much simpler (to my opinion is not to use scope and as shown in my suggestion how to draw your flowchart.

to simplify a flowchart code, i add tikz library chains and exploit its macro join for drawing arrows between nodes in chain. used chains are interrupted at three places. There the join is suppressed by defined style suppress join in document preamble.

• Your approach with chains is something I haven't looked at yet. What I'm trying to do is generate the chart with my own commands and environments. In my case a \NewDocumentCommand \step {m o} would create Node2-0 with the mandatory argument as content. The optional argument would create Node3-0. Branches can be created with my own environment \NewEnviron{Alternative} {...\BODY...} where each branch can be created in another environment \NewEnviron{Branch}[1][] {...\BODY}...}. Within that Branch environment, you can use the \step command to create the nodes for that branch. – David M. Mar 25 '19 at 13:29
• Counters will be used to define Node names as new steps are created by the \step command. Preferably I'd like to create paths between nodes as soon as they are defined. Otherwise I'd have to put many Node names in an array or something to later let them connect paths automatically. – David M. Mar 25 '19 at 13:41
• @DavidM., in first i like to know, if the resulted image is what you looking for, the second, to nodes is simple to add the names with, for example start chain = A going below, then nodes have names A-<number>, where number is generated by chains (starting with 1 for the first node in chain), third: it seems that i didn't understand your question well ... it might be because i see your effort with scopes as complication. you cannot positioning scopes as nodes. they are not meant for such purpose. – Zarko Mar 25 '19 at 14:12
• your picture is the result that I need. I'm not sure yet if it will work for my generation code, but it does look promising. If it turns out that it works, I'll mark this as the answer. – David M. Mar 25 '19 at 15:43
• Hey zarko, in your code you're placing the three branches below left, below and below right. However, this is too hardcoded for generating it. A slightly extreme example is having 20 branches and centering that beneath Node3-0. That's why I'm trying to find a way to move a whole group of nodes below a node. I am able to do this with nesting tikzpictures, since they will just go next to each other without overlapping. So the branches can be as wide as they want, but never overlap. By putting those 20 branched tikzpictures in a tikzpicture, the whole thing can be centered below Node3-0. – David M. Apr 1 '19 at 12:52