无法将 \foreach 设置的变量用作函数中的变量

无法将 \foreach 设置的变量用作函数中的变量

下面的 latex 代码无法编译。这是由于\foreach和函数造成\getcolor的。有人知道我如何在函数中使用 \foreach 设置的变量吗?

找到下面的代码

\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}

答案1

这是一个最小的工作示例,\foreach其中\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} 

在此处输入图片描述

答案2

Altermundus 的循环很有趣,但它不是尾递归。

\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} 

在此处输入图片描述

相关内容