# Fill color of a text that fits the background with Tikz

I would like to fill the texts' background in the shaded rectangle to overlay the dashed arrows, with a color that fits the figure's background.

I guess I should use the fill option with my background gradient color but how to choose the right values ? Is there an option of filling with a transparent color ?

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows}

\begin{document}
\begin{tikzpicture}

\shade[bottom color=gray!10, top color=gray!80] (0,0) --++ (5,0) --++ (0,4) --++ (-5,0) -- cycle;
\draw (0,0) --++ (5,0) --++ (0,4) --++ (-5,0) -- cycle;

\foreach \y [count =\c] in {0.5,1.5,2.5,3.5}{
\draw [-open triangle 60 reversed,thick](5,\y) --++ (1,0) --++ (0,0.5) node[below right]{$s_\c(\theta_0, t)$};
\draw [dashed] (5,\y) -- (0,2) node[midway, sloped] {$h_\c(t)$};
}

\foreach \y/\couleur [count = \c] in {0.5/red,1.5/purple,2.5/violet,3.5/blue}{
\pgfmathsetseed{\y+11}
\draw[\couleur] plot[domain=0:1.5, samples = 500] (\x-5,{rand*0.5*sin(\x*2000)*exp(-3.5*(-\x+1.5))+\y});
\draw (-6,\y) node {$h_{\c}^*(-t)$};
\draw [->, shorten >=1cm](-3,\y) -- (0,2);
}

\draw (-0.5,2) circle (0.4) node {\Large $\Sigma$};

\end{tikzpicture}
\end{document}

-
Not sure I understand what you desire. If you just want the background of the shaded rectangle to be the same as the background just comment out the \shade. Or add fill opacity=0.2 which will allow some of the background to show thru. But unless there is an image in the background, changing the opacity is just like changing the color. –  Peter Grill Feb 28 at 18:57
Sorry if I explained myself badly, I would like to fill the texts' background in the shaded rectangle, with a color gradient that fits the shaded rectangle. The purpose is to overlay the dashed arrows, I will edit my question. –  Thomas Feb 28 at 19:29
My best guess: add a clip to the text node and fill again with the same shading in the remaining scope –  Toscho Feb 28 at 19:37
I do not understand most of the code, but one thing a can give an advice: You can merge the first two commands in your tikzpicture to \shadedraw [bottom…] (0,0) …. –  Speravir Mar 1 at 5:01

Another way is

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows}

\begin{document}
\begin{tikzpicture}

(0,0) --++ (5,0) --++ (0,4) --++ (-5,0) -- cycle;

\foreach \y [count =\c] in {0.5,1.5,2.5,3.5}{
\path (0,2) -- (5,\y) node[midway, sloped] (tmp) {$h_\c(t)$};
\draw [dashed]  (0,2) --(tmp.west) (tmp.east) -- (5,\y);
}

\end{tikzpicture}
\end{document}


Remarks : you use \shaddrawto avoid the repetition of coordinates for the rectangle. Then you can place the nodes and then you can draw the dashed lines.

-
Thank you for this answer and the \shaddraw tip. –  Thomas Mar 1 at 8:38
Much more elegant than my own one. –  Toscho Mar 1 at 13:24

My solution:

• Do it as you did once.
• Clip the rectangle path of the text node.
• Fill again with the same shading in the clipped area.
• Create the paths with text nodes again, just don't draw them.

Because of the repeating, I put the shading and the path with the text node into macros.

Because the nodes partially overlap, you have to create a new layer on top for the texts. Otherwise, the repeated shading goes over some parts of the text.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows}
\pgfdeclarelayer{text}
\pgfsetlayers{main,text}
\shade[bottom color=gray!10, top color=gray!80] (0,0) --++ (5,0) --++ (0,4) --++ (-5,0) -- cycle;
}
\newcommand{\pathmacro}[1]{%
\path [dashed,#1] (5,\y) -- (0,2) node(nod\c)[midway,sloped] {$h_\c(t)$};
}
\begin{document}
\begin{tikzpicture}

\draw (0,0) --++ (5,0) --++ (0,4) --++ (-5,0) -- cycle;

\foreach \y [count =\c] in {0.5,1.5,2.5,3.5}{
\draw [-open triangle 60 reversed,thick](5,\y) --++ (1,0) --++ (0,0.5) node[below right]{$s_\c(\theta_0, t)$};
\pathmacro{draw}
}

\foreach \y/\couleur [count = \c] in {0.5/red,1.5/purple,2.5/violet,3.5/blue}{
\pgfmathsetseed{\y+11}
\draw[\couleur] plot[domain=0:1.5, samples = 500] (\x-5,{rand*0.5*sin(\x*2000)*exp(-3.5*(-\x+1.5))+\y});
\draw (-6,\y) node {$h_{\c}^*(-t)$};
\draw [->, shorten >=1cm](-3,\y) -- (0,2);
}

\draw (-0.5,2) circle (0.4) node {\Large $\Sigma$};
\foreach \y [count =\c] in {0.5,1.5,2.5,3.5}
{
\begin{scope}
\clip(nod\c.south west)--(nod\c.north west)--(nod\c.north east)--(nod\c.south east)--cycle;
\end{scope}
\begin{pgfonlayer}{text}
\pathmacro{}
\end{pgfonlayer}
}

\end{tikzpicture}
\end{document}


-
Thank you, it works very well. I though the solution would be simpler, I'm not used to scope and clip commands. That's a good opportunity to take a look at it –  Thomas Feb 28 at 21:03

You can also use the connection node decoration to interrup path and avoid any drawing for the node. The inner xsep defines how tight the spacing around the text of the node should be. The rest is pretty much the same.

\documentclass[tikz]{standalone}
\usetikzlibrary{arrows,decorations.markings}

\begin{document}

\begin{tikzpicture}[mydeco/.style = {
decoration={markings,
mark connection node=my node,
mark=at position .5 with
{\node [sloped,inner xsep=2pt] (my node) {#1};}
},
decorate
}
]
\shade[bottom color=gray!10, top color=gray!80] (0,0) --++ (5,0) --++ (0,4) --++ (-5,0) -- cycle;
\draw (0,0) --++ (5,0) --++ (0,4) --++ (-5,0) -- cycle;

\foreach \y [count =\c] in {0.5,1.5,2.5,3.5}{
\draw [-open triangle 60 reversed,thick](5,\y) --++ (1,0) --++ (0,0.5) node[below right]{$s_\c(\theta_0, t)$};
\draw [dashed,mydeco=$h_\c(t)$] (0,2) -- (5,\y);
}

\foreach \y/\couleur [count = \c] in {0.5/red,1.5/purple,2.5/violet,3.5/blue}{
\pgfmathsetseed{\y+11}
\draw[\couleur] plot[domain=0:1.5, samples = 500] (\x-5,{rand*0.5*sin(\x*2000)*exp(-3.5*(-\x+1.5))+\y});
\draw (-6,\y) node {$h_{\c}^*(-t)$};
\draw [->, shorten >=1cm](-3,\y) -- (0,2);
}
\draw (-0.5,2) circle (0.4) node {\Large $\Sigma$};

\end{tikzpicture}
\end{document}


-
Thanks, that's a simple and efficient solution. –  Thomas Mar 1 at 8:36