Underscore is shorter for ttfamily

Instead of \_, you can use \string_:

enter image description here

\documentclass{article}

\begin{document}

\ttfamily
Test \\
.------------.-------------.--------------.\\
\ |\ abcdefghig\ |\ klmnopqrstu\ |\ vwxyzabcdefg\ | \\
.------------.-------------.--------------.\\
\ |\ abc[e]ghig\ |\ klm[o]qrstu\ |\ vwxyza[c]efg\ | \\
.------------.-------------.--------------.\\
\ |\ dfi\string_cke[x]\ |\ dfi\string_cs\string_n[x]\ |\ dfi\string_act\string_n[x]\ | \\
.------------.-------------.--------------.

\end{document}

The most natural way is to use verbatim (see below). But let's explain a bit what happens first.

In OT1 encoding, \textunderscore, which \_ relies on, is faked using a rule (it is not a character of the current font). Switching to T1 encoding by adding \usepackage[T1]{fontenc} makes \_ use an actual character of the font selected by \ttfamily:

\documentclass{article}
\usepackage[T1]{fontenc}

\begin{document}
\ttfamily
Test \\
.------------.-------------.--------------.\\
\ |\ abcdefghig\ |\ klmnopqrstu\ |\ vwxyzabcdefg\ | \\
.------------.-------------.--------------.\\
\ |\ abc[e]ghig\ |\ klm[o]qrstu\ |\ vwxyza[c]efg\ | \\
.------------.-------------.--------------.\\
\ |\ dfi\_cke[x]\ |\ dfi\_cs\_n[x]\ |\ dfi\_act\_n[x]\ | \\
.------------.-------------.--------------.
\end{document}

enter image description here

The underscores now have the same size as other characters. However, as pointed out by Barbara Beeton, consecutive hyphens in non-verbatim mode combine into ligatures (en-dash with --, em-dash with ---). If we turn off ligatures—which is one of the things that verbatim does—then all lines have the expected length:

\documentclass{article}
\usepackage[T1]{fontenc}

\begin{document}

\begingroup
\ttfamily\makeatletter\@noligs\makeatother
Test \\
.------------.-------------.--------------.\\
\ |\ abcdefghig\ |\ klmnopqrstu\ |\ vwxyzabcdefg\ | \\
.------------.-------------.--------------.\\
\ |\ abc[e]ghig\ |\ klm[o]qrstu\ |\ vwxyza[c]efg\ | \\
.------------.-------------.--------------.\\
\ |\ dfi\_cke[x]\ |\ dfi\_cs\_n[x]\ |\ dfi\_act\_n[x]\ | \\
.------------.-------------.--------------.
\endgroup

\end{document}

enter image description here

Practically, using verbatim in such situations is more natural, though. The above examples were just to help understand what happens behind the scenes. Thus, what I would do here is this:

\documentclass{article}
\usepackage[T1]{fontenc}

\begin{document}

\begin{verbatim}
Test
.-------------.-------------.--------------.
 | abcdefghig | klmnopqrstu | vwxyzabcdefg |
.-------------.-------------.--------------.
 | abc[e]ghig | klm[o]qrstu | vwxyza[c]efg |
.-------------.-------------.--------------.
 | dfi_cke[x] | dfi_cs_n[x] | dfi_act_n[x] |
.-------------.-------------.--------------.
\end{verbatim}

\end{document}

With verbatim

Note that since “Test” is inside the verbatim environment, it doesn't get paragraph indentation in this case.

Technical details

For those interested, the implementation of \textunderscore in OT1 encoding, obtained with \tracingmacros=1\tracingonline=1, does:

\leavevmode \kern .06em\vbox{\hrule width .3em}

whereas in T1 encoding, in the end, it is a simple \char"5F (thus, a command that adds to the current list a character box containing the character with hexadecimal code 5F in the current font).


I'd use verbatim:

enter image description here

\documentclass{article}
\begin{document}
\begin{verbatim}
   Test
.------------.-------------.--------------.
| abcdefghig | klmnopqrstu | vwxyzabcdefg | 
.------------.-------------.--------------.
| abc[e]ghig | klm[o]qrstu | vwxyza[c]efg | 
.------------.-------------.--------------.
| dfi_cke[x] | dfi_cs_n[x] | dfi_act_n[x] | 
.------------.-------------.--------------.
\end{verbatim}
\end{document}

Tags:

Typewriter