Declaring layers causes tikzpicture to disappear

After fixing the problems highlighted in wh1t3's comment, there is still one that remains. The answer comes from Section 82.3 of the PGF manual (Using Layers):

Note: You can not add anything to the main layer using this environment. The only way to add anything to the main layer is to give graphic commands outside all {pgfonlayer} environments.

The main layer is not treated in the same way as the other layers. The layering system works by putting the commands for each layer into boxes and then placing those boxes in the right order. It's possible to not put stuff in boxes and everything that isn't in a box goes on the main layer. So the lay-out mechanism is:

  1. Process all the boxes listed before main in the order given.
  2. Process all non-boxed stuff.
  3. Process all the boxes listed after main in the order given.

When you use \begin{pgfonlayer}{main} then it looks for a box in which to put the stuff. For some unknown reason, it does actually put the stuff in a box (it really ought to flash a big neon sign saying "You're trying to put stuff on the main layer using pgfonlayer. DO NOT DO THIS.") but then that box gets shoved in the corner like last year's Christmas decorations and never looked at again.

So have another go but be sure not to use the main layer explicitly. If you still encounter problems, either edit the question to explain what those are or ask a new question.


Actually, the problem is having erroneous spaces in the pgfsetlayers command. Replace

\pgfsetlayers{background, main, foreground}

with

\pgfsetlayers{background,main,foreground}

and all should be fine!