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.

The idea behind the following sample code is this: There should be four plots, and for each plot two parameters should be set. This is achieved through \def just before \nextgroupplot. Apparently, this approach is not sound.

\documentclass{beamer}
\setbeamertemplate{navigation symbols}{}

\usepackage{pgfplots}
\pgfplotsset{compat=1.7}
\usepgfplotslibrary{groupplots}

\begin{document}

\begin{frame}
  \begin{tikzpicture}
    \begin{groupplot}[group style={group size=2 by 2}, height=5cm, width=6cm]
      \def\myA{1}
      \def\myB{1}
      \nextgroupplot[xlabel = {A: \myA, B: \myB} ]
      \addplot coordinates{(0,0) (1,\myA) (2,\myB)};

      \def\myA{1}
      \def\myB{2}
      \nextgroupplot[xlabel = {A: \myA, B: \myB} ]
      \addplot coordinates{(0,0) (1,\myA) (2,\myB)};

      \def\myA{2}
      \def\myB{1}
      \nextgroupplot[xlabel = {A: \myA, B: \myB} ]
      \addplot coordinates{(0,0) (1,\myA) (2,\myB)};

      \def\myA{2}
      \def\myB{2}
      \nextgroupplot[xlabel = {A: \myA, B: \myB} ]
      \addplot coordinates{(0,0) (1,\myA) (2,\myB)};
    \end{groupplot}
  \end{tikzpicture}
\end{frame}
\end{document}

Here's the output of the above snippet: output of the above snippet.

If everything worked as planned, we would see four different figures, with different values at x=1 and x=2, labelled like this:

A: 1, B:1   A: 1, B:2
A: 2, B:1   A: 2, B:2

Neither works. All four plots appear to take their parameters from the very first definition only. For the labels, the order is changed (2-3-4-1 rather than 1-2-3-4).

Q: Why is that? What's the proper way to do what I mean to do?

Q: Why can't I write that code as

\documentclass{beamer}
\setbeamertemplate{navigation symbols}{}

\usepackage{pgfplots}
\pgfplotsset{compat=1.7}
\usepgfplotslibrary{groupplots}

\begin{document}

\def\myAs{1,2}
\def\myBs{1,2}

\begin{frame}
  \begin{tikzpicture}
    \begin{groupplot}[group style={group size=2 by 2}, height=5cm, width=6cm]
      \foreach\myA in \myAs{
        \foreach\myB in \myBs{
          \nextgroupplot[xlabel = {A: \myA, B: \myB} ]
          \addplot coordinates{(0,0) (1,\myA) (2,\myB)};
        }
      }
    \end{groupplot}
  \end{tikzpicture}
\end{frame}
\end{document}
share|improve this question
    
pgfplots doesn’t execute everything right away but collects the plots calculates stuff and then draws it. See also section 8.1 “Utility commands”, pp. 417ff., of the manual for this. –  Qrrbrbirlbel Aug 31 '13 at 1:29
add comment

1 Answer 1

up vote 2 down vote accepted

The pgfplots package doesn’t execute everything right away but does collect the plots first, calculated stuff and then actually draws something. With the etoolbox package you can use your two loops (at least in this example) pretty easily to build a macro that contains all Plots with expanded values \myA and \myB.

Code

\documentclass{beamer}
\setbeamertemplate{navigation symbols}{}

\usepackage{pgfplots,etoolbox}
\pgfplotsset{compat=1.7}
\usepgfplotslibrary{groupplots}

\begin{document}

\def\myAs{1,2}
\def\myBs{1,2}

\begin{frame}
  \begin{tikzpicture}
    \begin{groupplot}[group style={group size=2 by 2}, height=5cm, width=6cm]
      \def\myPlots{}
      \pgfplotsforeachungrouped \myA in {1,2}{
        \pgfplotsforeachungrouped \myB in {1,2}{
          \eappto\myPlots{%
            \noexpand\nextgroupplot[xlabel = {A: \myA, B: \myB} ]
            \noexpand\addplot coordinates{(0,0) (1,\myA) (2,\myB)};
          }
        }
      }
      \myPlots
    \end{groupplot}
  \end{tikzpicture}
\end{frame}
\end{document}
share|improve this answer
    
Thank you very much. That makes a lot of sense :) –  anonymous Aug 31 '13 at 16:54
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.