Thursday, 11 November 2010

Printing to pdf from Matlab via eps

I use pdflatex for my publications, both because it is convenient to have PDF as the output format, and also because it accepts many kinds of graphic files, like JPG and PNG. If you want to use a vector graphics format, then you can save your graphics as PDFs. This is quite easy to do in Matlab, but I have found that it often leads to rasterization artifacts in the graphics. Exporting to EPS seems to work a lot better in this regard. The solution: Exporting from Matlab in EPS format, and postconverting to PDF using the esptopdf function (part of the texlive-extra-utils). I have made a Matlab function to automate the process:

function printpdfviaeps(filename, varargin)
%PRINTPDFVIAEPS Print current figure to pdf via eps
%
% printpdfviaeps(filename) will save the current figure to filename.pdf,
% using a default paper size of width = 12 cm, height = 9 cm.
%
% printpdfviaeps(filename, width, height) lets you specify the width and
% height yourself.
%
% Martin Skjelvareid, 2010-11-11

width = 12; % Default width in centimeters
height = 9; % Default height in centimeters

if nargin > 1
width = varargin{1};
end
if nargin > 2
height = varargin{2};
end

set(gcf, 'PaperUnits', 'centimeters');
set(gcf, 'PaperSize', [width height]);
set(gcf, 'PaperPositionMode', 'manual');
set(gcf, 'PaperPosition', [0 0 width height]);

print('-deps2','-r150', filename)
system(['epstopdf ' filename '.eps']);
delete([filename '.eps'])

1 comments:

H said...

Thanks, I guess it should be useful some day

Post a Comment