TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

(related but unequal question: Using Macro Defined Lists in TikZ/PGFplots)

I'm having several axis-environments inside the same tikzpicture. I would like to have some (but not all) share the same xmin and xmax, and some (but not all) share the same ymin and ymax. I tried to do that by defining it in my own style, in the same way that I set xlabel, ylabel, and other axis-specific styles.

Below is a minimal, not-working example. When I run it with pgfplots 2011/07/29 v1.5 (git show 1.5-1-gcc2f2d2 ), it fails with the error message:

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

I have two closely related questions:

  1. Why can I set xlabel, ylabel, ytick, but not ymin? Is there a fundamental reason for this, or is it just a lack of implementation?
  2. How can I achieve the effect of applying a particular set of xmin, xmax, ymin, ymax, to more than one axis?

This related question ( Using Macro Defined Lists in TikZ/PGFplots) might provide a working solution (q. 2), but does not answer my understanding-related question (q. 1).

Minimal (not) Working Example

        /pgfplots/ylabel={Spam (kg)},%
        /pgfplots/xlabel={Eggs (no.)},
        /pgfplots/ytick={0,10,...,200}, % this works
%        /pgfplots/xmax=3, this fails!
\addplot coordinates {(0, 0) (1, 1) (2, 2)};

\begin{axis}[spam, at={(5cm, 3cm)}]
\addplot coordinates {(-1, 0) (-1, 1) (1, 0)};
share|improve this question
The answer of cmhughes is good and should solve it. Here is just the missing piece of (technical) background for those who are wondering why it failed: Tikz styles are considered to be display styles. They are not evaluated for the survey phase of the axis, only for the display phase. Everything which might affect the survey phase (computing axis limits, for example), needs to be communicated to pgfplots - that is, by means of a style with the /pgfplots/ key prefix. Adding /pgfplots/spam/.style=... will solve the problem as good as \pgfplotsset{spam/.style...}. – Christian Feuersänger Nov 26 '11 at 13:08
Thanks; it was the why that I was still wondering about :) – gerrit Nov 26 '11 at 14:15
up vote 10 down vote accepted

Rather than defining your style in the first plot, you can define it in your pre-amble using \pgfplotsset; this allows you to put xmin, xmax, etc in your own style.



        ylabel={mystyle (kg)},
        xlabel={Eggs (no.)},


\addplot coordinates {(0, 0) (1, 1) (2, 2)};
\addplot coordinates {(-1, 0) (-1, 1) (1, 0)};


And if you want to inherit some of the properties from mystyle and overwrite others, then you can use, for example,

share|improve this answer
pgfplots really is beautiful – cmhughes Nov 25 '11 at 20:30
Side-note: you can get the same effect if you write \begin{tikzpicture}[/pgfplots/mystyle/.style=... . – Christian Feuersänger Nov 26 '11 at 13:05
@ChristianFeuersänger Thanks, that's good to know! And thank you for the amazing package :) – cmhughes Nov 26 '11 at 16:58

This partially answers your question but probably @Christian Feuersänger would answer this completely about why the limitation is in place.

I have used the following code :

\begin{tikzpicture}[every axis/.style={
ylabel={Spam (kg)},%
xlabel={Eggs (no.)}%

\begin{axis}\addplot coordinates {(0, 0) (1, 1) (2, 2)};\end{axis}

\begin{axis}[at={(10cm, 0cm)}]
\addplot coordinates {(-1, 0) (-1, 1) (1, 0)};

which results with

enter image description here

I am definitely guessing here but most probably, tikzpicture has no complete external access to the inner settings of axis environment. That's why you need to supply it directly to the axis environment options.

share|improve this answer

You can use a \def to add this in after nearly minimally


and then use it in an axis like


A better workaround is to use a macro

\def\spam{ylabel={Spam (kg)}, xlabel={Eggs (no.)}, ytick={0,10,...,20}, xmax={3}}

which can be used with the plot in almost the way you want


These (I hope) answer your second question, but obviously not your first. I have no ideas on that front...

share|improve this answer
It's also messy in a way you obviously don't want... – qubyte Nov 25 '11 at 15:34
actually, the idea is quite good. In fact, \pgfplotsset{lims/.style={xmax=<num>,ymax=<num>}} does the same: it defines a macro which sets the specified keys as soon as it is used. The only difference is that \pgfkeys (the backend for \pgfplotsset) only integrates the style definition better into the key setting stuff. see also tex.stackexchange.com/questions/2699/… – Christian Feuersänger Nov 26 '11 at 22:18

Your Answer


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.