# Fontspec causes long underscores

My collaborator added \usepackage[no-math]{fontspec} to a document we're working on. The result is that \_ underscores become v. long: cf. pictures below.

\documentclass{article}
\usepackage[no-math]{fontspec}
\pagestyle{empty}

\begin{document}
\textrm{metric\_space}
\end{document}


With fontspec:

Without fontspec:

The underscores are about twice as long with fontspec, and also much thinner. My question is, how do I get back to the original width? (Ideally I would like to preserve the thinness -- it looks much nicer than the original.)

In case someone asks, "why are you using \_?", the short answer is that I need to typeset identifiers involving an underscore. See http://i.stack.imgur.com/XynbT.png for an example in context. I'd be happy with using something other than \textrm, if it looks decent in the context of that example.

-
The fontspec package is added but an OpenType font isn't even used (\setmainfont or similar)? Also, the fontspec underscore is below the baseline. –  Qrrbrbirlbel Nov 8 '12 at 18:11
@Qrrbrbirlbel: I'm afraid I know nothing about fontspec. Is there a way to choose a 'safe' font that will be available on any system? –  Mohan Nov 8 '12 at 18:19
Counter question: Why did your collaborator added fontspec? What was their intent? Do they pan to include an OpenType font that is not available on your machine? I like libertineotf. –  Qrrbrbirlbel Nov 8 '12 at 18:24
It was something to do with making Unicode characters in the source display properly. (Possibly inside a verbatim?) –  Mohan Nov 8 '12 at 18:25
@Qrrbrbirlbel fontspec currently uses Latin Modern as default for roman, sans serif and typewriter fonts. –  Andrew Swann Nov 8 '12 at 19:23

You can redefine \_ to draw a rule similar to, but shorter than, the glyph used by Latin Modern, which is indeed rather long.

\makeatletter
\let\_\relax
\DeclareRobustCommand{\_}{%
\leavevmode\vbox{%
\hrule\@width.5em
\@height-.26ex
\@depth\dimexpr.26ex+.28pt\relax}}
\makeatother


Just to show the result in context, here is an example, which uses the redefined command and the glyph you got, which is still available as \textunderscore.

\documentclass{article}
\usepackage[no-math]{fontspec}

\makeatletter
\let\_\relax
\DeclareRobustCommand{\_}{%
\leavevmode\vbox{%
\hrule\@width.5em
\@height-.26ex
\@depth\dimexpr.26ex+.28pt\relax}}
\makeatother
\pagestyle{empty}

\begin{document}
\textrm{metric\_space}\textrm{metric\textunderscore space}

\textrm{metric\textunderscore space}
\end{document}


-
For reference the default definition in the latex kernel is \leavevmode \kern.06em\vbox{\hrule\@width.3em} which will reproduce the original size and spacing. –  Andrew Swann Nov 8 '12 at 19:21
egreg: I'm using this very frequently and would like it to be a package. If you didn't want to packageify it, I would happily do it myself (with full attribution of course), if that was alright with you? –  Mohan Mar 1 '13 at 13:51
@Mohan Do it, if you feel it useful. –  egreg Mar 1 '13 at 13:52

You can use \rule command to use instead of \_.

## Code

\documentclass{article}
\usepackage[no-math]{fontspec}
\pagestyle{empty}
\newcommand*{\oldunderscore}{\kern.061em\rule[0ex]{.7ex}{.4pt}}
\newcommand*{\newunderscore}{\rule[-.319ex]{1.74ex}{.26pt}}
\begin{document}
x\_x\oldunderscore x\newunderscore x
\end{document}

-