Every now and then I have the same problem when compiling certain files. Of course, I have forgotten every time how I solved it in the past. Then, I google and I am getting to this page. Next, I remember that I landed on this page for several times, but, unfortunately, the answers did'nt help me in the past for my kind of problem. To break that Groundhog Day circle, I write down this answer which may help myself (for the next circle) and hopefully other people. The bad news is that it works only (?) for MikTeX users.
pdflatex --enable-write18 --extra-mem-bot=10000000 --synctex=1 <filename>
You may replace 10000000 by another ridiculous high number.
As was commented by @coatless, you may have to add --extra-mem-top=10000000 for other document types than my test example. The MiKTeX manual contains the following explanations:
--extra-mem-bot=n: Set the extra size (in memory words) for large data structures like boxes, glue, breakpoints, et al. Relevant only after the memory dump file has been read.
--extra-mem-top=n: Set the extra size (in memory words) for chars, tokens, et al. Relevant only after the memory dump file has been read.
For illustration some example log entries from a test document:
Failing example without the extra memory:
21229 strings out of 493921 360228 string characters out of 3152094 3000000 words of memory out of 3000000 24030 multiletter control sequences out of 15000+200000 49729 words of font info for 45 fonts, out of 3000000 for 9000 841 hyphenation exceptions out of 8191
Working example with --extra-mem-bot=10000000:
23652 strings out of 493921 401787 string characters out of 3152094 10571134 words of memory out of 13000000 24308 multiletter control sequences out of 15000+200000 49729 words of font info for 45 fonts, out of 3000000 for 9000 841 hyphenation exceptions out of 8191
Working example with --extra-mem-bot=100000000:
23652 strings out of 493921 401787 string characters out of 3152094 100571134 words of memory out of 103000000 24308 multiletter control sequences out of 15000+200000 49729 words of font info for 45 fonts, out of 3000000 for 9000 841 hyphenation exceptions out of 8191
I guess that my example in truth needed 571134 words of memory (and the the rest is eaten up for fun).
matlab2tikzsmaller is to use the commandcleanfigurein Matlab, before usingmatlab2tikz. I got this advice from a colleague recently.