5

I have some diagrams that need to be repeated numerous times in a document with slight variations, so I thought I would make an environment that generates the diagrams and pass the variations as options. However, I'm having trouble passing xticks and xticklabels, I think because they're lists.

This was my first try, which does not compile:

\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}

So then I thought I would make xtick and xticklabels keys in my environment and pass them on explicitly:

\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}

This gives the right xticks, but {$\alpha$, $\beta$} is read as one tick label:

output

What should I do to be able to pass xticklabels properly to my environment?

1 Answer 1

3

After digging into the pgfplots code, I came up with this solution. I don't actually understand what this does, but it seems to work to add the following key-handling code:

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

Here's the full example:

\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}
1
  • 1
    Nice solution! You should email the StackExchange team via the "Contact Us" link at the bottom of the page to see if they can combine your accounts :-)
    – darthbith
    Feb 20, 2015 at 1:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .