# Using a previously defined command in a loop

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...

-
You already have \ensuremath in the foreach loop so remove the one in the definition of \pl or take it out in the loop. Also please read this: tex.stackexchange.com/questions/34830/… – percusse Oct 25 '12 at 10:55
Thank you but I also need to use \pl as a standalone. What I have done is created a new \plbare and define the standalone \pl using that. – Peutch Oct 25 '12 at 11:04
I still don't understand why doing all this stuff with \ensuremath and \xspace that 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
You're right, bad habit on my part. – Peutch Oct 25 '12 at 11:36

I'd simply say

\newcommand{\pl}{p^L}
\newcommand{\pr}{p^R}
\newcommand{\xx}{_{XX}}
\newcommand{\xc}{_{XC}}


so that

$\pl\xx$


would give the result you need. Clearer and simpler.

I really can't figure out why you would need \xx in text mode (so with \ensuremath).

There are many problems with abusing \ensuremath and \xspace; one you have found yourself: $\pl\xx$, which is the most natural syntax, doesn't work and you're forced to increase the number of commands to remember.

When you say "I need \pl also in text" you're wrong: you need to use a math formula in text, which is quite common and obtained by

$$\pl$$


(old timers like me are used to $\pl$) that will also appear marked up if you have a good editor.

If you really insist in using a wrong way of doing things, here's how.

\documentclass{article}
\usepackage{xspace}
\usepackage{pgffor}

\newcommand{\pl}{\ensuremath{p^L}\xspace}
\newcommand{\pr}{\ensuremath{p^R}\xspace}
\newcommand{\xx}{\ensuremath{_{XX}}\xspace}
\newcommand{\xc}{\ensuremath{_{XC}}\xspace}

\begingroup
\def\ensuremath#1{#1}
\def\xspace{}

\foreach \p in {xx,xc}{
\expandafter\xdef\csname pl\p\endcsname{%
\noexpand\ensuremath{\pl\csname\p\endcsname}\noexpand\xspace
}%
}
\endgroup

\begin{document}
Here \plxc is used in text

\texttt{\meaning\plxc}
\end{document}


-

You should suppress the expansion of the \pl in the loop too

Edit after the comment: And if you want to insert the command \xx (and not the string xx) you must use \csname\p\endsname or \noexpand\csname\p\noexpand\endcsname:

\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{\noexpand\pl\noexpand\csname\p\noexpand\endcsname}%
}%
}%

\begin{document}
$\plxc$
\end{document}

-
Thank you but I would like \plxc to give me in the final: $p^L_{XC}$ whereas in your code it gives me $p^L xc$. – Peutch Oct 25 '12 at 11:11
@Peutch: I have edited the answer. – Ulrike Fischer Oct 25 '12 at 11:23
@UlrikeFischer: \endcsname isn't expandable by \edef. – Ahmed Musa Oct 25 '12 at 15:16