It seems that setting PaperPositionMode
to auto
will get rid of extraneous white space for EPS files, but not PDF's.
To get rid of white space for PDF's, I wrote a little script to resize the paper to the figure size. Since it's so short, I've included it below in case anyone else needs it.
It is inspired by this document, as well as this StackOverflow question.
My solution works by manipulating only the paper size, not the figure axes, because manipulating the axes runs into trouble with subplots. This also means that some white space will remain. In MATLAB's vocabulary, the figures is bounded by its OuterPosition
rather than TightInset
.
function [filename] = printpdf(fig, name)
% printpdf Prints image in PDF format without tons of white space
% The width and height of the figure are found
% The paper is set to be the same width and height as the figure
% The figure's bottom left corner is lined up with
% the paper's bottom left corner
% Set figure and paper to use the same unit
set(fig, 'Units', 'centimeters')
set(fig, 'PaperUnits','centimeters');
% Position of figure is of form [left bottom width height]
% We only care about width and height
pos = get(fig,'Position');
% Set paper size to be same as figure size
set(fig, 'PaperSize', [pos(3) pos(4)]);
% Set figure to start at bottom left of paper
% This ensures that figure and paper will match up in size
set(fig, 'PaperPositionMode', 'manual');
set(fig, 'PaperPosition', [0 0 pos(3) pos(4)]);
% Print as pdf
print(fig, '-dpdf', name)
% Return full file name
filename = [name, '.pdf'];
end