9

Is there any way to use LaTeX commands inside \pgfplotsset?

For instance, the following code produces an error indicated in the \pgfplotsset line, whereas if one replaced \contents in the \pgfplotsset command with axis x line=bottom then all would be well.

\documentclass{article}


\usepackage{pgfplots}
\newcommand{\contents}{axis x line=bottom}

\begin{document}
\begin{tikzpicture}

\pgfplotsset{\contents}  % <----- fails with ! Package pgfkeys Error: I do not know the key '/pgfplots/axis x line=bottom'

\begin{axis}
\addplot[domain=-3e-3:3e-3,samples=201]{exp(-x^2 / (2e-3^2)) / (1e-3 * sqrt(2*pi))};
\end{axis}
\end{tikzpicture}

\end{document}
8

Probably Till Tantau was having similar problems so he created the error messages in a smarter way. Example; if I go something like this

\tikz[xasfwr=yeah]{}

The error message is

Package pgfkeys Error: I do not know the key /tikz/xasfwr, to which you passed yeah, and I am going to ignore it. Perhaps you misspelled it.

Now your example; let's read the error message

Package pgfkeys Error: I do not know the key /pgfplots/axis x line=bottom

Notice that it doesn't say you tried to pass bottom it says that it didn't understand the whole thing. Because it didn't parse it and missed the equation sign and the value you have provided. You can get more stupid cases such as

\pgfplotsset{\contents=out haha so funny}

Package pgfkeys Error: I do not know the key /pgfplots/axis x line=bottom, to which you passed out haha so funny, and I am going to ignore it.

These all tell that PGF keys didn't expand it properly. One immediate thing is to manually expand the argument by delaying the expandion of \pgfplotsset{ part

\expandafter\pgfplotsset\expandafter{\contents}

would work. Or you can still stay within PGF keys and use the /.expanded handler which does the expansion for you.

\pgfkeys{/utils/exec/.expanded={\noexpand\pgfplotsset{\contents}}}

What does this do? First, /utils/exec is the general handler for executing arbitrary TeX code. Then by just identifying what should not be expanded, here \pgfplotsset and adding \noexpand before them, you can set any kind of macro values.

You can also control how many times it should expand and you can check the manual for expand once and expand twice keys for that.

6

You did mention in a comment that you are writing a small lua Script to output the \pgfplotsset command. Writing a simple texfile is easy, which can be read after the environment (here called foobar) closes.

\begin{filecontents}{\jobname-pgf.tex}
\pgfplotsset{width=5cm}
% I am the file your LUA script makes
\end{filecontents}
\documentclass{article}
\usepackage{pgfplots}
\newenvironment{foobar}{}{}
\usepackage{etoolbox}
\AfterEndEnvironment{foobar}{\input{\jobname-pgf}}
\begin{document}
\begin{tikzpicture}
\begin{axis}
\addplot{x+1};
\end{axis}
\end{tikzpicture}

\begin{tikzpicture}
\begin{foobar}
    %Some luacode wrting a file represented by filecontents
\end{foobar}
%this file is being read here ^
%pgfplotsset is executed
\begin{axis}
\addplot{x^2};
\end{axis}
% the whole set is limited to the scope of the tikzpicture
\end{tikzpicture}

\begin{tikzpicture}
    \begin{axis}
\addplot{-x^2};
\end{axis}
\end{tikzpicture}
%everything back to normal herea
\end{document}

You should define your definitions as styles and later reuse them.

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{contents/.style={axis x line=bottom}}
\begin{document}
\begin{tikzpicture}
\begin{axis}
\addplot[domain=-3e-3:3e-3,samples=201]{exp(-x^2 / (2e-3^2)) / (1e-3 * sqrt(2*pi))};
\end{axis}
\end{tikzpicture}
%\pgfplotsset{contents}%JB: That would change the behaviour
%globally

\begin{tikzpicture}
    \begin{axis}[contents]%JB: Just for this axis environment
\addplot[domain=-3e-3:3e-3,samples=201]{exp(-x^2 / (2e-3^2)) / (1e-3 * sqrt(2*pi))};
\end{axis}
\end{tikzpicture}
\end{document}
  • Thank you @Johannes_B, but see my comment on the question itself. – JPi Aug 6 '14 at 9:34
  • @JPi Just figuring out the problem. – Johannes_B Aug 6 '14 at 9:36
  • 1
    @JPi I have added a completely different approach. – Johannes_B Aug 6 '14 at 10:06
  • 1
    Thinking about it this is more or less another approach to your other question, but does not help here in any way. – Johannes_B Aug 6 '14 at 10:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.