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.

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

The following MWE works nicely when I remove the optional argument [\forcol] from the \addplotcommand or when I change it to red, for example. Though I would like the curves to have different colors as defined in my list {1/black,2/red,3/green,4/blue,5/orange}, so I tried it as the following, but then it throws me the error

"! Undefined control sequence. \end{axis}"

\documentclass{article}
\usepackage{xcolor}
\usepackage{pgfplots}
\begin{document}
  \begin{tikzpicture}
    \begin{axis}
      \foreach \foridx/\forcol in {1/black,2/red,3/green,4/blue,5/orange}{
            \addplot[\forcol]{x^\foridx};
      }
    \end{axis}
  \end{tikzpicture}
\end{document}

What is my mistake? I would like to have this feature to set up a command for plotting many data files.

share|improve this question
3  
referencing section 8 of the manual, you can use \pgfplotsforeachungrouped \foridx/\forcol in {1/black,2/red,3/green,4/blue,5/orange}{ \addplot[\forcol]{x^\foridx}; \edef\temp{\noexpand\addplot[\forcol]{x^\foridx};} \temp } – cmhughes Mar 28 at 14:29
1  
@cmhughes: great, this works, thanks! You can create an answer of this! But I don't really understand this "magic" (even with the description of the manual), what the "extra groups" are that are not introduced here. – riddleculous Mar 28 at 14:37
    
@cmhughes: I just realize you had the ` \addplot[\forcol]{x^\foridx};` also outside your temp environment. This should be omitted. – riddleculous Mar 28 at 17:11
up vote 10 down vote accepted

You can try some \expandafter... statements to expand the \foreach loops. \foreach loops can be quite tricky, in my point of view, due to the way the \forcol macros are defined by pgffor.

\documentclass{article}
\usepackage{xcolor}
\usepackage{pgfplots}
\begin{document}
  \begin{tikzpicture}
    \begin{axis}
     \pgfplotsforeachungrouped \foridx/\forcol in {1/black,2/red,3/green,4/blue,5/orange}{
            \expandafter\addplot\expandafter[\forcol]{x^\foridx};
      }
    \end{axis}
  \end{tikzpicture}
\end{document}

enter image description here

share|improve this answer
    
i simply removed [\forcol] and the plot showed. but it has a lot of points in it. – naphaneal Mar 28 at 14:40
1  
@riddleculous: It can be really complicated with \expandafters, in my point of view, in nested \foreach loops and multiple \addplot[\foo,\forbar,\morefoo]{...} – Christian Hupfer Mar 28 at 14:53
3  
Your and cmhughes code are not the same. You are both expanding the color command (which is needed), but cmhughes also avoid the grouping around the \addplots. I would suggest to use \pgfplotsforeachungrouped in your code too. – Ulrike Fischer Mar 28 at 14:53
1  
@UlrikeFischer: Thanks, I've replaced \pgfplotsforeachungrouped instead of the usual \foreach – Christian Hupfer Mar 28 at 15:00
1  
@riddleculous: The axis environment defers a lot work to the end (e.g. the generation of the legend), and the addplots are not independant (see e.g. the cycle list, labels, maximum etc), so I would avoid to separate them. – Ulrike Fischer Mar 28 at 15:07

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.