# [Tex/LaTex] How to expand TeX’s “main memory size”? (pgfplots memory overload)

graphicsmemorypgfplotstex-core

I'm compiling one pretty simple LaTeX file with few dozen of pgfplots. Now, I can compile part by part, but am unable to compile it all at once. I really need good precision for all those plots (noise plots) and would like to avoid decimating graphs more. When I run compile, I get this error message:

[...] TeX capacity exceeded, sorry [main memory size=3000000].


Is there a way to avoid this message somehow? How am I supposed to use pgfplots if I can't plot couple dozen of figures with it without having TeX overloaded? How to expand TeX memory size, please?

The pgfplots package can be particularly heavy on TeX's memory, especially if you are creating plots with lots of data points. Indeed, there is a section in the pgfplots manual about expanding TeX's memory. However, that does not mean that expanding TeX's memory is the best solution. Instead, I would recommend using the 'externalization' approach (section 7.1 of the pgfplots manual).

The idea of externalization is to compile each plot as a separate TeX job. This leads to a graphic which can be used in the main job. Thus each plot has its own memory requirement, separate from all of the other plots. This usually avoids needing to make TeX's memory bigger. At the same time, the resulting files can be kept between TeX runs, which will speed up compilation for the second and subsequent runs. The latest version of the externalization system needs you to do two things. First, you put

\usepgfplotslibrary{external}
\tikzexternalize


in your preamble, to turn the system on. Secondly, you will need to enable 'shell escape'. This is done at the command line by adding the -shell-escape switch:

pdflatex -shell-escape <filename>


The same can be done in LaTeX editors: there is usually a place in the settings for this type of thing. I'll just add that shell escape does has some security implications: use only with documents that you trust!