Motivation
As stated in the title, I'm trying to define a command able, from a list of colors as input, to create a list in which colors are numbered. This, manually, has been done in Arrows coordinates in TikZ
Preliminary work
I've tried to search in the site questions related to the problem, such as How to define macros in a foreach loop with effects between iterations and after the loop without using global?, How to define macros in a foreach loop with effects between iterations and after the loop without using global? (especially solutions shown in Martin's comment). But I can't figure out a solution when the definition is based on \@namedef
The minimal-non-working-example
This is what I tried:
\documentclass[11pt,a4paper]{article}
\usepackage{tikz}
\makeatletter
\newcommand{\setcolorlist}[1]{
\foreach \listitem [count=\i] in {#1}{
\global\let\@tempa\@namedef{color@\i}{\listitem}
}
\@tempa
}
% Command in which colors are necessary
\newcommand{\drawcoloredarrows}[1]{
\foreach \i in {#1}{
\edef\mycolor{\@nameuse{color@\i}}
\draw[\mycolor,-stealth] (0,-\i)--(2,-\i);
}
}
\makeatother
\begin{document}
\setcolorlist{red,cyan,blue,green}
\begin{tikzpicture}
\drawcoloredarrows{1,...,4}
\end{tikzpicture}
\end{document}
getting:
How it should work
This is how it should work:
\documentclass[11pt,a4paper]{article}
\usepackage{tikz}
\makeatletter
\@namedef{color@1}{red}
\@namedef{color@2}{cyan}
\@namedef{color@3}{blue}
\@namedef{color@4}{green}
\newcommand{\setcolorlist}[1]{
\foreach \listitem [count=\i] in {#1}{
\global\let\@tempa\@namedef{color@\i}{\listitem}
}
\@tempa
}
% Command in which colors are necessary
\newcommand{\drawcoloredarrows}[1]{
\foreach \i in {#1}{
\edef\mycolor{\@nameuse{color@\i}}
\draw[\mycolor,-stealth] (0,-\i)--(2,-\i);
}
}
\makeatother
\begin{document}
%\setcolorlist{red,cyan,blue,green}
\begin{tikzpicture}
\drawcoloredarrows{1,...,4}
\end{tikzpicture}
\end{document}
getting: