[Tex/LaTex] Narrow underscore in listings package

listings

I've been using the listings package for a while, but have only recently noticed that the underscore is rendered with less width than the other characters. I'm working on some code snippets now which use a lot of variables containing an underscore, and so I see the effect more than ever. Is there any way to prevent this?

Best Answer

Say

\usepackage[T1]{fontenc}

in your preamble. The default underscore in OT1 encoding is indeed quite small.

If you don't want to switch font encoding, then the following code in the preamble will produce a similar underscore:

\makeatletter
\DeclareTextCommandDefault{\textunderscore}{%
  \leavevmode\kern0.03em\vbox{\hrule\@width.6em \@height-.25ex \@depth0.37ex}\kern0.03em}
\makeatother
Related Question