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

  • I suggest you to put everything inside a \begin{verbatim}\end{verbatim} and then you don't need to insert a lot o slashes. – Sigur Mar 26 at 22:54
10

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}
| improve this answer | |
  • Thank you. It works as expected. – Michael Mar 27 at 2:15
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).

| improve this answer | |
  • 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 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 at 23:09
  • @barbara beeton I've added an example that turns ligatures off. – frougon Mar 26 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 at 23:17
  • 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 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}
| improve this answer | |
  • 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 at 23:00
  • verbatim is not an option - it is not working inside mulicolumn – Michael Mar 27 at 2:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.