# 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

With a newer luaotfload (which means that is works only with lualatex) it is possible to exchange single glyph in a font. Here an example.

Attention! The combofont package is experimental. Also the example shows only how to setup the regular font, one would need similar definitions for bold, italic etc. So quite some work is involved. But it has the advantage over the definition of a rule that the underscore copy & paste correctly.

\documentclass{article}

\usepackage[]{fontspec}
\usepackage{combofont}
\setupcombofont{lmodernshortscoreA-regular}
{
{name:lmroman10-regular:\combodefaultfeat} at #1pt,
{name:lmmono10-regular} at #1pt
}
{{},0x5F}

\DeclareFontFamily{TU}{lmodernshortscoreA-regular}{}
\DeclareFontShape {TU}{lmodernshortscoreA-regular}{m}{n} {<->combo*lmodernshortscoreA-regular}{}

\setupcombofont{lmodernshortscoreB-regular}
{
{name:lmroman10-regular:\combodefaultfeat} at #1pt,
{name:lmroman10-regular} at \fpeval{#1*0.6}pt
}
{{},0x5F}

\DeclareFontFamily{TU}{lmodernshortscoreB-regular}{}
\DeclareFontShape {TU}{lmodernshortscoreB-regular}{m}{n} {<->combo*lmodernshortscoreB-regular}{}

\begin{document}
Normalunderscore:

abc\_abc

\fontfamily{lmodernshortscoreA-regular}\selectfont
Patched font with underscore from the ttfont:

abc\_abc

\fontfamily{lmodernshortscoreB-regular}\selectfont
Patched font with underscore scaled down:

abc\_abc

\end{document}


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}