通过环境将 xticklabels 传递给 tikzpicture

通过环境将 xticklabels 传递给 tikzpicture

我有一些图表需要在文档中重复多次,但略有变化,因此我想创建一个环境来生成图表并将变化作为选项传递。但是,我在传递 xticks 和 xticklabels 时遇到了麻烦,我想是因为它们是列表。

这是我的第一次尝试,但无法编译:

\documentclass{article}

\usepackage{pgfplots}
\usepackage{tikz}
\usepackage{environ}

\makeatletter
\newcommand{\@tikzexamplecommand}[2][]{%
  \pgfqkeys{/pgfplots}{remainingkeys/.style={}}%
  \pgfqkeys{/tikzangle}{%
    % save unknown keys in /pgfplots/remainingkeys; code from
    % http://tex.stackexchange.com/questions/22952/how-do-you-pass-unknown-keys-as-options-to-a-tikz-picture
    .unknown/.code={%
      \let\currname\pgfkeyscurrentname%
      \let\currval\pgfkeyscurrentvalue%
      \ifx#1\pgfkeysnovalue%
      \pgfqkeys{/pgfplots}{remainingkeys/.append style/.expand once={\currname}}%
      \else%
      \pgfqkeys{/pgfplots}{remainingkeys/.append style/.expand twice={\expandafter\currname\expandafter=\currval}}%
      \fi%
    },%
    #1}%
  % 
  \begin{tikzpicture}
    \begin{axis}[xticklabels=auto, /pgfplots/remainingkeys]
      \addplot+[domain=-10:10] {x};
      #2
    \end{axis}
  \end{tikzpicture}%
}

\NewEnviron{tikzexample}[1][]{\@tikzexamplecommand[#1]{\BODY}}
\makeatother

\begin{document}

\begin{tikzexample}[xtick={-5, 5}, xticklabels={$\alpha$, $\beta$}]
\end{tikzexample}

\end{document}

因此,我想我会在我的环境中创建 xtick 和 xticklabels 键并明确传递它们:

\documentclass{article}

\usepackage{pgfplots}
\usepackage{tikz}
\usepackage{environ}

\makeatletter
\newcommand{\tikzexample@xtick}{auto}
\newcommand{\tikzexample@xticklabels}{auto}
\pgfkeys{/tikzangle/.cd,% to set the path
  xtick/.initial=auto,
  xtick/.store in=\tikzexample@xtick,
  xtick/.get=\tikzexample@xtick,
  xticklabels/.initial=auto,
  xticklabels/.store in=\tikzexample@xticklabels,
  xticklabels/.get=\tikzexample@xticklabels,
}

\newcommand{\@tikzexamplecommand}[2][]{%
  \pgfqkeys{/pgfplots}{remainingkeys/.style={}}%
  \pgfqkeys{/tikzangle}{%
    % save unknown keys in /pgfplots/remainingkeys; code from
    % http://tex.stackexchange.com/questions/22952/how-do-you-pass-unknown-keys-as-options-to-a-tikz-picture
    .unknown/.code={%
      \let\currname\pgfkeyscurrentname%
      \let\currval\pgfkeyscurrentvalue%
      \ifx#1\pgfkeysnovalue%
      \pgfqkeys{/pgfplots}{remainingkeys/.append style/.expand once={\currname}}%
      \else%
      \pgfqkeys{/pgfplots}{remainingkeys/.append style/.expand twice={\expandafter\currname\expandafter=\currval}}%
      \fi%
    },%
    #1}%
  % 
  \begin{tikzpicture}
    \begin{axis}[xtick=\tikzexample@xtick, xticklabels=\tikzexample@xticklabels, /pgfplots/remainingkeys]
      \addplot+[domain=-10:10] {x};
      #2
    \end{axis}
  \end{tikzpicture}%
}

\NewEnviron{tikzexample}[1][]{\@tikzexamplecommand[#1]{\BODY}}
\makeatother

\begin{document}
\begin{tikzexample}[xtick={-5, 5}, xticklabels={$\alpha$, $\beta$}]

\end{tikzexample}
\end{document}

这给出了正确的 xticks,但 {$\alpha$, $\beta$} 被读作一个刻度标签:

输出

我应该怎么做才能将 xticklabels 正确传递到我的环境?

答案1

在深入研究了 pgfplots 代码后,我想出了这个解决方案。我实际上不明白这有什么用,但添加以下键处理代码似乎有效:

  xticklabels/.code={%
    \pgfplotslistnew\tikzexample@xticklabels{#1}%
    \let\pgfplots@xticklabels=\tikzexample@xticklabels
    \let\pgfplots@xticklabel=\pgfplots@user@ticklabel@list@x    
  },

以下是完整示例:

\documentclass{article}

\usepackage{pgfplots}
\usepackage{tikz}
\usepackage{environ}

\makeatletter

\newcommand{\tikzexample@xtick}{auto}
\pgfkeys{/tikzexample/.cd,% to set the path
  xtick/.initial=auto,
  xtick/.store in=\tikzexample@xtick,
  xtick/.get=\tikzexample@xtick,
  xticklabels/.code={%
    \pgfplotslistnew\tikzexample@xticklabels{#1}%
    \let\pgfplots@xticklabels=\tikzexample@xticklabels
    \let\pgfplots@xticklabel=\pgfplots@user@ticklabel@list@x    
  },
}

\newcommand{\@tikzexamplecommand}[2][]{%
  \pgfqkeys{/pgfplots}{remainingkeys/.style={}}%
  \pgfqkeys{/tikzexample}{%
    % save unknown keys in /pgfplots/remainingkeys; code from
    % http://tex.stackexchange.com/questions/22952/how-do-you-pass-unknown-keys-as-options-to-a-tikz-picture
    .unknown/.code={%
      \let\currname\pgfkeyscurrentname%
      \let\currval\pgfkeyscurrentvalue%
      \ifx#1\pgfkeysnovalue%
      \pgfqkeys{/pgfplots}{remainingkeys/.append style/.expand once={\currname}}%
      \else%
      \pgfqkeys{/pgfplots}{remainingkeys/.append style/.expand twice={\expandafter\currname\expandafter=\currval}}%
      \fi%
    },%
    #1}%
  \begin{tikzpicture}
    \begin{axis}[xtick=\tikzexample@xtick, /pgfplots/remainingkeys]
      \addplot+[domain=-10:10] {x};
      #2
    \end{axis}
  \end{tikzpicture}%
}

\NewEnviron{tikzexample}[1][]{\@tikzexamplecommand[#1]{\BODY}}
\makeatother

\begin{document}
\begin{tikzexample}[xtick={-5, 5}, xticklabels={$\alpha$, $\beta$}]

\end{tikzexample}
\end{document}

相关内容