pgfplots .provide 样式类似于 tikzset 的 .provide 样式

pgfplots .provide 样式类似于 tikzset 的 .provide 样式

我一直在tikzset{style name/.provide style={}}使用是否有类似 \providetikzstyle 和 \providecommand 的东西?定义为

\tikzset{/handlers/.provide style/.code={%
    \pgfkeysifdefined{\pgfkeyscurrentpath/.@cmd}{}%
        {\pgfkeys {\pgfkeyscurrentpath /.code=\pgfkeysalso {#1}}}%
}}

并且发现它非常方便。 的类似版本是什么\pgfplotsset? 以下不是似乎有效:

\pgfplotsset{/handlers/.provide style/.code={%
    \pgfkeysifdefined{\pgfkeyscurrentpath/.@cmd}{}%
        {\pgfkeys {\pgfkeyscurrentpath /.code=\pgfkeysalso {#1}}}%
}}

下面的 MWE 生成左侧的图像,但是一旦.provide style工作就应该生成右侧的图像\pgfplotsset(即,没有x刻度标签和粗红色框):

在此处输入图片描述

参考:

代码:

\documentclass[border=2pt]{standalone}

\usepackage{pgfplots}

% https://tex.stackexchange.com/questions/22640/is-there-something-like-providetikzstyle-similar-to-providecommand
\tikzset{/handlers/.provide style/.code={%
    \pgfkeysifdefined{\pgfkeyscurrentpath/.@cmd}{}%
        {\pgfkeys {\pgfkeyscurrentpath /.code=\pgfkeysalso {#1}}}%
}}

%% How to define this???
\pgfplotsset{/handlers/.provide style/.code={%
    \pgfkeysifdefined{\pgfkeyscurrentpath/.@cmd}{}%
        {\pgfkeys {\pgfkeyscurrentpath /.code=\pgfkeysalso {#1}}}%
}}

\begin{document}
\pgfplotsset{tick label style/.provide style={% output should be as if this was ".style" instead of ".provide style"
    xticklabels={},
    draw=red, 
    ultra thick,
}}

%% This should be ignored as it was already defined above.
\pgfplotsset{tick label style/.provide style={
    xticklabels={},
    yticklabels={},
    draw=blue, ultra thick,
}}

\begin{tikzpicture}
\begin{axis}[%
    tick label style,% <---- y ticklabels shown, x ticklabels suppressed, axis in red
]
    \addplot {x^2};
\end{axis}
\end{tikzpicture}
\end{document}

相关内容