1

Is it possible to define a tikz style to access the background layer by adding a style option to the node instead of having to put the entire node inside the environment? Here is an example code:

\documentclass[tikz]{standalone}

\usepackage{tikz}    
\usetikzlibrary{backgrounds}

\begin{document}

\pgfdeclarelayer{bg}
\pgfsetlayers{bg,main}

\tikzset{
  background/.style={%
    execute at begin node={\begin{pgfonlayer}{bg}},
    execute at end node={\end{pgfonlayer}}
  }
}

\begin{tikzpicture}

\draw[thick,red] (-1,0) -- (1,0);

\begin{pgfonlayer}{bg}
\node[circle,fill=blue,label={blue node behind red line, good}] at (0,0){};
\end{pgfonlayer}

\draw[thick,red] (-1,-1) -- (1,-1);
\node[circle,fill=blue,background,label={blue node above red line, bad}] at (0,-1){};

\end{tikzpicture}

\end{document}

As you see in my example, I already tried to add a custom style but with no effect. Honestly, I do not really understand how to define new styles and this is more the result of copy-pasting, so it might be pointing in a totally wrong direction.

  • 1
  • I did not know about this question. Thanks! I think that with this approach you can define the relative position while drawing a path. I am looking for a way to access the background layer. Meaning: you have an independent \draw and \node[background] command, not \draw ... node[behind path] – BR123 Mar 8 '18 at 14:04
  • I just realized that there are two questions posted. I was referring to tex.stackexchange.com/questions/416792. But tex.stackexchange.com/questions/20425/z-level-in-tikz seems to go in the right direction and I am currently checking it out. Thank you! – BR123 Mar 8 '18 at 14:35
2

Thanks to the comment by Torbjørn T. and Loop Space's answer (https://tex.stackexchange.com/a/20426) I was able to find a correctly working version:

\documentclass[tikz]{standalone}

\pgfdeclarelayer{bg}
\pgfdeclarelayer{bbg}
\pgfsetlayers{bbg,bg,main}

%%% see https://tex.stackexchange.com/a/20426
\makeatletter
\pgfkeys{%
  /tikz/on layer/.code={
    \pgfonlayer{#1}\begingroup
    \aftergroup\endpgfonlayer
    \aftergroup\endgroup
  },
  /tikz/node on layer/.code={
    \gdef\node@@on@layer{%
      \setbox\tikz@tempbox=\hbox\bgroup\pgfonlayer{#1}\unhbox\tikz@tempbox\endpgfonlayer\egroup}
    \aftergroup\node@on@layer
  },
  /tikz/end node on layer/.code={
    \endpgfonlayer\endgroup\endgroup
  }
}
\def\node@on@layer{\aftergroup\node@@on@layer}
\makeatother
%%%

\begin{document}   

\begin{tikzpicture}

\draw[thick,red] (-1,0) -- (1,0);

\begin{pgfonlayer}{bg}
\node[circle,fill=blue,label={blue node behind red line, good}] at (0,0){};
\end{pgfonlayer}
\begin{pgfonlayer}{bbg}
\draw[thick,black] (-1,-0.1) -- (1,-0.1);
\end{pgfonlayer}

\draw[thick,red] (-1,-1) -- (1,-1);
\node[circle,fill=blue,node on layer=bg,label={blue node above red line, bad}] at (0,-1){};
\draw[thick,black,on layer=bbg] (-1,-1.1) -- (1,-1.1);

\end{tikzpicture}

\end{document}

enter image description here

Note: I added a black line to show the different syntax for \node[node on layer=...] and \draw[on layer=...].

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.