# Problem with fill and cycle

Can someone tell me why the rectangle is not filled? So basically, why "cycle does not work?" I dont get it..

\documentclass{article}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}

\tikzstyle{circ} = [circle, minimum width=2.0mm, inner sep=0pt,draw,fill]
\tikzstyle{num}  = [yshift=3mm]
\node[circ] (a) at (0.0, 0.0) {};
\node[circ] (b) at (1.5, 0.0) {};
\node[circ] (c) at (2.3, 1.0) {};
\node[circ] (d) at (0.8, 1.0) {};
\path[draw, line width=0.3mm,top color=gray!20,bottom color=white] (a)
node[num]{1} -- (b) node[num]{2} -- (c) node[num]{3} -- (d) node[num]{4} -- cycle;

\end{tikzpicture}

\end{document}


Ok, after following the linked thread I rewrote. Now it works, but i seems like an awfull lot of code for such a small picture..

\begin{tikzpicture} % , show background rectangle

\tikzstyle{circ} = [circle, minimum width=2.0mm, inner sep=0pt,draw,fill]
\tikzstyle{num}  = [yshift=3mm]
\coordinate (a) at (0.0, 0.0);
\coordinate (b) at (1.5, 0.0);
\coordinate (c) at (2.3, 1.0);
\coordinate (d) at (0.8, 1.0);
\path[draw, line width=0.3mm,top color=gray!40,bottom color=white] (a)    node[num]{1} -- (b) node[num]{2} -- (c) node[num]{3} -- (d) node[num]{4} -- cycle;
\node[circ]  at (a) {};
\node[circ]  at (b) {};
\node[circ]  at (c) {};
\node[circ]  at (d) {};

\end{tikzpicture}

-
There was a duplicate for this but I can't find it. In a nutshell, you are drawing 4 separate paths instead of a one continous path. You can see that by using (a.center) -- (b.center) ... etc. then it would fill but that would be over your nodes. –  percusse Nov 7 '12 at 22:20
Ah maybe this one : tex.stackexchange.com/questions/78731/… And Welcome to TeX.sx! –  percusse Nov 7 '12 at 22:23
Usually, we don't put a greeting or a "thank you" in our posts. While this might seem strange at first, it is not a sign of lack of politeness, but rather part of our trying to keep everything very concise. Upvoting is the preferred way here to say "thank you" to users who helped you. –  Kurt Nov 7 '12 at 22:34
With your link I got it working, although its a whole lot of code going from \coordinate to \path, then to \node, for such a small picture. I cant post a reply, right? Or How I changed the code? –  Johannes Nov 7 '12 at 22:40
Here's another question that's fairly similar: tex.stackexchange.com/q/4057/86 –  Loop Space Nov 7 '12 at 23:02

I couldn't find a duplicate although I'm sure there is one but here is a brief picture.

Usually when TikZ receives a fill command or option it looks at the last piece of path and tries to close it automatically with or without the cycle option. This is simply because there must be an area to fill. But when there are only two points and a fill directive, it shrugs and goes OK, whatever, your loss.

Here is a simple example.

\begin{tikzpicture}
\fill[red,ultra thick] (0,0) -- (1,1);
\fill[blue,ultra thick] (0,0) -| (1,-1);
\end{tikzpicture}


As you can see the red fill doesn't care but the blue fill is visible because it has a proper area to fill (the corner is automatically created via orthogonal -|).

The issue with the nodes are based on this problem. If you connect two nodes with a border shape (circle, rectangle etc.) directly they are connected with a line that starts from points on their borders. Let me try to demonstrate it via putting colored links such that line segments are visible.

\begin{tikzpicture}
\node[draw,circle] (a) at (0,0) {A};
\node[draw] (b) at (2,1) {B};
\node[draw] (c) at (2,-1) {C};
\draw[ultra thick,blue!20] (a) -- (b);
\draw[ultra thick,green] (b) -- (c);
\draw[ultra thick,red] (c) -- (a);
\draw (a) -- (b) -- (c) -- (a);
\end{tikzpicture}


All these colored links are standalone and disjoint line segments. And you can see that the last black seemingly closed path is not actually a continuous path. It starts on one border and ends on the other. Then a brand new path is started. That's why we can't fill inside because the last path to be filled is from (c) to (a) hence two points and hence no area to be colored. This is also the same reason why cycle doesn't work because it goes back to (c) since the last part started at the border of (c).

So you have to use coordinates for example if I use coordinates instead of nodes then they don't have any borders and they are just points. It will be a continuous path. Let's try

\begin{tikzpicture}
\node[draw,circle] (a) at (0,0) {A};
\node[draw] (b) at (2,1) {B};
\node[draw] (c) at (2,-1) {C};
\draw[ultra thick,blue!20] (a) -- (b);
\draw[ultra thick,green] (b) -- (c);
\draw[ultra thick,red] (c) -- (a);
\fill (a.center) -- (b.center) -- (c.center);
\end{tikzpicture}


And Bob's your uncle. That's why you need a little more that just going around via nodes. It's slightly disappointing but I can assure you that this is actually a good thing which you might appreciate more and more when you get a hang of it :)

-
Great explanation, thanks. Will read the FAQ more careful and try to thank you:-) –  Johannes Nov 8 '12 at 7:23

This is just a complement to @percusse's elaborate answer: You may use positioning and background libraries.

\documentclass{article}
\usepackage{tikz}

\begin{document}
\usetikzlibrary{positioning,backgrounds}

\begin{tikzpicture}

\tikzstyle{circ} = [circle, minimum width=2.0mm, inner sep=0pt,draw,fill]
\tikzstyle{num}  = [yshift=3mm]
\node[circ] (a) at (0.0, 0.0) {};
\node[circ] (b) at (1.5, 0.0) {};
\node[circ] (c) at (2.3, 1.0) {};
\node[circ] (d) at (0.8, 1.0) {};
\begin{pgfonlayer}{background}
\fill[draw, line width=0.3mm,top color=gray!20,bottom color=white] (a.center)
node[num]{1} -- (b.center) node[num]{2} -- (c.center) node[num]{3} -- (d.center) node[num]{4} -- (a.center);
\end{pgfonlayer}
\end{tikzpicture}

\end{document}


-
Also thank you for this nice sample! –  Johannes Nov 8 '12 at 7:24

This is in response to the comment:

but i seems like an awfull lot of code for such a small picture..

TikZ is quite verbose in its syntax. Usually, that's a good thing as it helps make it clear what's going on. However, it can sometimes seem a bit too much in which case it can be useful to know a few techniques for condensing it a little. Here's an example of that for this code. It's not the most concise, as I've tried to ensure that it is still comprehensible. The main thing that I've done is to collapse all the drawing to a single path. The reason that this works is because nodes are drawn after the path that they are on (whereupon the drawing won't occlude the nodes) but their coordinates are available straight away. I've also used labels to do several things at once. By doing that, I can define the coordinate, draw the circle, and place the number.

\documentclass{article}
%\url{http://tex.stackexchange.com/q/81848/86}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}

\tikzset{
circ/.style={
label={[circlabel=#1]center:{}},
},
circlabel/.style={
circle,
minimum width=2.0mm,
inner sep=0pt,
draw,
fill,
label={above:#1},
},
}
line width=0.3mm,
top color=gray!20,
bottom color=white
]
(0.0, 0.0)
coordinate[circ=1] (a)
--
(1.5, 0.0)
coordinate[circ=2] (b)
--
(2.3, 1.0)
coordinate[circ=3] (c)
--
(0.8, 1.0)
coordinate[circ=4] (d)
--
cycle;
\end{tikzpicture}

\end{document}


Defining it all on one path avoids the issue with the non-continuous path since the nodes are not used in defining the actual path. As I've done it above then the coordinates (a), (b), (c), (d) refer to their exact points meaning that in later calls they will work properly: \fill (a) -- (b) -- (c) -- (d) -- cycle; will do what you want it to. If you aren't going to use these coordinates again (so their only purpose was in defining this shape) then you could skip the naming step and condense the code even further.

\documentclass{article}
%\url{http://tex.stackexchange.com/q/81848/86}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}

\tikzset{
circ/.style={
circle,
minimum width=2.0mm,
inner sep=0pt,
draw,
fill,
label={above:#1},
},
}
line width=0.3mm,
top color=gray!20,
bottom color=white
]
(0.0, 0.0)
node[circ=1] {}
--
(1.5, 0.0)
node[circ=2] {}
--
(2.3, 1.0)
node[circ=3] {}
--
(0.8, 1.0)
node[circ=4] {}
--
cycle;

\end{tikzpicture}

\end{document}


Lastly, since the labels follow a sequence, we can automate them.

\documentclass{article}
%\url{http://tex.stackexchange.com/q/81848/86}
\usepackage{tikz}
\newcounter{clabel}

\begin{document}

\begin{tikzpicture}

\tikzset{
c/.style={
insert path={
node[
circle,
minimum width=2.0mm,
inner sep=0pt,
draw,
fill,
label={above:\stepcounter{clabel}\theclabel},
] {}
}
},
reset clabel/.code={%
\setcounter{clabel}{0}%
}
}
reset clabel,
line width=0.3mm,
top color=gray!20,
bottom color=white,
]
(0.0, 0.0)
[c]
--
(1.5, 0.0)
[c]
--
(2.3, 1.0)
[c]
--
(0.8, 1.0)
[c]
--
cycle;

\end{tikzpicture}

\end{document}


All of these produce exactly the same picture.

-