Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

numbered letters

share|improve this question
4  
Welcome to TeX.SX! Nice question! –  egreg Jul 28 at 20:35
    
@egreg I agree, a nice one. Maybe soul would be useful here? –  tohecz Jul 28 at 20:36
    
Perhaps number only the characters on a per-line basis? –  Werner Jul 28 at 20:38
2  
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. –  Who is crazy first Jul 28 at 20:48
    
@Please-dont-touch I agree. The sentence I need this for has only 51 letters. –  MartinB Jul 28 at 20:54

2 Answers 2

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
    \tl_map_function:nN { ##1 } \martinb_nl_addnumber:N
    \seq_put_right:NV \l_martinb_nl_textoutput_seq \l_martinb_nl_word_tl
   }
  \seq_use:Nn \l_martinb_nl_textoutput_seq { ~ }
 }

\cs_new_protected:Npn \martinb_nl_addnumber:N #1
 {
  \token_if_letter:NTF #1
   {
    \int_incr:N \l_martinb_nl_number_int
    \tl_put_right:Nx \l_martinb_nl_word_tl 
     {
      \martinb_nl_addnumber_aux:nn { #1 }
       { \int_to_arabic:n { \l_martinb_nl_number_int } }
     }
   }
   {
    \tl_put_right:Nn \l_martinb_nl_word_tl { #1 }
   }
 }
\cs_new_protected:Npn \martinb_nl_addnumber_aux:nn #1 #2
 {
  \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}

enter image description here

share|improve this answer

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%
    \stackunder[8pt]{#1}{\addstackgap{\tiny \thelettercount}}%
  \else\addtocounter{lettercount}{-1}#1\fi}

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

\long\def\bl@t#1#2{\if\bpar#2\par\addtocounter{lettercount}{-1}%
  \else\if.#2\addtocounter{lettercount}{-1}\censordot\else%
  \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}

enter image description here

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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