
문서에 약간의 변형을 가하여 여러 번 반복해야 하는 다이어그램이 있으므로 다이어그램을 생성하고 변형을 옵션으로 전달하는 환경을 만들어야겠다고 생각했습니다. 그러나 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}