# Adding text under words (to count words) and automatically

In order to count easily words in a text, i'd like to display for each word its "number",

Context : I'm teaching my students to copy a text and to evaluate them I count how many words they successfully copied.

It's not necessary that the counting is done automatically, I'm using the same text,

This is what I've tried for now :

\documentclass{article}
\usepackage{amsmath,stackengine}
\stackMath
\newcommand{\wordcount}[2]{\stackunder{\text{#1}}{\scriptstyle \text{#2}}}
$\setstackgap{S}{3pt} \wordcount{Jusqu'}{5} \wordcount{au}{6} \wordcount{{\fontsize{14}{11}\selectfont XII}}{7} \wordcount{siècle}{8}$
\end{document}


Which give this result :

My problem are multiple :

• The code is "heavy" I have a long text, ideally I'd like a simple command...
• It's in math mode
• Spacing is a nightmare (the 6 and 7 are higher than the 5 and no space between words)...

Any help is appreciated (sorry for any mistake, french here),

Thanks !

• Also, please provide a complete example that compiles, beginning with \documentclass. Also, no undefined macros like \ieme. You can edit your existing question. Welcome to the site. Apr 29, 2021 at 23:54
• To get rid of the uneven vertical positioning, \renewcommand\stacktype{L}. Then you can adjust the baselineskip with \setstackgap{L}{<dimen>}. Apr 29, 2021 at 23:57
• Thanks for the comments, edit done ! May 1, 2021 at 7:10

This should hopefully be more painless than what you were using.

\documentclass{article}
\usepackage{amsmath,stackengine}
\usepackage[french]{babel}
\renewcommand\stacktype{L}
\setstackgap{L}{9pt}
\newcounter{wordcnt}
\newcommand{\wordcount}[2][-1]{%
\ifnum#1>-1\relax\setcounter{wordcnt}{#1}\fi%
\wordcountaux#2 \relax}
\def\wordcountaux#1 #2\relax{%
\stepcounter{wordcnt}%
\stackunder{#1}{\scriptsize\thewordcnt}\ %
\ifx\relax#2\relax\else\wordcountaux#2\relax\fi}
\begin{document}
$y = mx + b\quad\wordcount[4]{Jusqu' au \fontsize{14}{11}\selectfont XII\ieme{} siècle}$$E= mc^2\quad\wordcount{Count picks up by default}$
\end{document}