How to properly define NOx

I'm writing a document containg NOx a lot. It appears in section titles, regular text and tables, in sans serif, roman, bold and medium. Therefore I defined a command:

\newcommand \nox {NO\textsubscript{x}\xspace}


To use this with hyperref I redefined in to:

\newcommand \nox {\texorpdfstring{NO\textsubscript{x}}\xspace}


The problem I now encounter is that the \xspace command no longer works and all spaces between NOx and the next word disappeared. I can manually include the space by adding a ~-symbol, but that makes spaces appear everywhere, which is also unwanted. How should I redefine \nox to achieve correct spacing and work together with hyperref.

• Does \newcommand{\nox}{\texorpdfstring{NO\textsubscript{x}}{NOx}\xspace} help? For chemical typesetting you might want to have look at mhchem. – moewe Sep 6 '13 at 14:30
• You're missing an argument to \texorpdfstring; add {} or, better {NOx}, in front of \xspace. – egreg Sep 6 '13 at 14:33

\texorpdfstring requires two arguments: the first is what's typeset by TeX, the second what's passed on to hyperref for bookmarks and the like. So you should do

\newcommand\nox{\texorpdfstring{NO\textsubscript{x}}{NOx}\xspace}


Example:

\documentclass{article}
\usepackage{xspace}
\usepackage{hyperref}

\makeatletter
\DeclareRobustCommand{\textsubscript}[1]{\@textsubscript{\selectfont#1}}
\newcommand{\@textsubscript}[1]{%
{\m@th\ensuremath{_{\mbox{\fontsize\sf@size\z@#1}}}}%
}
\makeatother
\newcommand\nox{\texorpdfstring{NO\textsubscript{x}}{NOx}\xspace}

\begin{document}
\section{Here's \nox and something else}

\nox is very dangerous.
\end{document}


Alternatively, you can do

\newcommand\nox{NO\textsubscript{x}\xspace}
\pdfstringdefDisableCommands{%
\def\nox{NOx\xspace}%
}


so defining a proper replacement for hyperref's needs.

• Update: defining \textsubscript is not needed with newer versions of LaTeX. – egreg Jun 27 '18 at 9:05

With fixltx2 an issue with \textsubstring is corrected. So please try the following MWE:

\documentclass{report}

\usepackage{xspace}
\usepackage{fixltx2e}% for \textsubscript
\usepackage{hyperref}

\newcommand{\nox}{\texorpdfstring{NO\textsubscript{x}}{NOx}}
\newcommand{\noxa}{\texorpdfstring{NO\textsubscript{x}}{NOx}\xspace}

\begin{document}

\nox Blafasel \noxa Blafasel

\end{document}


The macro \texorpdfstring{first}{second}needs two arguments. The first is for the document (with subscruiption for example), the second if special things are not allowed (without: NOx).

• Adding the second argument to \texorpdfstring made it work. Thanks! – GrowMyHair Sep 6 '13 at 15:01

Simply use mhchem package for chemical formulas and notations, see more details at this link.

\documentclass{report}
\usepackage[version=3]{mhchem}

\begin{document}

$\ce{NO_x}$

\end{document}

• How to correctly type the symbol is not the main problem. While your answer is correct for the chemical formula itself, it does not address the main issue, i.e. the interaction with hyperref. – campa Jun 27 '18 at 9:14
• It works perfectly with hyperref. – Deepak Ingole Jan 19 '19 at 18:57