# Using LaTeX commands inside pgfplotsset

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}

• – Johannes_B Aug 6 '14 at 9:27
• @Johannes_B: good point generally, thanks. The reason I want to do this is to address a problem raised in tex.stackexchange.com/questions/195029/… which I don't think I can solve easily by using styles, but I'll look into it further. – JPi Aug 6 '14 at 9:32
• it needs to be expanded so it is \expandafter\pgfplotsset\expandafter{\contents} – percusse Aug 6 '14 at 11:00

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.

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.

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}
\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}
\end{axis}
% the whole set is limited to the scope of the tikzpicture
\end{tikzpicture}

\begin{tikzpicture}
\begin{axis}
\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
• @JPi I have added a completely different approach. – Johannes_B Aug 6 '14 at 10:06
• 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