matlab2tikz is a great package for converting matlab/octave figures to tikz code.
However if the figure contains a lot of points the resulted tikz code is very heavy for latex to typeset. Some times compilation even fails with the message that: TeX capacity exceeded, sorry.
Is there a way to fix this?
matlab2tikzhas a very clever option of downsampling the figures during the conversion to tikz-pgf. It is calledminimumPointsDistance. This does remedy the situation in most of the times.However, sometimes 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
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)
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
minimumPointsDistancemust 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
minimumPointsDistanceable to be set at a specific value uniformly for all figures, producing reduced versions of uniform quality, normalisation is used, and nowminimumPointsDistance“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!