Why do I get "! You can't use a prefix with `the character 0'." when using \ifnum?
The problem isn't in the \ifnum
, but in the use of \global\valueC
. The use of \global
is intended for use with definitions or assignments, to specify the scope of the definition or assignment. So \global\def...
or \global\let...
are typical. Since \valueC
is not a definition or an assignment, the syntax \global\valueC
does not mean anything useful.
I am not sure what was intended by \global\valueC
, but if you mean to make the definition of \valueC
global, you can use \global\let\valueC\valueC
. [I also changed \item
to \xitem
, because using an already defined command makes me squeamish]
\documentclass{article}
\usepackage{pgffor}
\newcommand\addcolor[1]{%
\foreach \xitem [count=\step] in {#1} {%
\ifnum\step=1 \pgfmathsetmacro\valueC{\xitem}\fi%
\ifnum\step=2 \pgfmathsetmacro\valueM{\xitem}\fi%
\ifnum\step=3 \pgfmathsetmacro\valueY{\xitem}\fi%
\ifnum\step=4 \pgfmathsetmacro\valueK{\xitem}\fi%
\global\let\valueC\valueC\global\let\valueM\valueM%
\global\let\valueY\valueY\global\let\valueK\valueK%
}%
}%
\addcolor{0,20,100,0}
\begin{document}
\section{Current CMYK Values}
C:\valueC{} M:\valueM{} Y:\valueY{} K:\valueK
\end{document}
Joseph suggests an even more streamlined approach:
\documentclass{article}
\usepackage{pgffor}
\newcommand\addcolor[1]{%
\foreach \xitem [count=\step] in {#1} {%
\pgfmathsetmacro\temp{\xitem}
\ifnum\step=1 \global\let\valueC\temp\fi%
\ifnum\step=2 \global\let\valueM\temp\fi%
\ifnum\step=3 \global\let\valueY\temp\fi%
\ifnum\step=4 \global\let\valueK\temp\fi%
}%
}%
\addcolor{0,20,100,0}
\begin{document}
\section{Current CMYK Values}
C:\valueC{} M:\valueM{} Y:\valueY{} K:\valueK
\end{document}
Finally, here is a way with \listofitems
:
\documentclass{article}
\usepackage{pgffor,listofitems}
\newcommand\addcolor[1]{%
\setsepchar{,}%
\readlist\cvalues{#1}%
\readlist\clabels{C,M,Y,K}%
\readlist\cquence{1,2,3,4}%
\foreachitem\xitem\in\cquence{%
\pgfmathsetmacro\temp{\cvalues[\xitem]}%
\expandafter\global\expandafter\let\csname value\clabels[\xitem]\endcsname\temp%
}%
}%
\addcolor{0,20,100,0}
\begin{document}
\section{Current CMYK Values}
C:\valueC{} M:\valueM{} Y:\valueY{} K:\valueK
\end{document}
You're using a sledgehammer; anyway, \global\valueC
is illegal and should be
\global\let\valueC\valueC
But there's a better way:
\documentclass{article}
\usepackage{pgf}
\makeatletter
\newcommand\addcolor[1]{%
\add@color#1\@nil
}
\def\add@color#1,#2,#3,#4\@nil{%
\pgfmathsetmacro\valueC{#1}%
\pgfmathsetmacro\valueM{#2}%
\pgfmathsetmacro\valueY{#3}%
\pgfmathsetmacro\valueK{#4}%
}
\makeatother
\begin{document}
\addcolor{0,20,100,0}
C:\valueC \quad M:\valueM \quad Y:\valueY \quad K:\valueK
\addcolor{1,2*3,3+12,4}
C:\valueC \quad M:\valueM \quad Y:\valueY \quad K:\valueK
\end{document}
An even easier way:
\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand\addcolor{m}
{
\tl_set:Nx \valueC { \fp_eval:n { \clist_item:nn { #1 } { 1 } } }
\tl_set:Nx \valueM { \fp_eval:n { \clist_item:nn { #1 } { 2 } } }
\tl_set:Nx \valueY { \fp_eval:n { \clist_item:nn { #1 } { 3 } } }
\tl_set:Nx \valueK { \fp_eval:n { \clist_item:nn { #1 } { 4 } } }
}
\ExplSyntaxOff
\begin{document}
\addcolor{0,20,100,0}
C:\valueC \quad M:\valueM \quad Y:\valueY \quad K:\valueK
\addcolor{1,2*3,3+12,4}
C:\valueC \quad M:\valueM \quad Y:\valueY \quad K:\valueK
\end{document}