0

An easy one: I would like to define a shortcut so that I can write \eta without having to enclose it in $\eta$.

However, the naive approach will not work:

\documentclass{article}

\newcommand{\pt}{\ensuremath{p_T}}
% \renewcommand{\eta}{\ensuremath{\eta}} % hangs if I do it like this

\begin{document}

I have $\pt = x$, $\eta = y$. 

Just like \pt, I can also refer to %\eta.
--- no I can't.

\end{document}

It seems this approach results in an endless loop. What is the best way around this?

Andrew Swann
  • 95,762
  • What's the purpose? I find no advantage whatsoever. – egreg Feb 27 '15 at 10:57
  • Of course it does, since it tries to use eta –  Feb 27 '15 at 10:59
  • I'm saving two key strokes. – fuenfundachtzig Feb 27 '15 at 11:02
  • 4
    @fuenfundachtzig And making your input more confusing. – egreg Feb 27 '15 at 11:03
  • The macroname \pt is not really recommendable, as it is misleading and could be confused with the TeX dimension pt –  Feb 27 '15 at 11:03
  • Thanks for the hint about \pt. However, it did a good job for me so far... (I never use / need the TeX dimension but I need to refer to the transverse momentum a lot.) – fuenfundachtzig Feb 27 '15 at 11:04
  • 2
    I agree with egreg, $\pt$ gives a much clearer markup. Most editors will also highlight it. Also you will have to remember blah blah \pt{} blah blah so that it does not eat a space. So those saved $ will come and bite you in the end. – daleif Feb 27 '15 at 12:00
  • Nope: I always use xspace for things like \pt (I didn't include it in the minimal example, however). So it would be blah \pt blah. I appreciate all the well-intentioned comments, but you may trust me: It does work very well for me :) (\pt is also highlighted by the way.) – fuenfundachtzig Feb 27 '15 at 12:26
  • @fuenfundachtzig, then look back though the archives to find what many people think about xparse, there are situations where \xparse does it wrong.... (don't remember them from the top of may head) – daleif Feb 27 '15 at 14:31
  • @daleif Are you talking about xparse or xspace? – sebhofer Feb 27 '15 at 15:21
  • @sebhofer, typo, \xspace, my bad – daleif Feb 27 '15 at 15:28
  • @daleif The discussion you are probably thinking of is here btw. – sebhofer Feb 27 '15 at 15:49
  • See When not to use \ensuremath for math macro for some hints about the problem. If you try \pt+\eta you'll get spacing completely wrong, unless you add $ around the expression. And \pt-\eta in text mode would be really outrageous. – egreg Feb 27 '15 at 16:20
  • Yes, there are corner cases in which \xspace gets the spacing wrong. Still, I found it to be a tremendous help, and those cases in which it fails cannot be that numerous because I cannot come up with one from the top of my head. – fuenfundachtzig Feb 27 '15 at 17:54

2 Answers2

4

Regardless whether it's useful or not, as a shortcut without $...$ it's possible to redefine \eta, but it's necessary to store the meaning of \eta before (making a copy) using \let.

My statements are of general nature, I do not really recommend to use \eta in this way.

\documentclass{article}


\newcommand{\pt}{\ensuremath{p_T}}
\let\etaorig\eta%
\renewcommand{\eta}{\ensuremath{\etaorig}} % hangs if I do it like this

\begin{document}

I have $\pt = x$, $\eta = y$. 

Just like \pt, I can also refer to \eta.
--- Yes I can!

\end{document}

enter image description here

1

Here is another approach which doesn't allocate the new name \etaorig:

\edef\eta{\noexpand\ensuremath{\mathchar\the\eta\space}}
wipet
  • 74,238
  • What if \eta happens to not be defined with \mathchardef? I know this is usually the case, but why assuming it? – egreg Feb 27 '15 at 16:13
  • @egreg I assumed this because I suppose that user will need to redefine \alpha, \beta etc. These \mathcharedef charcters can be redefined without allocating \alphaorig, \betaorig, ..., \omegaorig. – wipet Feb 27 '15 at 16:52
  • You misunderstood my comment. What if \eta is *not* defined with \mathchardef, but some package, for its own purpose, has changed the usual definition into something else? Say for getting a different symbol in some non math alphabet. Unlikely, but why making assumptions when it's possible to avoid them? – egreg Feb 27 '15 at 16:56