# [Tex/LaTex] pgfplots – axis xmode/ymode in user-defined style

pgfplotstikz-styles

I would like to specify xmode and ymode for the axis with \pgfplotsset (or any other way, so that the mode can be changed by an if statement) The error message which I get is:

! Package pgfplots Error: Sorry, you can't change /pgfplots/xmode' in this con
text. Maybe you need to provide it as \begin{axis}[/pgfplots/xmode=...] ?.


Here is a minimal working example:

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.5}

\def\someoption{1} % change this to zero to see the error

\ifcase\someoption % this is the reason why I want to define xmode with styles
\pgfplotsset{/pgfplots/mystyle/.style={
/pgfplots/xmode=log, % this is not working
/pgfplots/ymode=log % this too
}}
\else
\pgfplotsset{/pgfplots/mystyle/.style={}}
\fi

\begin{document}

\section*{with style: (not working)}

\begin{tikzpicture}
\begin{axis}[/pgfplots/mystyle]
\addplot coordinates {(10, 100) (100, 15) (2000, 200)};
\end{axis}
\end{tikzpicture}

% how it should look like

\section*{with hard-coded axis properties:}

\begin{tikzpicture}
\begin{axis}[xmode=log,ymode=log]
\addplot coordinates {(10, 100) (100, 15) (2000, 200)};
\end{axis}
\end{tikzpicture}

\end{document}


This is related to Axis limits in user-defined style, but this doesn't help in this case.

You are encountering an internal limitation of pgfplots - that's why it is claiming that you cannot change the key in your context (it's not a bug, it's a feature).

The reason is key filtering: pgfplots extracts the xmode and ymode keys from the input argument list first to decide which default configuration set (every loglog axis etc) should be activated. To this end, it sets only options which belong to the key family /pgfplots/scale. Without this restriction, keys might be set in wrong contexts.

If you can ensure that your style mystyle contains nothing more than xmode and/or ymode, you can solve the problem using

\ifcase\someoption % this is the reason why I want to define xmode with styles
\pgfplotsset{/pgfplots/mystyle/.style={
/pgfplots/xmode=log, % this is not working
/pgfplots/ymode=log % this too
},
mystyle/.belongs to family=/pgfplots/scale,% <----
}
\else
\pgfplotsset{/pgfplots/mystyle/.style={}}
\fi


if your style sets more than that, you should at least ensure that it contains only changes to other styles (like ticklabel style={...}`).

This is not documented in the manual. I am not sure if it should be.