TeX capacity exceeded, sorry

Peter Pablo edited this page Mar 30, 2015 · 7 revisions

Sometimes when converting a MATLAB(R) figure into TikZ, LaTeX will bail out will bail out with an error message of the type

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

The error is caused by a memory limitation built into the classical TeX implementations and occurs when you try to compile documents with too many data points in it.

There are at multiple possible work-arounds for this problem.

1 Use LuaTeX

The LuaTeX project was created several years ago in order to deal with the shortcomings of the classical TeX implementations. One of LuaTeX's benefits is that it removes the strict memory limitation. If you are using a reasonably modern TeX distribution, it is probably already included in your environment. Try to run the lualatex command in your system prompt:

lualatex main.tex

and see if it works for you.

2 Reduce the number of points in your plot

You could either manually reduce the number of points you are plotting. But we also provide the cleanfigure that tries to do this automatically for you. Generally, this is good idea even when using LuaLaTeX, as the compile time of your figures depends strongly on the number of points you want to plot.

Most of the time, you do not need as many data points as MATLAB(R) includes in its figure. To make the figure a little sparser (without losing too much information), matlab2tikz provides the cleanfigure() script. In particular, the subfunction coarsenLine() might help you out.

3 Adjust output format

Run matlab2tikz with the additional parameters floatFormat and for example %.3f to output the numbers with three decimals. The default of m2t is %.15g.

4 Extend your LaTeX compiler limits

If you cannot (or are reluctant to) change your compiler, you can increase the memory available to LaTeX. This will not remove the problem completely: for a lot bigger figures, you will still encounter problems.

The procedure differs for the different LaTeX distributions and is explained in more detail in Section 6.1 of the pgfplots manual. This manual also contains ways to edit those limits e.g. on a per-user basis (might be needed if you don't have write access to these files, as might be the case in a corporate environment).

MikTeX (Windows):

Execute the following command in your prompt (Windows > Run > "cmd")

initexmf --edit-config-file=pdflatex

This opens an editor where you can put the new values. E.g.


Afterwards call initexmf --dump=pdflatex to update the LaTeX format files.

For regular LaTeX instead of PDFLaTeX, replace pdflatex with latex above.

TeXLive & MacTeX (Linux, OS X, ...)

Edit your texmf.cnf file. This file can be found at e.g. /usr/local/texlive/2014/texmf.cnf (for TeXLive/MacTeX 2014). Change its contents to contain something like:


Afterwards, call texhash to update the LaTeX format files.

5 Export your plot as PNG

If you are unable to use any other way, you can always export your figure as a different format. Typically, if you export a lot of points to a vectorized format such as TikZ, PDF or EPS your file size grows quite large. So it might be worthwhile to export your figure to a PNG figure.

If you still want the typical style of a pgfplots figure, you can use the approach suggested in the pgfplots manual in Section 4.3.8 in the paragraph entitled "Support for External Three-Dimensional Graphics". As its name suggests, this is especially interesting for 3D plots, for which TikZ only has basic support anyway.


You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.