Fontspec causes long underscores

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}

enter image description here


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}

enter image description here

Tags:

Fontspec