Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question
    
Actually there are already the environments semilogxaxis, semilogyaxis and loglogaxis. Why can't you use them? –  Claudio Fiandrino Jul 28 '12 at 17:16
    
I can't change them dynamically. I'll update my MWE –  Thomas Rebele Jul 28 '12 at 17:23
add comment

2 Answers 2

up vote 4 down vote accepted

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.

share|improve this answer
    
Thanks, it works great :) also thanks for the explanation (If I want to change anything other than xmode/ymode I could use another if-defined style) –  Thomas Rebele Jul 30 '12 at 9:26
add comment

You can supply it to the TikZ picture instead of the axis. But it's indeed strange to see that this doesn't work. Probably it's due to the \pgfkeys handling but I couldn't find the problem.

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{mystyle/.style={/pgfplots/xmode=log,/pgfplots/ymode=log}}

\begin{document}
\begin{tikzpicture}[every axis/.style={/pgfplots/mystyle}]
\begin{axis}
\addplot coordinates {(10, 100) (100, 15) (2000, 200)};
\end{axis}
\end{tikzpicture}
\end{document}
share|improve this answer
    
thanks, this works also, but Christian Feuersänger's answer is more general –  Thomas Rebele Jul 30 '12 at 9:28
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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