4

I'm struggling with the problem with underscores in a mono font. It looks like it is shorter than other symbols. For example:

\documentclass{article}

\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}

The result:

The result

As you can see, the last line (with underscores) is shifted left. How I can make the

P.S. No, I can't use a native table - this is a user-generated piece I have to insert as-is. P.S.S. The verbatim is not an option because it is not working inside multicolumn

Michael
  • 197

3 Answers3

9

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}
Werner
  • 603,163
7

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).

frougon
  • 24,283
  • 1
  • 32
  • 55
  • Sorry, not. The hyphens have ligatured into en-dashes, which seem to be the same length as hyphens. verbatim really is the best (most reliable) way to go. – barbara beeton Mar 26 '20 at 23:04
  • That is true and a good point, but I was answering what was happening with the underscores, not the hyphens (as per the question). Oh well... – frougon Mar 26 '20 at 23:09
  • @barbara beeton I've added an example that turns ligatures off. – frougon Mar 26 '20 at 23:16
  • Yes, the question stated that the underscores weren't matching in width, but I'm pretty sure that the OP wasn't aware that something else could happen to throw off the (intended) alignment. – barbara beeton Mar 26 '20 at 23:17
  • Good addition. I like it, even if it's not the approach I'd normally use. – barbara beeton Mar 26 '20 at 23:18
  • I've completed the answer in a way that, I hope, makes it more suitable for the OP. Thanks for your valuable feedback, @barbarabeeton. – frougon Mar 26 '20 at 23:40
  • Thank you. But I can't use verbatim because it is not working in multicolumn – Michael Mar 27 '20 at 02:13
  • Ah, I see. That would have been worth mentioning in the first version of the question... My solution with \@noligs (possibly in the form of \csname @noligs\endcsname) should be usable in these new conditions, though, and has the advantage of working correctly in the modern T1 encoding (the hyphens in Werner's solution are only as you expect them because it uses OT1 encoding). – frougon Mar 27 '20 at 08:31
  • \textunderscore, which _ relies on, is faked using a rule which is presumably also why the stroke width is so different as well – Chris H Mar 27 '20 at 10:34
  • 1
    @ChrisH Sure. According to the Technical details at the end of my answer and the TeXbook p. 221, the rule width (actually, its height) is 0.4pt. No idea how much it is for the font used here, but for a start, this 0.4pt doesn't depend on the font in use. – frougon Mar 27 '20 at 10:45
3

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}
Sigur
  • 37,330
  • You can improve this answer by explaining the difference between \ttfamily and verbatim.. (verbatim is definitely the way to go,; similarly \verb for short strings.` – barbara beeton Mar 26 '20 at 23:00
  • verbatim is not an option - it is not working inside mulicolumn – Michael Mar 27 '20 at 02:12