![Tikz:如何避免库和用户之间的层冲突](https://linux22.com/image/440697/Tikz%EF%BC%9A%E5%A6%82%E4%BD%95%E9%81%BF%E5%85%8D%E5%BA%93%E5%92%8C%E7%94%A8%E6%88%B7%E4%B9%8B%E9%97%B4%E7%9A%84%E5%B1%82%E5%86%B2%E7%AA%81.png)
在 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}