Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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

share|improve this question
1  
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
1  
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
add comment

2 Answers

up vote 3 down vote accepted

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}

enter image description here

share|improve this answer
add comment

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}
share|improve this answer
    
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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.