如何给句子的字母编号?

如何给句子的字母编号?

我需要连续地对句子的字母进行编号。每个字母都应在正下方居中显示相应的小数字(见下图)。有人知道怎么做吗?我试过了,\undersetamsmath没有成功。

数字字母

答案1

这只允许使用普通拉丁字母(无重音符号)。需要更复杂的方法来容纳重音字母。

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

在此处输入图片描述

答案2

已编辑以辨别符号和字母。

这通过修改包的元素来实现结果censor\blackout宏已为此目的进行了调整。它将跨行和跨段落打破其参数。

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

在此处输入图片描述

相关内容