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.

  • 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 '17 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. – Henri Menke Jun 24 '17 at 10:04
  • what would you do with a MWE for this? – percusse Jun 24 '17 at 10:13
  • 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 '17 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 '17 at 10:24
  • 1
    @percusse: If this would be the MWE, the question would indeed be a duplicate, because the document has only one slide and therefore counting the words in the document would be same as counting the words in the slide. – Schweinebacke Jun 24 '17 at 11:13
  • 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 '17 at 13:14
  • @HenriMenke I didn't realize at the time that I should add an MWE, but you are very right that I should. It is now done! – I Like to Code Jun 24 '17 at 13:37
  • @ChrisH This was my first thought too, but AFAIK, e.g., Philipp's answer can be used to count the words of only parts of a document, too. This is the reason, I've voted for the duplicate. – Schweinebacke Jun 24 '17 at 13:41
  • 1
    If the limit is 40 words, you can count it by yourself, this is faster, than TeXing a solution. This comment has 25 words in it. – Michael Fraiman Jun 24 '17 at 13:43
  • @Schweinebacke I still think the difference would merit an answer rather than a duplicate redirect. – Chris H Jun 24 '17 at 18:07
  • @ChrisH It's reopened, so please give an answer. – Schweinebacke Jun 24 '17 at 18:09
  • This is going to be complicated if you use overlays. In that case my suggestion of splitting into frames and passing through texcount wouldn't work. Is that an issue here? – Chris H Jun 25 '17 at 06:17
  • Overall I'm not sure word count is the way to go. Imagine a slide with the title "The 30 longest words in English". In some fields "Some important jargon" would be similarly long. – Chris H Jun 25 '17 at 06:20

1 Answers1

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}
Henri Menke
  • 109,596