Defining a new unit in siunitx that has a subscript

I would like to define a new unit, analogous to \electronmass, in the siunitx package but this time for the proton mass. So I just copy/pasted the definition of \electronmass from the style file and modified it. See MWE below.

\documentclass{article}

\usepackage{siunitx}

\DeclareSIUnit[]\protonmass
{ \text { \ensuremath { m _ { \textup { p } } } } }

\begin{document}

electron mass = \si{\electronmass}

proton mass = \si{\protonmass}

\end{document}


If I look at the output, there is a small space between the 'm' and the 'p'. .

How should I define the \protonmass unit so that the space is gone?

Remove the spaces from the definition. The reason why the space doesn't occur for \electronmass is I'd think that siunitx is an expl3 package, and in the environment defined by expl3 spaces in the input are all ignored. Your new definition is not written in that environment.

\documentclass{article}

\usepackage{siunitx}

\DeclareSIUnit[]\protonmass
{\text{\ensuremath{m_{\textup{p}}}}}

\begin{document}

electron mass = \si{\electronmass}

proton mass = \si{\protonmass}

\end{document}


Here's the output.

• I think Joseph should have the definitions of units in a different file not evaluated under \ExplSyntaxOn. – egreg Sep 18 '15 at 15:38

After the definition

\DeclareSIUnit[]\protonmass
{ \text { \ensuremath { m _ { \textup { p } } } } }


the replacement text for \protonmass becomes

•\text{•\ensuremath{•m•_{•\textup{•p•}•}•}•}•


where • denotes a space token. The third, fourth, fifth, eighth and ninth space tokens get ignored, because they happen to be evaluated in math mode. The remaining ones aren't (but the outer ones are removed by siunitx) because they're in text mode. So you still get the second, sixth, seventh and tenth space tokens: a total of four. Indeed

\documentclass{article}

\usepackage{siunitx}

\DeclareSIUnit[]\protonmass
{ \text { \ensuremath { m _ { \textup { p } } } } }

\begin{document}

X\si{\electronmass}X

X\si{\protonmass}X

\end{document}


produces

where the four spaces are clearly visible.

Correct definition

\documentclass{article}

\usepackage{siunitx}

\DeclareSIUnit{\protonmass}{%
\text{$m_{\textup{p}}$}%
}

\begin{document}

X\si{\electronmass}X

X\si{\protonmass}X

\end{document}


Notes

1. \ensuremath is not needed, because you know to be in text mode
2. Joseph Wright, the package author, should provide the unit definitions in a different file not under \ExplSyntaxOn, in order to provide a model for further definitions.