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 latex code below does not compile. This is due to \foreach and the function \getcolor. Does anybody know, how I can use the variable set by \foreach in a function.

Find below the code

\documentclass{article}
\usepackage{pgfplots}
\usepackage[debug,pdftex]{insdljs}
\usetikzlibrary{patterns}


\def\Colorarray{{1}{blue}{2}{red}{3}{orange}{4}{yellow}}
\def\getcolor#1{\expandafter\xgetcolor\Colorarray{#1}{}test{#1}}
\def\xgetcolor#1#2#3test#4{\ifnum#4=#1 #2\else\xgetcolor#3test{#4}\fi}

\pgfkeys{/pgf/number format/.cd, set decimal separator={,{\!}}, set thousands separator={}}

\newcommand{\doAxeX}{
  \begin{axis}[ xmin=0,xmax=1800, %ymin=0, ymax=250, 
                            axis y line*=right, axis x line*=bottom, axis x line=none,
                            y tick label style={font=\tiny, color=green}, 
                            ylabel style={at={(1.3,0.5)},anchor=center},
                            ylabel={\color{green}$Load$}]
      \addplot[smooth,green] table[x index=0,y index=5] {S1SearchArticle.dat};
  \end{axis}
}

\begin{document}
\begin{tikzpicture}[scale=1]
\doAxeX
    \begin{axis}[title={Evolution du temps page pour le scénario}, grid=major, axis x line=bottom, axis y line=left,
          xlabel={Durée du test}, ylabel={Page time(s)}, 
          x tick label style={font=\tiny, rotate=35},
          y tick label style={font=\tiny},
          x label style={font=\small},
          y label style={font=\small},
          legend entries={\xarrayget{Page}{#1}},legend style={font=\tiny, at={(1.5,1)}}]
            \node [opacity=0.3] at (axis description cs:0.17,0.9) {\includegraphics[scale=0.5]{image001.png}};
            \foreach \c in {1,2,...,4} {
                    \addplot[smooth,\getcolor{\c}] table[x index=0,y index=\c] {S1SearchArticle.dat};
    \end{axis}
\end{tikzpicture}

\end{document}
share|improve this question
    
Welcome to TeX.sx! A tip: If you indent lines by 4 spaces or enclose words in backticks `, they'll be marked as code, as can be seen in my edit. You can also highlight the code and click the "code" button (with "{}" on it). –  Joseph Wright Jul 13 '12 at 7:31
1  
Could you please turn your code into a minimal example that does not rely on external files (use some dummy data) and only includes the parts necessary for illustrating the problem? –  Jake Jul 13 '12 at 7:34
2  
I think what you're trying to do is much more easily achieved using the plot cycle list feature of PGFPlots, instead of using a collection of arrays and macros. See this answer for an example: tex.stackexchange.com/a/50878/2552 –  Jake Jul 13 '12 at 7:42
    
Would need an MWE to see exactly what is going on here. But at a minimum I think you're missing the close bracket for foreach. –  kgr Jul 13 '12 at 17:29
add comment

2 Answers

Here a minimal working example with \foreach and \getcolor

\documentclass[11pt]{scrartcl}
\usepackage{tikz}

\begin{document}

\def\Colorarray{{1}{blue}{2}{red}{3}{orange}{4}{yellow}}
\def\getcolor#1{\expandafter\xgetcolor\Colorarray{#1}{}test{#1}}
\def\xgetcolor#1#2#3test#4{\ifnum#4=#1 #2\else\xgetcolor#3test{#4}\fi}

\begin{tikzpicture}

\foreach \c in {1,2,...,4} {
         \node[draw,\getcolor{\c}] at (\c,\c){\c};}
\end{tikzpicture}
\end{document} 

enter image description here

share|improve this answer
add comment

Altermundus's loop is interesting but it isn't tail recursion.

\documentclass[crop]{standalone}
\usepackage{graphicx}
\usepackage{tikz}
\makeatletter
\def\colorarray{{1}{blue}{2}{red}{3}{orange}{4}{green}{5}{magenta}}
\def\getcolor#1{\expandafter\xgetcolor\colorarray{#1}{black}\@nil{#1}}
\def\xgetcolor#1#2#3\@nil#4{%
  \ifnum#4=#1 \expandafter\@firstoftwo\else\expandafter\@secondoftwo\fi
  {#2}{\xgetcolor#3\@nil{#4}}%
}
\makeatother

\begin{document}
\scalebox{.5}{%
  \begin{tikzpicture}
  \foreach \c in {1,2,...,7}{%
    \node[draw,thick,\getcolor{\c}] at (\c,\c){\c};
    \node[draw,thick,\getcolor{\c}] at (\c,-\c){\c};
    \draw[<->,very thick,\getcolor{\c}]
      \pgfextra{\pgfmathparse{\c-.25}\let\tempa\pgfmathresult}
      (\c,-\tempa)--(\c,\tempa);
  }
  \end{tikzpicture}
}
\end{document} 

enter image description here

share|improve this answer
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.