8

I have to create a set of groupplots with pgfplots and want to use one centralized definition of the plot style (width, height, x-axis label, etc.). It seemed to me that this should be easily done by collecting the definitions in a file that I then input. The MWE is as follows:

\documentclass[10pt]{article}
\usepackage{tikz}
\usepackage{fancyhdr}
\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}
\usepackage[active,tightpage]{preview}
\PreviewEnvironment{tikzpicture}
\pgfplotsset{compat=1.12}
\begin{document}
\thispagestyle{empty}
\begin{tikzpicture}
\begin{groupplot}[
  width=6.0cm,height=4.0cm, 
  group style={group size=1 by 2},
  xmin=0.0,xmax=1.0,xlabel=$x$,
%  \input{style.tex} % does not work
  ]
  \nextgroupplot[ymin=0.0,ymax=1.0,ylabel={$y_2$}]
  \addplot table {
   0.1 0.9
   0.9 0.1
  }; 
  \nextgroupplot[ymin=0.0,ymax=1.0,ylabel={$y_1$}]
  \addplot table {
   0.1 0.2
   0.9 0.8
  }; 
\end{groupplot}
\end{tikzpicture}
\end{document} 

The file style.tex contains the following:

width=6.0cm,height=4.0cm, 
group style={group size=1 by 2},
xmin=0.0,xmax=1.0,xlabel=$x$,

If I run the above MWE, pgfplots works as intended. If I comment out the lines that are reproduced in style.tex and comment in the line containing the \input command, I get the following error:

! Use of \pgfplots@@environment@groupplot doesn't match its definition.
\pgfutil@ifnextchar ...1\def \pgfutil@reserved@a {
                                                  #2}\def \pgfutil@reserved@...
l.17   ]

How come? I thought that the \input command is equivalent to typing out the commands in the file being input.

user1362373
  • 2,859
  • Common option of group plot put in \pgfplotsset{...} which (for local use) should folow \begin{tikzpicture}. – Zarko Dec 27 '15 at 19:43

2 Answers2

6

I think the best way is to define a style with \pgfplotsset{SomeStyleName/.style={<options>}}. Note that using group style={...} directly in the definition of the new style doesn't work (see Group style key does not work in pgfplotsset), which is why I use the more verbose group/<key>=<value>.

\documentclass[border=3mm]{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}
\pgfplotsset{compat=1.12,
    MyPlotStyle/.style={
       width=6.0cm,
       height=4.0cm,
       xmin=0.0,
       xmax=1.0,
       xlabel=$x$,
       group/group size=1 by 2
       }
}
\begin{document}
\begin{tikzpicture}
\begin{groupplot}[
MyPlotStyle
  ]
  \nextgroupplot[ymin=0.0,ymax=1.0,ylabel={$y_2$}]
  \addplot table {
   0.1 0.9
   0.9 0.1
  }; 
  \nextgroupplot[ymin=0.0,ymax=1.0,ylabel={$y_1$}]
  \addplot table {
   0.1 0.2
   0.9 0.8
  }; 
\end{groupplot}
\end{tikzpicture}
\end{document} 

enter image description here

Torbjørn T.
  • 206,688
  • Nice solution, however it make sense, when you have more figures with the common options. – Zarko Dec 27 '15 at 20:46
  • Thanks for the hint about using pgfplotsset, but your answer does not address my actual question - why can I not use \include? – user1362373 Dec 29 '15 at 05:56
  • 3
    @user1362373 Keyval input is not expanded as it is parsed (LaTeX's \input is not expandable anyway). As such, this will never work: the parser doesn't see the content of your file, it sees \input... which then ends up in the wrong places (treated I think as a colour). – Joseph Wright Dec 29 '15 at 10:40
5

Try something as follows:

\documentclass[10pt]{article}
\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}
\usepackage[active,tightpage]{preview}
\PreviewEnvironment{tikzpicture}
\pgfplotsset{compat=1.12}

    \begin{document}
\thispagestyle{empty}
\begin{tikzpicture}
    \pgfplotsset{width=6.0cm,height=4.0cm,  %<-- group plot "style": common options
                 group style = {group size=1 by 2},
                 xmin = 0,  xmax = 1,   xlabel=$x$,
                 ymin = 0,  ymax = 1}
\begin{groupplot}
  \nextgroupplot[ylabel={$y_2$}]
  \addplot table {
   0.1 0.9
   0.9 0.1
  };
  \nextgroupplot[ylabel={$y_1$}]
  \addplot table {
   0.1 0.2
   0.9 0.8
  };
\end{groupplot}
\end{tikzpicture}
    \end{document}

In above code I use only necessary packages.

enter image description here

Zarko
  • 296,517