I use this piece of code in the preamble, obtained from Environment that counts words inside:

  \marginpar{\arabic{words} words}}

I use this in my main file that includes several chapters. If I include up to the end chapter 4 (33599 words) then the counting environment works fine. If I include the next chapter (~6K words) I get the TeX capacity exceeded error. Having tested and excluded all other options it is definitely down to the assignment environment reaching a buffer(?) limit at some point over 33.599K words. I am not sure how to create a MWE for this particular case without sharing my actual work which for obvious reasons I cannot do.

  • 2
    This may be just too hard for TeX, I don't know. Is using LuaLaTeX an option? If yes, you might want to look at this: gist.github.com/phi-gamma/2622252 (didn't test, just googled it).
    – mbork
    Feb 23, 2013 at 9:45
  • 1
    You can try whether you get the error with lots of automatically generated contents like this 100k long lipsum text. Then you post and example with a text PUT 100k OF TEXT HERE instead of the long text.
    – yo'
    Feb 23, 2013 at 10:20
  • 1
    \blindtext also takes an optional repetition argument which could be made large enough to trigger the problem.
    – Christian
    Feb 23, 2013 at 10:42
  • 1
    Hi DGarside. A tip: if you copy and paste directly the link of the answer in your question the title will appear. Feb 23, 2013 at 11:13
  • Can you add the complete error text?
    – egreg
    Feb 23, 2013 at 11:32

1 Answer 1


If I run the document below as posted I get a save stack error:

[30] [31] [32] [33] [34] [35] [36] [37]
! TeX capacity exceeded, sorry [save size=50000].

If I uncomment the two lines marked %%%% then it runs to the end

Output written on xew33.pdf (84 pages).

and reports 51720 words

enter image description here



%%%%     \endgroup
\expandafter\ifx\csname wordcount@xs@searchlist\endcsname\relax
  \marginpar{\arabic{words} words}%
%%%%     \begingroup\expandafter\def\csname @currenvir\endcsname{assignment}%


\def\a{one two three four five six seven eight nine ten. }
\def\b{red yellow green blue. }
\def\c{apple orange pear banana.}


one one two three

one two three four five six


the document environment in LaTeX plays a similar trick to use the environment syntax but avoid making a group around the whole document.

  • This does calculate for size > 50000 but I do not seem able to have one assignment environment follow on immediately after another. The second environment registers as zero. Would the way around that be to create assignment_main and assignment_bibliography environments?
    – DGarside
    Feb 28, 2013 at 17:12
  • code updated to re-use the existing search structure seems to get the right answer on a couple of smaller lists at the end, see posted image Feb 28, 2013 at 18:20
  • It seems that your code edit removed the %%%% markings that you mention in the body of your answer. Was that intentional?
    – LSpice
    Jun 15, 2015 at 22:13

You must log in to answer this question.

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