4

I want to use groupplots, and set a variable to conditionally plot the plots. Here is the minimal non-working example:

\documentclass{article}
\usepackage{pgfplots}
\usetikzlibrary{calc}
\usepgfplotslibrary{groupplots}
\def\a{1}
\begin{document}
\begin{tikzpicture
  \begin{groupplot}[group style={group size=1 by \a},height=3cm,width=6cm]
    \ifnum\a>0{ %
    \nextgroupplot[title=One]
    \addplot coordinates {(0,0) (1,1) (2,2)};
    \ifnum\a>1{ %
    \nextgroupplot[title=Two]
    \addplot coordinates {(0,2) (1,1) (2,0)};
    \ifnum\a>2{ %
    \nextgroupplot[title=Three]
    \addplot coordinates {(0,2) (1,1) (2,1)};
    \ifnum\a>3{ %
    \nextgroupplot[title=Four]
    \addplot coordinates {(0,2) (1,1) (1,0)};
    }\fi
    }\fi
    }\fi
    }\fi
  \end{groupplot}
\node (title) at (0,0) {THE Title};
\end{tikzpicture}
\end{document}

This fails with:

! Extra }, or forgotten \endgroup.
l.24     }
          \fi

Not sure if it's because of the nested \ifnums, or because of \nextgroupplot; if the first \nextgroupplot is commented, error is now:

! Undefined control sequence.
l.11     \addplot
                  coordinates {(0,0) (1,1) (2,2)};

Can such a conditional plotting of group plots be done?

1 Answer 1

6

Drop the use of groups inside the groupplot construction. I've added some indentation, and corrected the small type (missing } at the beginning of the tikzpicture environment):

enter image description here

\documentclass{article}
\usepackage{pgfplots}
\usetikzlibrary{calc}
\usepgfplotslibrary{groupplots}
\def\a{3}
\begin{document}
\begin{tikzpicture}
  \begin{groupplot}[group style={group size=1 by \a},height=3cm,width=6cm]
    \ifnum\a>0
      \nextgroupplot[title=One]
      \addplot coordinates {(0,0) (1,1) (2,2)};
      \ifnum\a>1
        \nextgroupplot[title=Two]
        \addplot coordinates {(0,2) (1,1) (2,0)};
        \ifnum\a>2
          \nextgroupplot[title=Three]
          \addplot coordinates {(0,2) (1,1) (2,1)};
          \ifnum\a>3
            \nextgroupplot[title=Four]
            \addplot coordinates {(0,2) (1,1) (1,0)};
          \fi
        \fi
      \fi
    \fi
  \end{groupplot}
\end{tikzpicture}
\end{document}
1
  • Fantastic, @Werner - works like a charm! :) I used the groups in the first place, because I was afraid \ifnum wouldn't work with groups of commands without them - but your solution shows it actually works fine like that. Many thanks!
    – sdaau
    Mar 22, 2014 at 4:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .