在 tikz 中,用户可以使用以下方式声明新图层:
\pgfdeclarelayer{foo}
\pgfdeclarelayer{bar}
\pgfsetlayers{foo,bar}
我的库需要声明一些层(目前只有 2 个),但我担心会与用户层发生冲突。事实上,\pgfsetlayers
会删除所有之前创建的层。我该如何解决这个问题,最好对用户透明?
平均能量损失
\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}
答案1
由于\pgfsetlayers
它在本地起作用,所以永远不要在全局使用它。始终将其放在要使用它的环境或组中。
\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}