I have a plot with a lot of measurement datapoints. I exported this plot from Matlab2tikz.

When I tried to compile, it said that the memory was exceeded. I found this question and it tells me that I can export different tikzpictures to different files. Unfortunately, this didn't help me since I have only 1 tikzpicture causing the problem.

So I actually tried to increase the memory of TeX. I went to /usr/share/texlive/texmf/web2c then in texmf.cnf I changed main_memory = 3000000 to main_memory = 5000000. Then I ran texhash and tried to compile my TeX file again.

My problem is that after I increased the memory limit I got again the same error: ! TeX capacity exceeded, sorry [main memory size=3000000]. However I just changed it to 5000000. I presume that I did something wrong when increasing the memory limit. Did I?


Eventually I just used lualatex. This was due to the fact that I think there is something wrong with my texlive install: it is installed twice on my system for some reason. I think, however, that Heiko Oberdiek's answer is correct and hence marked it as the correct answer.

  • Have you considered to export the image generated with Matlab and create the plot with the command \addplot graphic?
    – andrMollo
    Oct 5, 2012 at 10:05
  • No because I create different plots in Matlab. They are autogenerated by a tool I use. I then export them to tikz and in tikz combine all the plots.
    – WG-
    Oct 5, 2012 at 10:12
  • 3
    You could try compiling the document with lualatex, which doesn't have those memory limitations.
    – Jake
    Oct 5, 2012 at 10:50
  • You may not be lucky after the recent update of LuaLaTeX which gives an error messages as ! Package pgf Error: Driver file ``pgfsys-luatex.def'' not found. In this case, you can add two lines \RequirePackage{luatex85} \def\pgfsysdriver{pgfsys-pdftex.def} in the preamble and the re-run the code in LuaLaTeX. Exaplaination of the issue is found at tug.org/pipermail/tex-live/2016-April/038296.html
    – Khaaba
    Jun 7, 2016 at 15:57

2 Answers 2


From texmf.cnf:

% Memory. Must be less than 8,000,000 total.
% main_memory is relevant only to initex, extra_mem_* only to non-ini.
% Thus, have to redump the .fmt file after changing main_memory; to add
% to existing fmt files, increase extra_mem_*.  (To get an idea of how
% much, try \tracingstats=2 in your TeX source file;
% web2c/tests/memtest.tex might also be interesting.)

Thus you have to regenerate the format files (fmtutil) to see an effect of changing main_memory.

  • I ran fmutil in sudo fmtutil-sys --all in /usr/share/texlive/texmf/web2c but I still get ! TeX capacity exceeded, sorry [main memory size=3000000]. instead of ! TeX capacity exceeded, sorry [main memory size=5000000], what I would expect.
    – WG-
    Oct 5, 2012 at 10:27
  • 2
    @Wouter Find the relevant texmf.cnf by running kpsewhich texmf.cnf as normal user. Edit this one (as root if located under /usr/share/texlive/).
    – AlexG
    Oct 5, 2012 at 10:58
  • @AlexG, just did that. Still does not work :-( I believe for some reason I also have 2 texlive installations... When I ran kpsewhich texmf.cnf I was directed to: /usr/local/texlive/2012/texmf.cnf Also I have 2 texmf.cnf's I just noticed. One is located in: /usr/local/texlive/2012/texmf/web2c and one in /usr/share/texlive/texmf/web2c
    – WG-
    Oct 5, 2012 at 11:06
  • 2
    @Wouter: Dont't touch the config files below .../web2c/.... Edit the one kpsewhich texmf.cnf told you. As root, enter kpsewhich texmf.cnf once more, in order to see which installation will be affected by subsequent fmtutil-sys --all. It is probably the one under /usr/share which was installed via your Linux distribution's package manager. I recommend installing TeXLive-2012 as a normal user into your home directory, e. g. to ~/bin/texlive/2012 and prepend ~/bin/texlive/2012/bin/i386-linux to your PATH environment variable.
    – AlexG
    Oct 5, 2012 at 12:12
  • Has the main_memory max been increased to 12 435 455 as that is where I get it exceeded. Is there any plans to increase this in future builds? Jan 20, 2016 at 1:56

Increasing the memory may help you to compile your plots but you will end up with very big files that takes lots of time to load by pdf viewers and make scrolling jerky. In addition, as you increase your plots complexity you might hit a performance cap.

Here is another solution that gives very small files while keeping the 'crispiness' of the full tikz approach.

  1. Plot each elements that are of differing colour on different graphics. Ensure that the axes limits are the same, remove the axis (axis off in matlab) and export them as high resolution png using the print command

    print -dpng -r800 *name_of_file.png*
  2. Convert the .png to bitmap with either gimp or imagemagick. With the latter :

    convert *name_of_file.png* *name_of_file.bmp*

You might want to directly export in bmp format from Matlab. I have not tested it though and have no license at hand right now.

  1. Use potrace to retrieve an .svg vector trace of your .bmp

    potrace *name_of_file.bmp* -s
  2. Then recombine the .svg with Inkscape, applying wanted colours that have been lost when using potrace.

  3. Either export your final result as pdf or as tikz code using Inkscape inkscape2tikz extension. If the former, use \addplot graphic (see pgfplots manual about that command).

I've use that technique on scatter plots with 10 000 points that overlap quite a lot. The resulting files are very small but almost perfectly slick while .png look edgy when one fancy to zoom a lot.

You must log in to answer this question.

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