I am using pdflatex to document as my program runs. My program prints out some tikz commands in a file, which is compiled by pdflatex offline. In some cases the order of the commmands is not what I like, because the events in my program triggering printing tikz commands occur in the order which is dictated by the program logic, rather than in the order appropriate for preparing the plot. For example, I put a help grid on the figure, but I can find out the size of the grid only when the program is about to terminate, so the help grid will be on the top of other objects; or I am putting some labels on top of a filled rectangle, before the rectangle is drawn. I can work with the figure, but for publication, only for beauty, I need to rearrange manually the order of the objects in the prepared file. Can I use layers for this goal, i.e. I would need three layers, to prepare the plot in a way that my program, in addition to the commands, also names the target layer, and at the end I arrange the three layers, as I want to plot them?