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 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?

share|improve this question

1 Answer 1

up vote 6 down vote accepted

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}
share|improve this answer
    
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 at 4:53

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.