
我有一些圖表需要在文件中重複多次,並且略有變化,所以我想我應該創建一個環境來產生圖表並將變化作為選項傳遞。但是,我在傳遞 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}