2

Let us assume the following example:

\documentclass{article}
\usepackage{amsmath}
\newcommand{\pt}{$p_{T}$}
\begin{document}

We analyse all cases of the transverse momentum \pt. First, let us 
assume that $p_{T} \leq 50 \text{GeV}$.

\end{document}

The result is

enter image description here

It is inviting to use the command \pt in the second mathematical expression and write it like that

$\pt \leq 50 \text{GeV}$

In case I do it, I get the error

! Missing $ inserted.
<inserted text> 
                $
l.6 ...omentum \pt. First, let us assume that $\pt
                                                   \leq 50 \text{GeV}$.

Suggestions?

Viesturs
  • 7,895

3 Answers3

8

The problem is that the \pt macro contains $...$. The solution is to either use \ensuremath, so that the $...$ are added when needed,

\documentclass{article}
\usepackage{amsmath}
\newcommand{\pt}{\ensuremath{p_{T}}}
\begin{document}

We analyse all cases of the transverse momentum \pt. First, let us
assume that $p_{T} \leq 50 \text{GeV}$.

\end{document}

or to use:

\documentclass{article}
\usepackage{amsmath}
\newcommand{\pt}{p_{T}}
\begin{document}

We analyse all cases of the transverse momentum $\pt$. First, let us
assume that $p_{T} \leq 50 \text{GeV}$.

\end{document}

Personally, I prefer the latter because explicit is better than implicit and because this will probably work better with the syntax highlighter in your editor. See also When not to use \ensuremath for math macro?.

4

The error is because you are nesting two math environments. Change you command definition to:

\newcommand{\pt}{\ensuremath{p_{T}}}
  • Answer from @Zarko contains an additional important information on \xspace therefore I change the "Accepted answer". – Viesturs Dec 30 '17 at 22:46
3
\documentclass{article}
\usepackage{amsmath}
\usepackage{xspace}
\newcommand{\pt}{\ensuremath{p_T^{}}\xspace}
\begin{document}

We analyse all cases of the transverse momentum \pt. First, let us
assume that $\pt \leq 50 \text{GeV}$.

\end{document}

enter image description here

xspace ensure correct spacing after use of \pt.

Zarko
  • 296,517
  • \xspace, I see, is indeed important. But the use of ^{} I don't understand. – Viesturs Dec 30 '17 at 22:04
  • @Viesturs, compare $p_T$ and $p_T^{}$, and observe difference. i prefer the second result :-) – Zarko Dec 30 '17 at 22:24
  • It seems that T is lower if ^{} is used. – Viesturs Dec 30 '17 at 22:30
  • 1
    @Viesturs, yes. ^{} push subscripts down and with this prepare more space for superscripts (which in this case is not used). similar effect you can obtain with use of package subdepth. – Zarko Dec 30 '17 at 22:43
  • I consider \xspace as important therefore I shift "Accepted answer" to this answer. – Viesturs Dec 30 '17 at 22:45