# TikZ: “Inline” pgfonlayer? [duplicate]

Possible Duplicate:
“Z-level” in TikZ

When using layers in tikz I usually do the following:

\begin{pgfonlayer}{background}
\draw[red] (0,0)--(1,0);
\end{pgfonlayer}


Is there a way of using the layers inline? e.g.:

\draw[layer=background, red] (0,0) -- (1,0);

-

## marked as duplicate by Andrew Stacey, Joseph Wright♦Jan 5 '12 at 22:30

I don’t know a way for this. My workaround is creating new commands for layers:

\documentclass{minimal}

\usepackage{tikz}
\pgfdeclarelayer{background}
\pgfsetlayers{main,foreground}
\newcommand{\onlayer}[2]{%
\begin{pgfonlayer}{#1}%
#2%
\end{pgfonlayer}%
}
\newcommand{\bglayer}[1]{%
\begin{pgfonlayer}{background}%
#1%
\end{pgfonlayer}%
}

\begin{document}
\begin{tikzpicture}
\fill (0,0) rectangle (2,2);
\bglayer{\fill [gray] (1,1) rectangle (3,3);}
\end{tikzpicture}
\end{document}


It’s not as comfortable as your inline version but better (shorter) than using the pgfonlayer environment …

Edit Another way is shown in "Z-level" in TikZ

-
Accepted because of "Another way is shown in "Z-level" in TikZ" –  romeovs Jan 2 '12 at 10:17