3

Is there a way to automatically count the number of words on each beamer slide, and to display it on the slide? I would like this feature to be available only when I am creating draft versions of the slides. Ideally, I should be able to toggle this feature off when I create the final version of the slides for distribution.

MWE

\documentclass{beamer}

\begin{document}

\begin{frame}{First Slide}
This is my first slide.

% I want to generate the word count below automatically
nwords = 7
\end{frame}

\begin{frame}{Second Slide}
This is my second slide.
It has more words than the first slide.

% I want to generate the word count below automatically
nwords = 15
\end{frame}

\end{document}

The reason why I ask this question is because some people recommend that a slide should not have too many words. For example, an article by Presentation Magazine titled How many words should I have on each slide? gives the rule of thumb of "less than 40 words per slide." It would thus be useful to know how many words I have in each slide.

14
  • 3
    If you have to count the number of words automatically in a slide, you must think seriously about your concept of a good presentation. I am hesitant to said that it could be possible, but thinks that at worst your wishes could be fulfilled.
    – Fran
    Jun 24, 2017 at 10:03
  • 5
    Member with more than 2k rep and no MWE? -1. Also this sounds very much like a do-it-for-me question. Would downvote twice if I could. Jun 24, 2017 at 10:04
  • 1
    @percusse it would at least reduce the amount of work everyone had to put into this. If there was a MWE that would at least create one slide one could (if one was interested in providing a solution) test some stuff without the need to create a MWE completely on his own.
    – Skillmon
    Jun 24, 2017 at 10:17
  • 1
    Here is a MWE, \documentclass{beamer}\usepackage{lipsum}\begin{document} \frame{\lipsum[10]}\end{document}. Again how is this remotely related to the question? This is pure OCD. Not every question needs a fake MWE for justification
    – percusse
    Jun 24, 2017 at 10:24
  • 3
    I have to say I'm not convinced by the duplicate. This question is per slide (or frame). All the answers to the linked question are per document. The ones I'm familiar with wouldn't be trivial to adapt (I'd use a script to split on \begin{frame} and pass each frame through texcount). Voting to reopen.
    – Chris H
    Jun 24, 2017 at 13:14

1 Answer 1

4

Good thing, that the beamer frame collects its body in a token list like the amsmath environments. Thus we can simply examine the token list where the contents are stored. However, we have to hack the beamer frame for that. All the arguments to the frame, in this case {First Slide}, are counted as well. Brace groups count as one token and other oddities apply, see the documentation of \seq_set_split.

\documentclass{beamer}
\usepackage{xparse}

\ExplSyntaxOn

\cs_generate_variant:Nn \seq_set_split:Nnn { Nno }

\NewDocumentCommand \setwordcount { m m }
{
  % split the frame contents at spaces
  \seq_set_split:Nno \l_tmpa_seq { ~ } { \the#2 }
  % remove all mentions of the counter variable
  \seq_remove_all:Nn \l_tmpa_seq { #1 }
  % set counter
  \tl_gset:Nx #1 { \seq_count:N \l_tmpa_seq }
}

\ExplSyntaxOff

\makeatletter

\def\beamer@frameenv{%
  \def\beamer@process@envbody{\endgroup%
    \setwordcount\insertwordcount\beamer@envbody% Count number of space delimited tokens in the input
    \expandafter\expandafter\expandafter\beamer@framecommand\expandafter\beamer@frameoptions\expandafter{\the\beamer@envbody}}%
  \global\beamer@envbody{}\def\beamer@begin@stack{b}%
  \begingroup
  \let\frame\beamer@collect@@body
  \def\beamer@process@envbody{\frame}%
  \beamer@process@envbody%
}

\makeatother

\begin{document}

\begin{frame}{First Slide}
  This is my first slide.
  \insertwordcount
\end{frame}

\end{document}

enter image description here


It may be better to count words with l3regex ... but maybe not. Who knows? This is recursive in subgroups and doesn't count control sequences but math material is still tricky.

This produces similar output but with count 7.

\documentclass{beamer}
\usepackage{xparse}

\ExplSyntaxOn

\cs_generate_variant:Nn \regex_count:nnN { noN }

\NewDocumentCommand \setwordcount { m m }
{
  % regex match words
  \regex_count:noN { \w+ } { \the#2 } \l_tmpa_int
  % set counter
  \tl_gset:Nx #1 { \int_to_arabic:n \l_tmpa_int }
}

\ExplSyntaxOff

\makeatletter

\def\beamer@frameenv{%
  \def\beamer@process@envbody{\endgroup%
    \setwordcount\insertwordcount\beamer@envbody% Count number of space delimited tokens in the input
    \expandafter\expandafter\expandafter\beamer@framecommand\expandafter\beamer@frameoptions\expandafter{\the\beamer@envbody}}%
  \global\beamer@envbody{}\def\beamer@begin@stack{b}%
  \begingroup
  \let\frame\beamer@collect@@body
  \def\beamer@process@envbody{\frame}%
  \beamer@process@envbody%
}

\makeatother

\begin{document}

\begin{frame}{First Slide}
  This is my first slide.
  \insertwordcount
\end{frame}

\end{document}
2
  • 1
    With your proposition, a call to \lipsum[4] adds only 1 word! Jun 25, 2017 at 5:29
  • 2
    @PaulGaborit Yes, because the frame body is not expanded. If I expanded the body, hell would break loose on fragile commands. Jun 25, 2017 at 5:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .