I'm working on an illustration of a particular multiscale analysis method.

\begin{tikzpicture}[
scale=1.5,
circ/.style = {
circle,
draw,
black!75,
outer sep=0pt,
inner sep=1pt
}
]

\begin{scope}[step=0.5, very thin, black!30]
\draw [xshift=-2.5cm] (-0.9, -1) grid (1, 1);
\draw [] (-1, -1) grid (1, 1);
\draw [xshift=2.5cm] (-1, -1) grid (0.9, 1);
\end{scope}

\filldraw[fill=blue!40!white, fill opacity=0.5, draw=blue!50!white, very thin] (-0.5, -1) rectangle (0.5, 1);

\foreach \y in {-1, -0.5, 0, 0.5, 1} {
\foreach \x in {-0.5, 0, 0.5} {
\node [circ, fill=black!75] (enode) at (\x, \y) {};
}
\foreach \x in {-1.5, -1, 1, 1.5} {
\node [circ, fill=white] (bnode) at (\x, \y) {};
}
}
\end{tikzpicture}


The above code produces the following result:

I'm now, out of general interest and in an attempt to become better at TikZ, trying to put the finishing touches to the figure and try and fade the five hanging horizontal line segments of the left and right subdomains to white without affecting the rest of the subdomain.

My attempt to reproduce the answer by Ilhan Polat leads to an error that the counter variable \x is undefined. Another answer, by Jean-Christophe Dubacq, to a similar question does not work as expected — I assume this is because the grid is not centered and transform-ing the fade doesn't work either.

Could someone please lend a hand and explain how to do this in an elegant way, or, at all? Thanks in advance.

• Welcome to TeX.SE. For future reference, while code snippets are useful in explanations, it is always best to compose a fully compilable MWE that illustrates the problem including the \documentclass and the appropriate packages so that those trying to help don't have to recreate it. This is especially important for tikz as there are numerous libraries and sometimes this takes time to figure out. – Peter Grill Aug 28 '18 at 18:46
• @PeterGrill Thank you. In the future I will make sure that the MWE compiles directly. – user169291 Aug 28 '18 at 19:38

Not, sure if this is what you are looks for, but with the \usetikzlibrary{fadings} you can use path fading=west on the lines on the left and path fading=east on the right hand side

    \begin{scope}[step=0.5, very thin, black!30]
\draw [xshift=-2.5cm, path fading=west] (-0.9, -1) grid (1, 1);
\draw [] (-1, -1) grid (1, 1);
\draw [xshift=2.5cm, path fading=east] (-1, -1) grid (0.9, 1);
\end{scope}


to achieve:

If you just want the hanging parts to fade, not the squares, then draw them separately using:

\begin{scope}[step=0.5, very thin, black!30]
\draw [xshift=-2.5cm] (-0.5, -1) grid (1, 1);
\draw [xshift=-2.5cm, path fading=west] (-0.9, -1) grid (-0.5, 1);
\draw [] (-1, -1) grid (1, 1);
\draw [xshift=2.5cm] (-1, -1) grid (0.5, 1);
\draw [xshift=2.5cm, , path fading=east] (0.5, -1) grid (0.9, 1);
\end{scope}


which yields:

Code:

\documentclass[border=2pt]{standalone}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}[
scale=1.5,
circ/.style = {
circle,
draw,
black!75,
outer sep=0pt,
inner sep=1pt
}
]

\begin{scope}[step=0.5, very thin, black!30]
\draw [xshift=-2.5cm, path fading=west] (-0.9, -1) grid (1, 1);
\draw [] (-1, -1) grid (1, 1);
\draw [xshift=2.5cm, path fading=east] (-1, -1) grid (0.9, 1);
\end{scope}

\filldraw[fill=blue!40!white, fill opacity=0.5, draw=blue!50!white, very thin]
(-0.5, -1) rectangle (0.5, 1);

\foreach \y in {-1, -0.5, 0, 0.5, 1} {
\foreach \x in {-0.5, 0, 0.5} {
\node [circ, fill=black!75] (enode) at (\x, \y) {};
}
\foreach \x in {-1.5, -1, 1, 1.5} {
\node [circ, fill=white] (bnode) at (\x, \y) {};
}
}
\end{tikzpicture}
\end{document}

• Thank you for your reply. I managed to achieve this on my own and honestly prefer what this looks like over what I wanted. However, out of interest: How would I do this "without affecting the rest of the subdomain", ie. make only the hanging segments fade out and not the other squares, as stated in the original post? I cannot find anything like this even in the Tikz / PGF manual. – user169291 Aug 28 '18 at 19:42
• @user169291: I am not sure I understand the difference between what is shown here and "make only the hanging segments fade out and not the other squares". Please clarify. – Peter Grill Aug 28 '18 at 20:44
• @user169291: Have updated the answer. Is that what you were looking for? – Peter Grill Aug 28 '18 at 20:52
• Yes, drawing them separately is what I was looking for; I submitted an answer to my own question. – user169291 Aug 28 '18 at 21:06

I figured out how to do this on my own but prefer the look of the answer by Peter Grill. Simply create another column and apply the fade to them:

The artifact on the right does not appear in the PDF. Here's the MWE:

\documentclass{standalone}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}[
scale=1.5,
circ/.style = {
circle,
draw,
black!75,
outer sep=0pt,
inner sep=1pt
}
]

\begin{scope}[step=0.5, very thin, black!30]
\draw [xshift=-3.5cm, path fading=west] (-0.5, -1) grid (0, 1);
\draw [xshift=-2.5cm] (-1, -1) grid (1, 1);
\draw [] (-1, -1) grid (1, 1);
\draw [xshift=2.5cm] (-1, -1) grid (1, 1);
\draw [xshift=3.5cm, path fading=east] (0, -1) grid (0.5, 1);
\end{scope}

\filldraw[fill=blue!40!white, fill opacity=0.5, draw=blue!50!white, very thin] (-0.5, -1) rectangle (0.5, 1);

\foreach \y in {-1, -0.5, 0, 0.5, 1} {
\foreach \x in {-0.5, 0, 0.5} {
\node [circ, fill=black!75] (enode) at (\x, \y) {};
}
\foreach \x in {-1.5, -1, 1, 1.5} {
\node [circ, fill=white] (bnode) at (\x, \y) {};
}
}
\end{tikzpicture}
\end{document}


It should also be possible to create custom shadings and clip them to accomplish an analogous effect when working on more complex shapes where such tricks do not apply.

Edit: Upon further review, this is the same as employed here by Dubacq. I misread.

• You probably can clip the fading but still need to draw the non-faded part separately. So, not sure there is any benefit in trying to clip the fading. – Peter Grill Aug 28 '18 at 21:02