I am using matlab2tikz to import figures from MATLAB to tex.

However I quickly get the message TeX capacity exceeded, sorry [main memory size = 3000000].

I have tried expand main memory using instructions from http://docs.miktex.org/manual/runtimeparams.html using initexmf --edit-config-file=xelatex but that only opens an empty file!

I have also tried to use externalization functionality of pgfplots using instructions from its manual and also from Export eps figures from TikZ, but I've also failed (probably because I am using xetex and not pdflatex).

What should I do furhter?

EDIT: I am using MikTeX 2.9 on Windows 7 (with XeTeX 0.9998)

  • Check that you haven't any curly brackets that are not closed.
    – yannisl
    Jun 12, 2012 at 12:35
  • 1
    Too many points cause this problem. It is ok to get the empty file, just put in the commands in this empty file, save and close. However the memory limit can stiill bea reached, use lualatex to compile, there are no memory limits
    – Martin H
    Jun 12, 2012 at 12:49
  • 2
    There is a limit to the memory even if you set it to the max. possible. Try to compile with lualatex for testing. If you have a large number of points in e.g. a plot this always happens. Annoying, I know. I now use lualatex and tikz's external library that renders tikz graphics to pdfs for faster compilation in the future.
    – Martin H
    Jun 12, 2012 at 12:59
  • I've got a problem just the same as you mentioned in this post. I tried to download the modified matlab2tikz file but seems that it can't be downloaded. Please can you help me with refreshing the download link ?I'm in urgent. Thanks in advance Catherine
    – cathy
    Sep 4, 2012 at 13:09

3 Answers 3


Since I didn't have any luck with altering the TeX memory capacity, I had to find another way to solve my problem.

It was matlab2tikz that actually did the job. It has an option of downsampling the figures during the conversion to tikz-pgf. It is called minimumPointsDistance.

The final (downsampled) figures where absolutely indistinguishable from the original and MUCH, MUCH more light. Also note, that you don't have to downsample the figures in MATLAB yourself, but just set the minimumPointsDistance which only affects matlab2tikz's output code.

EDIT: This is an expansion of my answer. I apologise for getting out of the TeX limits, but this extension offers a better solution, so I believe it should be posted here. Now on the problem...

matlab2tikz's point reduction algorithm although fast and simple has a

main disadvantage

It can distort certain "stiff" areas of the curve when the actual number of points kept drops very low. This is illustrated in the following picture

enter image description here

The blue curve (1184 points) is the original while the red one (117 points) is the reduced. The distortion is obvious.

To deal with this and at the same time keep the number of points low I wrote a new point reduction algorithm which is much more effective (but can be slower sometimes). The results (red curve is 116 points)

enter image description here

It is clear that the distortion is gone.

But there still is

one more disadvantage

that exists in both algorithms. When one converts a batch of figures to tikz, they usually want uniform quality for all reduced diagrams. But sometimes this is not possible (at least without extra effort) because typically minimumPointsDistance must be set differently for each one of the figures and the results actually depend on the size of the intervals [min_y, max_y] and [min_x, max_x] as well as the number of points in the figure.

So in order to overcome this problem and make minimumPointsDistance able to be set at a specific value uniformly for all figures, producing reduced versions of uniform quality, normalisation is used, and now minimumPointsDistance "internally" refers to the actual size of the printed-on-paper figure.

You can download the modified matlab2tikz.m file from here along with instructions.

NOTE that exhaustive tests were NOT made so be careful!

  • If the file can't be downloaded, please post a comment so I can fix it.
    – niels
    Sep 4, 2012 at 20:16
  • Any chance you could update this for the latest version of matlab2tikz? or better yet, contribute the code to the 'matlab2tikz' author?
    – lyxicon
    Oct 8, 2013 at 20:20
  • @lyxicon As mentioned in the copyright notice; I do not claim any copyright, so the matlab2tikz author is free to use the new point reduction algorithm. I'll update to the new version soon.
    – niels
    Oct 11, 2013 at 8:10
  • It would be nice if you could share point reduction algorithm as a separate file.
    – Ali
    Nov 9, 2013 at 11:11
  • @niels I'm pretty sure your file download link is down.
    – Mew
    May 16, 2021 at 14:00

As nice and flawless the usage of such a plug-in can be, the TeX memory problems are quite annoying in the long run and I had them many times as well processing spectral data.

Another way to deal with it is to use LuaLaTeX as those memory restrictions do not apply here. Together with the externalize library from tikz, the finished pictures get converted into single PDF files. This makes subsequent compiling fast again.


I just found a simple solution today while using their latest version of matlab2tikz. Run the code in LuaLaTeX instead of pdfLaTeX. This solves the issues of

  • ! TeX capacity exceeded, sorry [main memory size=5000000] while running on Mac or
  • TeX capacity exceeded, sorry [main memory size = 3000000] in case of Windows OS.

But I found a new problem on the same code which works on Mac. It did not run on (LuaLaTeX) Windows OS after the update of LaTeX. The error says

  • ! Package pgf Error: Driver file ``pgfsys-luatex.def'' not found.

in this case, one can put the following two lines in the preamble


and run the code in LauLaTeX! Issue is explained here

You must log in to answer this question.

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