# Shading applied only to the first element with foreach

I want to apply the same shading to multiple rectangles. Here is a MWE of the code I use (for a sample of 3 rectangles):

\documentclass{article}

\usepackage[letterpaper,textwidth=8.5in,textheight=11in]{geometry}
\usepackage{lscape}

\usepackage{tikz}

\begin{document}
\pagestyle{empty}
\begin{landscape}
\begin{figure}
\centering
\begin{tikzpicture}[scale=1]

\coordinate(A) at (1.5,4);

\foreach \x in {0,1,2}
\shadedraw [thick, left color=white, right color=black] (A) ++(6*\x,0) rectangle +(1,-0.35);

\end{tikzpicture}
\end{figure}
\end{landscape}
\end{document}


Nevertheless, here is what I get with this code:

Only the first rectangle is shaded... How can I solve this problem?

• ++ is not a calculation specification but denotes a relative coordinate (which all following relative coordinate specifications are relative to, in contrast to +). Compare to (A) -- ++ (6*\x,0). Jun 5 '15 at 0:34

This is indeed rather esoteric but the action (A) ++(x,y) is actually counting the (A) as a part of the path and what you see is the whole fading strip punched and viewed through 3 holes. Because each of them actually takes (A) into account for the fading even though you don't draw anything in between.

You can fix it via ([xshift=6*\x cm]A) or with calc library syntax ($(A)+(6*\x,0)$) and so on. As long as you don't modify the path pen you are good to go.

• I usually don't like to mix canvas and xy coordinates: I'd use either ([shift=(0:6*\x)A) or more likely ([shift=(A)]6*\x,0) (the shift=(A) part can also be added to the path then). Jun 4 '15 at 23:41
• @Qrrbrbirlbel why not ? Jun 4 '15 at 23:55
• If the x vector has been changed (say x=2cm or x=(30:1cm)) then (6*\x,0) and (6*\x cm,0) will not be the same anymore. Jun 5 '15 at 0:13
• @Qrrbrbirlbel True but shift would be the same so that is up to OPs liking. I just mean that (A) should be inside the coordinate calculation not in the pen movetos. Am I missing your point? Jun 5 '15 at 0:17
• No, ([xshift=<value> cm]<sth>) is not the same as ([xshift=(0:<value>)<sth>), except when x=(0:1cm) (the default). In my eyes, a fundamental difference in TikZ/PGF. But, yes, that doesn't concern OP's problem (which you have identified correctly). Jun 5 '15 at 0:25

Just playing...

\documentclass[tikz,border=5pt]{standalone}

\begin{document}
\begin{tikzpicture}

\coordinate(A) at (1.5,4);
\foreach \x in {0,1,2}
\shadedraw [thick, left color=white, right color=black] (A) ++(6*\x,0) rectangle +(1,-0.35);

\coordinate(b0) at (-4.5,2);
\path [draw, thick, left color=white, right color=black] foreach \x [count=\i] in {0,1,2} {(b\x |- b0) ++(6,0) coordinate (b\i) rectangle ++(1,-0.35)};

\coordinate(C) at (1.5,0);
\foreach \x [count=\i] in {0,1,2}
\shadedraw [thick, left color=white, right color=black] ([xshift=6*\x cm]C) rectangle +(1,-0.35);

\coordinate(D) at (1.5,-2);
\foreach \x in {0,1,2}
\path (D) ++(6*\x,0) node [minimum height=3.5mm, minimum width=10mm, inner sep=0pt, anchor=north west, thick, draw, left color=white, right color=black] {};

\end{tikzpicture}
\end{document}