# Circling/framing and referring to a bunch of nodes

The end goal: drawing paths between nodes like in the example there but the difference is the nodes are inside a picture here and in particular, the other node is a bunch of nodes in the same picture. So a minor goal is to be able to refer to some nodes as a whole.

My first attempt was just to wrap the said bunch of nodes in a big node but bizarrely, that didn't work ;). So I had a look at

but I still have problems. See the related questions, in particular Q5-Q8 in the example code below. I tried two approaches, the preferred approach would be the one called first try below but that didn't work (Q5). The second approach (using the fit library) is close to what I want but there are still a few flaws left (Q6-Q8).

\documentclass{minimal}

\usepackage{tikz}
\usetikzlibrary{arrows,shapes,fit}

\begin{document}

\tikzstyle{every picture}+=[remember picture]

\begin{tikzpicture}[scale=1.4]

\begin{scope}[>=stealth]
%% Timeline
\draw (0,0) -- (2.2,0);%
\draw[loosely dotted] (2.5,0) -- (3.5,0);%
%%% Q1: In fact, it would have been great if I could just position
%%% the second part of the graph after the continuation dots in
%%% the next scope. See Q4
\draw (3.8,0) -- (7.2,0);%
\draw[loosely dotted] (7.2,0) -- (7.5,0);%
\draw[->] (7.5,0) -- (7.7,0);%
%%% Q2: Is it possible to use calc or something else to just say 5/N+\x-4
\foreach \x/\xtext in {0,...,2,4/N,5/N+1,6/N+2,7/N+3} {%
\draw[shift={(\x,0)}] (0, +0.2) -- (90:0);%
%%% Q3: How do I put this above the tick without manually
%%% setting 0.3? I'm not sure the draw command allows to write
%%% beyond the path ...
\node at (\x,0.3) {$\xtext$};%
}
\end{scope}

\begin{scope}[>=stealth]
%%% Q4: In fact, I wish I could just say continue at any point,
%%% say 3.5 and just start at 1 instead of 4 in this scope. Is
%%% there a way to say this scope should start at 3.5 in the
%%% current picture?
\draw[shift={(4,0)}] (0,0) -- %
node[very near end,right] {$Div_N$} +(90:-0.2);%
\node[right,fill=red!15,baseline] (continuation) at (4,-0.5) {$+ P_N$};%

% ### First try
%%% It's really what I want to do but I can't succeed in
%%% positioning precisely the nested tikzpicture environment.
%%% Q5: What's going wrong here?
% % Maturity phase
% \node[right,fill=red!15] (maturity) at (5,0) {
%   \begin{tikzpicture}
%     \draw[shift={(5,0)}] (0,0) -- %
%     node[very near end,right] (5) {$Div_{N+1}$} +(90:-0.2);%
%     \foreach \x/\xtext in {6/(1+g),7/(1+g)^2} {%
%       \draw[shift={(\x,0)}] (0,0) -- %
%       node[very near end,right] {$Div_{N+1}$} +(90:-0.2);%
%       \node[right] (\x) at (\x,-0.5) {$\times \xtext$};%
%     }
%   \end{tikzpicture}
%   };
% ### Alternative solution with the fit library.
%%% It's very close to the end result but 3 problems here:
%%% Q6: Even playing with the opacity, I can't get the same thing
%%% as with the (continuation) node: I can't see the text as well.
%%% Q7: Adding the inner sep option (see why in Q8) gives
%%% underfull \hbox warning???
%%% Q8: But much worse is the fact that if I get rid of the
%%% ellipse and the inner sep options, the rectangular draw
%%% doesn't include the first $Div_{N+1}$ perfectly
% Maturity phase
\draw[shift={(5,0)}] (0,0) -- %
node[very near end,right] (5) {$Div_{N+1}$} +(90:-0.2);%
\foreach \x/\xtext in {6/(1+g),7/(1+g)^2} {%
\draw[shift={(\x,0)}] (0,0) -- %
node[very near end,right] {$Div_{N+1}$} +(90:-0.2);%
\node[right] (\x) at (\x,-0.5) {$\times \xtext$};%
}
\node[fill=red!15,fill
opacity=0.5,anchor=base,thick,ellipse,fit=(5)(6)(7),inner
sep=3.5pt] (maturity) {};%
\end{scope}

\begin{scope}[overlay,>=stealth,very thick,red!15]
\path[->] (maturity) edge [bend left] (continuation);
\end{scope}

\end{tikzpicture}

\end{document}

-
Three things, why you mayby haven't gotten an answer: You have a lot of questions in your code (1) that aren't related to your actual question (2). How does your output look like and how should it look like (3)? –  Qrrbrbirlbel Nov 25 '12 at 4:45
I edited to make it clear. The problem was I wanted to provide a working code and I didn't want to break it by interspersing my questions which were related to precise parts of the code. So for (1) Q1-Q4 may deserve their own thread but are not directly relevant to the actual problem. (2) If you run the example, you can see a close example of what I want to achieve and the flaws in my second approach. In particular, if I fit with a rectangle and not an ellipse + inner sep, I can't circle/frame all the nodes I want to. (3) I want to circle/frame a bunch of nodes and point to it from elsewhere –  green diod Nov 25 '12 at 9:29

(Note: You ask too many questions in one!)

Some remarks:

• Don't use scale with the fit library !
• ellipse fits the bounding box of its content, so it always seems too high.
• You can use the backgrounds library to put something below existing content.

\documentclass[margin=1mm]{standalone}

\usepackage{tikz}
\usetikzlibrary{shapes,backgrounds,fit,positioning}

\begin{document}
\begin{tikzpicture}[x=1.4cm]
% all points
\foreach \x in {0,...,7}{
\coordinate (pt \x) at (\x,0);
}

\foreach \x\xtext in {0/0,1/1,2/2,4/N,5/N+1,6/N+2,7/N+3}{
\path (pt \x) node[above=3mm] (tick) {$\xtext$};%
\draw (pt \x) -- (tick);
}

% draw axe
\foreach \x in {0,1,4,5,6}{
\pgfmathtruncatemacro{\xnext}{\x+1};
\draw (pt \x) -- (pt \xnext);
}
% first dotted hole
\draw (pt 2) -- +(.2,0);
\draw[loosely dotted] (pt 2) ++(.5,0) -- ++(1,0);
\draw (pt 2) ++(1.8,0) -- (pt 4);
% second dotted hole
\draw (pt 7) -- ++(.2,0);
\draw[loosely dotted] (pt 7) ++(.2,0) -- ++(.3,0);
\draw[-stealth] (pt 7) ++(.5,0) -- ++(.2,0);

\foreach \x/\form/\subform in {%
4/$Div_{N}$/$+P_N$,%
5/$Div_{N+1}$/,%
6/$Div_{N+1}$/$\times(1+g)$,%
7/$Div_{N+1}$/$\times(1+g)^2$}{%
\path (pt \x)
node[below=3mm,anchor=north,inner sep=0] (form \x) {\form};
\node[below=1pt of form \x.south west,anchor=north west,inner sep=0]
(subform \x){\subform};
\draw (pt \x) -- (form \x);
}

% red ellipse and rectangle
\begin{scope}[on background layer]
\node[rounded corners,fit=(form 5.north west)(subform 7),inner sep=2pt,
fill=red!15] (rr) {};
\node[rounded corners,fit=(subform 4),inner sep=2pt,
fill=red!15] (r4) {};
\draw[-stealth,red!15,very thick] (rr) to[bend left=30] (r4);
\end{scope}
\end{tikzpicture}
\end{document}

-
For the note, I'm sorry to ask so many questions at the same time, it's because they all occurred in that example. Thanks for the scale and background remarks, they nearly solved all problems. Any hint on the first approach? Namely, group all the rightmost nodes in one big tikzpicture environment but then, how to position that nested tikzpicture exactly? –  green diod Nov 25 '12 at 18:23
By the way, you also have the underfull hbox error. Where does it come from? –  green diod Nov 25 '12 at 18:36
@greendiod This is a bug in fit library. With the latest CVS version of TikZ/pgf, there is no error. –  Paul Gaborit Nov 25 '12 at 22:40