Tight bounding box around PDF of MATLAB figure

bounding-box, figure, matlab, pdf

Solution

You can format the bounding box as follows

figure(1)
hold on;
plot(1,1,'x')

ps = get(gcf, 'Position');
ratio =  ps(4) / ps(3)
paperWidth = 10;
paperHeight = paperWidth*ratio;


set(gcf, 'paperunits', 'centimeters');
set(gcf, 'papersize', [paperWidth paperHeight]);
set(gcf, 'PaperPosition', [0    0   paperWidth paperHeight]);


print(gcf, '-dpdf', 'test2.pdf');

For smaller borders, you can adjust the `paperposition` property, e.g.

set(gcf, 'PaperPosition', [-0.5   -0.5   paperWidth+0.5 paperHeight+0.5]);

~edit~

I corrected the calculation of the ratio because it was wrong, as pointed out by Space47's answer. (Thanks @Space47).

Problem

When creating a simple figure in MATLAB and saving it as PDF, the resulting PDF file will have a luxurious bounding box. ``` plot(1,1,'x') print(gcf, '-dpdf', 'test.pdf'); ``` (From the ratio of the output it seems they always put in on an A page.) Is there a simple way to get a tight bounding box around the PDF?

Original source