7

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.

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

3 Answers 3

9

\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}

enter image description here

Alternatively, you can do

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

so defining a proper replacement for hyperref's needs.

1
  • 1
    Update: defining \textsubscript is not needed with newer versions of LaTeX.
    – egreg
    Commented Jun 27, 2018 at 9:05
4

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

1
  • Adding the second argument to \texorpdfstring made it work. Thanks!
    – GrowMyHair
    Commented Sep 6, 2013 at 15:01
0

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}
2
  • 1
    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
    Commented Jun 27, 2018 at 9:14
  • It works perfectly with hyperref. Commented Jan 19, 2019 at 18:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .