2

This is a follow-up question to my question here which is solved thanks to the \tabto package. But I would like an explanation of why my approach of verbatim is incorrect. For convenience's sake, I have pasted the code and output here.

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[table]{xcolor}
\usepackage{array}

\begin{document}
\begin{enumerate}
    \item Tomas\verb+           +Thomas
    \item Maine\verb+           +Main
    \item Patty\verb+           +Patti
    \item Roberts\verb+         +Robertson
    \item Springfield\verb+     +Springvale
    \item Nixon\verb+           +Dixon

\end{enumerate}
\end{document}

And this is the result:

latex render

As you can see, the second line is not perfectly aligned even though it should if \verb faithfully printed however many space in the source code.

I would like an explanation of why that was not the case and where my understanding of \verb went wrong.

Mib
  • 103
jxhyc
  • 1,121
  • 1
    The font is not monospaced (i.e., different letters and the space occupy different widths). Try it again with a monospaced font. That is to say, add \ttfamily after \begin{document} – Steven B. Segletes May 01 '20 at 10:13
  • In the font your are using different characters have different widths, so it is unlikely that they should line up. If you throw in a \ttfamily before the enumerate you get perfect alignment because all letters have the same width. – moewe May 01 '20 at 10:14
  • 2
    Surely \verb is not supposed to be used for spacing. – egreg May 01 '20 at 10:23
  • Already without \verb in the mix, you'll find that the first three words have the same number of letters and that in the code as shown on this site they occupy the same width (because code on this site uses a monospaced font), but in the output the three words don't have the same width, because your output uses a proportional font. Just to be clear: the primary use of \verb is not to stop LaTeX from collapsing space tokens, it's intended to let you output exactly what you type without having to escape special characters. It's mainly used for writing about (LaTeX) code. – moewe May 01 '20 at 10:24

2 Answers2

4

Your idea has several flaws. First: \verb uses a different font than the normal text font and the (normal) width of a space differs:

\sbox0{ }\the\wd0

\sbox0{\texttt{ }}\the\wd0

would produce

enter image description here

Second: the width of “Tomas” differs from the width of “Maine”. Same experiment:

\sbox0{Tomas}\the\wd0

\sbox0{Maine}\the\wd0

enter image description here

Adding the same amount of spaces between Tomas and Thomas as between Maine and Main would not align the latter words.

If you want perfect alignment by spaces you need to use a monospaced font (the same used by \verb)

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[table]{xcolor}
\usepackage{array}

\begin{document}
\begin{enumerate}\ttfamily
    \item Tomas\verb+           +Thomas
    \item Maine\verb+           +Main
    \item Patty\verb+           +Patti
    \item Roberts\verb+         +Robertson
    \item Springfield\verb+     +Springvale
    \item Nixon\verb+           +Dixon

\end{enumerate}
\end{document}

enter image description here

Of course this is not what you'd like to see. That's why an approach with tabto is effective and necessary, unless you do it manually.

\documentclass{article}

\newcommand{\fl}[2]{\makebox[#1][l]{#2}\ignorespaces}

\begin{document}

\begin{enumerate}
\item \fl{6em}{Tomas}       Thomas
\item \fl{6em}{Maine}       Main
\item \fl{6em}{Patty}       Patti
\item \fl{6em}{Roberts}     Robertson
\item \fl{6em}{Springfield} Springvale
\item \fl{6em}{Nixon}       Dixon
\end{enumerate}

\end{document}

enter image description here

egreg
  • 1,121,712
4

Why doesn't the code produce the desired result?

The font in the output is a so-called proportional font, which means that letters do not all have the same width, instead the only occupy the space they need. An 'i' takes up less width than an 'X' or an 'M' in fonts like that.

You can already see the effect of that in the first three lines: 'Tomas', 'Maine' and 'Patty' all have the same number of letters, yet they do not not line up completely at the end. That's because the letters have different widths, 'Patty' has two smaller 't's and 'Maine' a thin 'i', but the letters in 'Tomas' are all fairly wide.

'Springfield' is a very good example: It contains the small letters 'i' and 'l' and additionally a 'fi' ligature that makes the already fairly slim 'f' and 'i' come even closer together. In the output

MMMMMMMMMMM

would be considerably wider than

Springfield

with the same number of letters.

So you can't align the words perfectly just by counting characters and spaces.

If you look at the code

\begin{enumerate}
    \item Tomas\verb+           +Thomas
    \item Maine\verb+           +Main
    \item Patty\verb+           +Patti
    \item Roberts\verb+         +Robertson
    \item Springfield\verb+     +Springvale
    \item Nixon\verb+           +Dixon
\end{enumerate}

the words line up precisely because the code is shown in a different, monospaced font where all characters have the same width.

If you tell LaTeX to use a monospaced font with \ttfamily, stuff lines up

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

\begin{document}
\begin{enumerate}\ttfamily
    \item Tomas\verb+           +Thomas
    \item Maine\verb+           +Main
    \item Patty\verb+           +Patti
    \item Roberts\verb+         +Robertson
    \item Springfield\verb+     +Springvale
    \item Nixon\verb+           +Dixon
    \item MMMMMMMMMMM
\end{enumerate}
\end{document}

because all characters and spaces (\verb switches the font to the same font as \ttfamily) have the same width.

enter image description here

Why is \verb the wrong tool here?

As the example shows, \verb is the wrong tool here, because it would only align the words with a number of literal spaces instead of aligning the words via a more robust method as discussed in the great answers you got in How to align words on one line to the words on the next without resorting to table.

It's the equivalent of holding the space bar in MS Word until the words roughly line up instead of using TAB or some tabular method.

The main use case for \verb is to typeset text (usually computer code) containing special characters that normally needs escaping in TeX (\, #, _, ^, &, % etc.). See Escape character in LaTeX. It was not intended to be used as a way to align arbitrary text surrounding it.

moewe
  • 175,683