There are several reasons, why this does not work:
\lx1 is not a command name, it consists of the command \lx, followed by digit 1.
Either replace 1 by a letter or the usage is a little more complex via \csname.
\x and \y contain the x and y values, the definition of \x defines \x, not the macro inside and \y whill change with every loop. \expandafter expands a macro one level:
\expandafter\def\x{...}
Doing the same with \y requires lots of more \expandafter, therefore using \let
is easier here.
\foreach puts the loop body inside groups, thus local definitions will be lost.
This can be solved by global definitions.
Example:
\usepackage{pgffor}
\foreach \x/\y in {\lxA/1,\lyA/2,\lzA/3}
{%
\global\expandafter\let\x\y
}
Or with \lx1 as macro:
\foreach \x/\y in {\lx1/1,\ly1/2,\lz1/3}
{%
\begingroup
\escapechar=-1 % suppresses backslash with \string
\global\expandafter\let
\csname \expandafter\string\x\endcsname\y
\endgroup
}
\typeout{\expandafter\string\csname lx1\endcsname:
\expandafter\meaning\csname lx1\endcsname}
Addition for the fans of \expandafter:
\gdef can be used instead of \global\let by using additional \expandafter:
\foreach \x/\y in {\lxA/1,\lyA/2,\lzA/3}
{%
\expandafter\expandafter\expandafter
\gdef\expandafter\x\expandafter{\y}%
}
\typeout{\string\lxA: \meaning\lxA}
\foreach \x/\y in {\lx1/1,\ly1/2,\lz1/3}
{%
\begingroup
\escapechar=-1 % suppresses backslash with \string
\expandafter\gdef
\csname \expandafter\string\x\expandafter\endcsname
\expandafter{\y}%
\endgroup
}
\typeout{\expandafter\string\csname lx1\endcsname:
\expandafter\meaning\csname lx1\endcsname}
As can be seen, the version with \let is more efficient and elegant.
\lx1,\ly1and\lz1before entering\foreach? Moreover, you cannot use macros as-is with numbers in it. – Werner Aug 27 '12 at 2:32