I have the following defined:
\newcommand{\pl}{\ensuremath{p^L}\xspace}
\newcommand{\pr}{\ensuremath{p^R}\xspace}
\newcommand{\xx}{\ensuremath{_{XX}}\xspace}
\newcommand{\xc}{\ensuremath{_{XC}}\xspace}
And I would now like to define
\newcommand{\plxx}{\ensuremath{\pl\xx}\xspace}
\newcommand{\plxc}{\ensuremath{\pl\xc}\xspace}
...
and so on. (I actually more than four...)
I tried to adapt this solution
\documentclass{minimal}
\usepackage{pgffor}
\newcommand{\pl}{\ensuremath{p^L}}
\newcommand{\xx}{_{XX}}
\newcommand{\xc}{_{XC}}
\foreach \p in {xx,xc}{
\expandafter\xdef\csname pl\p\endcsname{%
\noexpand\ensuremath{\pl\p}%
}%
}%
\begin{document}
$\plxc$
\end{document}
But it gives me the error
! Missing $ inserted.
<inserted text>
$
l.15 $\plxc
$
! Missing $ inserted.
<inserted text>
$
l.16 \end{document}
Really sorry to bother you with this, since I already asked a similar question but I'm not able to adapt the solution to the other one...

\ensuremathin the foreach loop so remove the one in the definition of\plor take it out in the loop. Also please read this: http://tex.stackexchange.com/questions/34830/when-not-to-use-ensuremath-for-math-macro – percusse Oct 25 '12 at 10:55\ensuremathand\xspacethat only complicates things. What's so difficult in writing$\pl$instead of\pl? If it's math it should be written in math. – egreg Oct 25 '12 at 11:12