# How to number the letters of a sentence?

I need to number the letters of a sentence consecutively. Every letter should have the corresponding small number centered below it (see image below). Does anybody know how to do this? I tried \underset from amsmath but it didn't work.

-
Welcome to TeX.SX! Nice question! – egreg Jul 28 '14 at 20:35
@egreg I agree, a nice one. Maybe soul would be useful here? – yo' Jul 28 '14 at 20:36
Perhaps number only the characters on a per-line basis? – Werner Jul 28 '14 at 20:38
How many digits does the number for the last letter in TeXBook take? Using more than 2 (perhaps 3) digits per letter will make the inter-letter space too big. – kiss my armpit Jul 28 '14 at 20:48
@Please-dont-touch I agree. The sentence I need this for has only 51 letters. – MartinB Jul 28 '14 at 20:54

This allows only plain Latin letters (no accents). Something more complicated would be needed to accommodate accented letters.

\documentclass{article}
\usepackage{xparse}
\usepackage{tgbonum} % wide chars

\ExplSyntaxOn
\NewDocumentCommand{\numberletters}{m}
{
\group_begin:
\huge\raggedright
\martinb_nl_numberletters:n { #1 }
\par
\group_end:
}

\int_new:N \l_martinb_nl_number_int
\seq_new:N \l_martinb_nl_textinput_seq
\seq_new:N \l_martinb_nl_textoutput_seq
\tl_new:N \l_martinb_nl_word_tl

\cs_new_protected:Npn \martinb_nl_numberletters:n #1
{
\int_zero:N \l_martinb_nl_number_int
\seq_clear:N \l_martinb_nl_textoutput_seq
\seq_set_split:Nnn \l_martinb_nl_textinput_seq { ~ } { #1 }
\seq_map_inline:Nn \l_martinb_nl_textinput_seq
{
\tl_clear:N \l_martinb_nl_word_tl
\seq_put_right:NV \l_martinb_nl_textoutput_seq \l_martinb_nl_word_tl
}
\seq_use:Nn \l_martinb_nl_textoutput_seq { ~ }
}

{
\token_if_letter:NTF #1
{
\int_incr:N \l_martinb_nl_number_int
\tl_put_right:Nx \l_martinb_nl_word_tl
{
{ \int_to_arabic:n { \l_martinb_nl_number_int } }
}
}
{
\tl_put_right:Nn \l_martinb_nl_word_tl { #1 }
}
}
{
\leavevmode
\vbox_top:n
{
\halign
{
##\cr
\strut#1\cr
\noalign{\nointerlineskip\vskip-1pt}
\tiny\hidewidth#2\hidewidth\cr
}
}
}

\cs_generate_variant:Nn \seq_put_right:Nn { NV }

\ExplSyntaxOff

\begin{document}

\numberletters{Hi, this is a test}

\numberletters{This is much more complicated!---It even
has an en-dash and also breaks across lines}

\end{document}


-

EDITED to discern symbols from letters.

This achieves the result by modifying elements of the censor package. The \blackout macro was adapted for the purpose. It will break its argument across lines and paragraphs.

\documentclass{article}
\usepackage{stackengine,censor}
\def\stacktype{L}
\newcounter{lettercount}
\makeatletter
\renewcommand\censorrule[1]{\ifcat #1A%

\renewcommand\@cenword[1]{\censorrule{#1}}

\long\def\blackout#1{\Huge\setcounter{lettercount}{0}%
\def~{-}\censor@Block#1\stringend\let~\sv@tilde\par\medskip\normalsize}
\long\def\censor@Block{\stepcounter{lettercount}%
\IfNextToken\stringend{\@gobble}%
{\IfNextToken\@sptoken{ \bl@t{\censor@Block}}%
{\bl@t{\censor@Block}}}}

\censor{#2}\fi\fi#1}
\makeatother
\begin{document}
Testing:\par
\blackout{Hi,

This is a test.}\par
And now for another test:\par
\blackout{This is a test.  This is a test!  This is a test/
This is a test,  This is a test-  This is a test:  This is a test.}
\end{document}


-