# How to set node on line on a different layer than line?

Is it possible to declare, that node on lines are on the main layer regardless that lines are in background layer? For example:

With the following MWE:

\documentclass[12pt,tikz,border=3mm]{standalone}
\usetikzlibrary{arrows,arrows.meta,%
backgrounds,positioning}
\pgfdeclarelayer{foreground}
\pgfdeclarelayer{background}
\pgfsetlayers{background,main,foreground}

\usepackage{amsmath}
\begin{document}
\begin{tikzpicture}[
node distance = 0mm,
LC/.style = {draw=#1,
line width=1mm,
arrows={-Stealth[fill=#1,inset=0pt,length=0pt 1.6,angle'=90]},
},
X/.style = {draw, very thin, fill=white, fill opacity=0.75,
font=\scriptsize,
text=black, text opacity=1, align=left,
inner sep=2pt, sloped, anchor=west,pos=0.07},
]\sffamily
%---
%-------
\coordinate                     (a0)    at (0,0);
\coordinate[right=77mm of a0]   (b0);
\foreach \i [count=\xi from 0] in {1,2,...,4}
{
\coordinate[below=7mm of a\xi]  (a\i);
\coordinate[below=7mm of b\xi]  (b\i);
}
\draw[|->]  (a0) -- (a3) node[above left]   {$t$};
\draw[|->]  (b0) -- (b3) node[above right]  {$t$};
\draw[LC=gray]  (a1)
to node[X] {data\\
$(\text{SeqNum}=0,\ell=1000)$}
(b2);
%-------
\begin{scope}[ X/.append style={anchor=east},
LC/.append style={transform canvas={yshift=-2mm}},
on background layer]
\draw[LC=teal]  (b1)
to node[X] {ACK(AckNum$=$1000)}
(a2);
\end{scope}
%----------------
\end{tikzpicture}
\end{document}


I get:

The first picture I obtain with drawing second lines twice: first as line and over it again invisible one with node. Since my actual diagrams have up to dozen such lines I looking for more convenient solutions for declaring, that nodes are in main plane even if the line is in background.

• No. If I suppress background layer in the scope, than second line go over the node in the first line. I case, that node in the first line has three rows of text, this is quit disturbing. – Zarko Dec 14 '14 at 16:56
• Yes, I noticed that, so I deleted my comment; I will also delete this one shortly. – Gonzalo Medina Dec 14 '14 at 17:00
• You can use this answer of Loop Space. In your case, using his code, you can put \draw[LC=teal] (b1) to node[yshift=-2mm, node on layer=foreground,X] {ACK(AckNum$=$1000)} (a2);. – Kpym Jun 29 '15 at 16:55
• @Kpym, thank you for this link. I need some time to study answer there. On the first sight seem promising. – Zarko Jun 29 '15 at 17:22

How about placing the node on the front layer with a follow-up path command:

\path (b1) to node[X,anchor=east,yshift=-2mm] {ACK(AckNum$=$1000)} (a2);


Here's the complete document:

\documentclass[12pt,tikz,border=3mm]{standalone}
\usetikzlibrary{arrows,arrows.meta,backgrounds,positioning}
\pgfdeclarelayer{foreground}
\pgfdeclarelayer{background}
\pgfsetlayers{background,main,foreground}
\usepackage{amsmath}

\begin{document}

\begin{tikzpicture}
[
node distance = 0mm,
LC/.style = {draw=#1,
line width=1mm,
arrows={-Stealth[fill=#1,inset=0pt,length=0pt 1.6,angle'=90]},
},
X/.style = {draw,
very thin,
fill=white,
fill opacity=0.75,
font=\scriptsize,
text=black,
text opacity=1,
align=left,
inner sep=2pt,
sloped,
anchor=west,
pos=0.07},
]
\sffamily
%---
%-------
\coordinate                     (a0)    at (0,0);
\coordinate[right=77mm of a0]   (b0);
\foreach \i [count=\xi from 0] in {1,2,...,4}
{
\coordinate[below=7mm of a\xi]  (a\i);
\coordinate[below=7mm of b\xi]  (b\i);
}
\draw[|->]  (a0) -- (a3) node[above left]   {$t$};
\draw[|->]  (b0) -- (b3) node[above right]  {$t$};
\draw[LC=gray]  (a1)
to
node[X] {data\\
$(\text{SeqNum}=0,\ell=1000)$}
(b2);
%-------
\begin{scope}[X/.append style={anchor=east},
LC/.append style={transform canvas={yshift=-2mm}},
on background layer]
\draw[LC=teal]  (b1)
to
(a2);
\end{scope}
%----------------
\path (b1) to node[X,anchor=east,yshift=-2mm] {ACK(AckNum$=$1000)} (a2);
\end{tikzpicture}
\end{document}


• Yes, this is the possible solution. As I mentioned in the question, temporary I solve the problem with \draw[draw=none] (a) to node {...} (b);. Your solution with use of path is better. But, in cases of lot such nodes, even using path this approach become cumbersome. So I wonder, if it is possible somehow declare (with tikzset, for example), that all nodes must appear in main layer regardless on which layer they are anchored. – Zarko Dec 14 '14 at 18:16
• If you have multiple such paths to create, I would do one of two things: (1) pass them through a \foreach group where you can automate the process of drawing within a scoped environment and then constructing the node outside that environment. Or, (2) I would define a macro which would access the parameters to be passed to the scope, draw the line within the scope environment, and then create the node outside the enviornment. – A.Ellett Dec 15 '14 at 4:05
• Ellet, temporary I use \foreach loops for drawing lines on background layer (where it is possible) and nodes separately in in the main layer. This approach has weakness that in such way it is difficult to structure diagram code, maintaining it or reuse it for similar new diagrams. So, the options for node, by which one declare on which layer it should appear, will simplify coding a lot. – Zarko Dec 15 '14 at 8:09