1

In tikz a user would declare new layers using:

\pgfdeclarelayer{foo}
\pgfdeclarelayer{bar}
\pgfsetlayers{foo,bar}

My library needs to declare some layers (just 2 for now), but I'm afraid to have conflict with user layers. Indeed, \pgfsetlayers erases all previously created layers. How could I solve that, ideally transparently for the user?

MWE

\documentclass[10pt]{article}
\usepackage{geometry}
\usepackage{tikz}

% My library would declare \pgfdeclarelayer{background} \pgfdeclarelayer{foreground} \pgfsetlayers{background,main,foreground}

% The user would declare this, and erase above layers. \pgfdeclarelayer{foo} \pgfdeclarelayer{bar} \pgfsetlayers{foo,bar} %% <-- erases background,main,foreground.

\begin{document}

% The user wants to draw \begin{figure} \begin{tikzpicture} \begin{pgfonlayer}{bar} \node[fill=green]{Hello}; \end{pgfonlayer} \begin{pgfonlayer}{foo} \node[fill=blue!50!white,circle] at (0.5,0) {Bye}; \end{pgfonlayer} \end{tikzpicture} \end{figure}

% The library wants to draw \begin{figure} \begin{tikzpicture} \begin{pgfonlayer}{foreground} \node[fill=green]{Hello}; \end{pgfonlayer} \begin{pgfonlayer}{background} \node[fill=blue!50!white,circle] at (0.5,0) {Bye}; \end{pgfonlayer} \end{tikzpicture} \end{figure}

\end{document}

tobiasBora
  • 8,684
  • 2
    basically you can't. You can check at a suitable place if \pgf@layerlist contains your layers, you could also (re)add your layers, but imho if there are really various user layers it wouldn't e easy to decide where to add your layers exactly. Do you really need layers? – Ulrike Fischer Oct 08 '21 at 08:26
  • Thanks for the advise. How would you add your own layer it it's not present? I do need one layers (background) to place arrows below nodes in some pictures, so I can always add my layer behind. For now I'm using this to specify the layer on node https://tex.stackexchange.com/questions/20425/z-level-in-tikz/20426#20426 – tobiasBora Oct 08 '21 at 09:03
  • As a hack, I would use layer names that are very unlikely to be used by users. This could be a pain in your library creation but could prevent them to be erased by user. – SebGlav Oct 08 '21 at 09:23
  • What do you mean? Pgfsetlayers remove all layer not listed, so my layer is going to be removed anyway. – tobiasBora Oct 08 '21 at 09:43

1 Answers1

2

Since \pgfsetlayers acts locally, just never use it globally. Always put it inside the environment or group where it is to be used.

\documentclass[10pt]{article}
\usepackage{geometry}
\usepackage{tikz}

% My library would declare \pgfdeclarelayer{background} \pgfdeclarelayer{foreground}

% The user would declare this, and erase above layers. \pgfdeclarelayer{foo} \pgfdeclarelayer{bar}

\begin{document}

% The user wants to draw \begin{figure} \pgfsetlayers{foo,bar} %% <-- erases background,main,foreground. \begin{tikzpicture} \begin{pgfonlayer}{bar} \node[fill=green]{Hello}; \end{pgfonlayer} \begin{pgfonlayer}{foo} \node[fill=blue!50!white,circle] at (0.5,0) {Bye}; \end{pgfonlayer} \end{tikzpicture} \end{figure}

% The library wants to draw \begin{figure} \pgfsetlayers{background,main,foreground} \begin{tikzpicture} \begin{pgfonlayer}{foreground} \node[fill=green]{Hello}; \end{pgfonlayer} \begin{pgfonlayer}{background} \node[fill=blue!50!white,circle] at (0.5,0) {Bye}; \end{pgfonlayer} \end{tikzpicture} \end{figure}

\end{document}

John Kormylo
  • 79,712
  • 3
  • 50
  • 120