TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

\usepackage{tgbonum} % wide chars

  \martinb_nl_numberletters:n { #1 }

\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

\cs_generate_variant:Nn \seq_put_right:Nn { NV }



\numberletters{Hi, this is a test}

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


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.

\renewcommand\censorrule[1]{\ifcat #1A%
    \stackunder[8pt]{#1}{\addstackgap{\tiny \thelettercount}}%


  {\IfNextToken\@sptoken{ \bl@t{\censor@Block}}%


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

enter image description here

share|improve this answer

Your Answer


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.