Skip to main content

Recovering TeX’s short underscore in LaTeX

In LaTeX\LaTeX, writing the\_long\_underscore\fbox{\verb|the\_long\_underscore|} yields the_long_underscore\fbox{\text{the\_long\_underscore}}, which looks too long when appearing in source code listings. (Here in KaTeX\KaTeX it looks not that long.) One solution is using the underscore from a monospaced font instead.

Of course, Knuth’s plain TeX\TeX does not have that issue. The underscore is properly sized. Why? Because TeX\TeX does not load a real glyph for \_\fbox{\verb|\_|}. It is defined as \leavevmode \kern .06em \vbox {\hrule width.3em}\fbox{\verb|\leavevmode \kern .06em \vbox {\hrule width.3em}|}, i.e., a horizontal line!

We can recover that good old definition in LaTeX\LaTeX:

\renewcommand{\textunderscore}  {\leavevmode \kern .06em \vbox {\hrule width.3em}}\begin{array}{l} \verb|\renewcommand{\textunderscore}|\\ \verb| |\verb| {\leavevmode \kern .06em \vbox {\hrule width.3em}}| \end{array}

It seems that \_\fbox{\verb|\_|} is implemented using \textunderscore\fbox{\verb|\textunderscore|}, so we do not need to redefine the former. It also seems that LaTeX\LaTeX does include the good old definition, but somehow overrides it.