摆脱matlabgraphicspdf输出的空白

我想在LaTeX文档中使用我的matlab图的PDF版本。 我正在使用带有PDF选项的“saveas”命令来保存数字,但是我在pdf文件的图中获得了巨大的空白空间。 这是正常的吗? 我怎样才能摆脱它? 自动,当然,因为我有“很多”的情节。

出口数字出版是一个很好的起点。 而不是-deps使用PDF输出的PDF文件。

您可以使用下面的代码修复边界框问题。

 set(gcf, 'PaperSize', [6.25 7.5]); set(gcf, 'PaperPositionMode', 'manual'); set(gcf, 'PaperPosition', [0 0 6.25 7.5]); set(gcf, 'PaperUnits', 'inches'); set(gcf, 'PaperSize', [6.25 7.5]); set(gcf, 'PaperPositionMode', 'manual'); set(gcf, 'PaperPosition', [0 0 6.25 7.5]); set(gcf, 'renderer', 'painters'); print(gcf, '-dpdf', 'my-figure.pdf'); print(gcf, '-dpng', 'my-figure.png'); print(gcf, '-depsc2', 'my-figure.eps'); 

你可以在Tobin的文章中阅读更多关于这个的内容。

上面的答案似乎太复杂了。 这个函数使用一个graphics句柄和一个string打印PDF文件的东西没有巨大的利润率。

 function printpdf(h,outfilename) set(h, 'PaperUnits','centimeters'); set(h, 'Units','centimeters'); pos=get(h,'Position'); set(h, 'PaperSize', [pos(3) pos(4)]); set(h, 'PaperPositionMode', 'manual'); set(h, 'PaperPosition',[0 0 pos(3) pos(4)]); print('-dpdf',outfilename); 

例如,要打印当前graphics,可以使用以下命令来调用它:

 printpdf(gcf,'trash') 

然而,如果你真的想要一个像Matlab生成的eps一样的pdfgraphics,也就是说,只有graphics的矩形凸包(或者一组子图),这里是更复杂的版本:

 function printpdf(h,outfilename) % first use the same non-relative unit system for paper and screen (see % below) set(h,'PaperUnits','centimeters'); % now get all existing plots/subplots a=get(h,'Children'); nfigs=length(a); % bounds will contain lower-left and upper-right corners of plots plus one % line to make sure single plots work bounds=zeros(nfigs+1,4); bounds(end,1:2)=inf; bounds(end,3:4)=-inf; % generate all coordinates of corners of graphs and store them in % bounds as [lower-left-x lower-left-y upper-right-x upper-right-y] in % the same unit system as paper (centimeters here) for i=1:nfigs set(a(i),'Unit','centimeters'); pos=get(a(i),'Position'); inset=get(a(i),'TightInset'); bounds(i,:)=[pos(1)-inset(1) pos(2)-inset(2) ... pos(1)+pos(3)+inset(3) pos(2)+pos(4)+inset(4)]; end % compute the rectangular convex hull of all plots and store that info % in mypos as [lower-left-x lower-left-y width height] in centimeters auxmin=min(bounds(:,1:2)); auxmax=max(bounds(:,3:4)); mypos=[auxmin auxmax-auxmin]; % set the paper to the exact size of the on-screen figure using % figure property PaperSize [width height] set(h,'PaperSize',[mypos(3) mypos(4)]); % ensure that paper position mode is in manual in order for the % printer driver to honor the figure properties set(h,'PaperPositionMode', 'manual'); % use the PaperPosition four-element vector [left, bottom, width, height] % to control the location on printed page; place it using horizontal and % vertical negative offsets equal to the lower-left coordinates of the % rectangular convex hull of the plot, and increase the size of the figure % accordingly set(h,'PaperPosition',[-mypos(1) -mypos(2) ... mypos(3)+mypos(1) mypos(4)+mypos(2)]); % print stuff print('-dpdf',outfilename); 

关于这个b4,我感到有些不解。 保存为.eps,然后将.eps转换为.pdf。 在Mac OS中,这可以在预览中完成。

我只花了一些时间来尝试大部分这些选项,但是我的朋友Espen指出了最简单的方法:如果您在LaTeX中使用了graphicx软件包,请使用标准的Matlab pdf输出并使用\ includegraphics中的trim选项。 Beamer幻灯片中的示例:

\ includegraphics [trim = 0.1in 2.5in 0.1in 2.5in,clip,scale = 0.5] {matlabgraphic.pdf}

这里修剪参数的顺序是左边,底部,右边,顶部。 关键是要削减顶部和底部很多,摆脱多余的空间。 更多在维基教科书 。

对于光栅图像输出(例如png),我的Makefile中有以下内容:

 convert -trim input.png input-trimmed.png 

这需要imagemagick。

更新:对于我最近的所有出版物,我使用了https://github.com/altmany/export_fig ,这是迄今为止我发现的最好的解决scheme(以及许多其他解决scheme,在单个包中的其他问题)。 我觉得这个工具至less和Matlab的官方部分一样强大。 我用它作为:

 export_fig -transparent fig.pdf 

其中导出当前图,默认情况下裁剪输出。

需要ghostcript

在MATLAB 文件交换中,我发现了JürgSchwizer提供的非常有用的function:

 plot2svg( filename, figHandle ); 

将graphics输出为vectorgraphics(.svg),将各个graphics元素输出为像素graphics(默认为.png)。 这使您可以对graphics进行各种操作(删除标签,移动颜色条等),但是如果您有兴趣去除白色背景,只需使用例如inkscape打开.svg文件,取消组合项目并导出你感兴趣的项目作为一个新的数字。

对于那些使用Linux的人来说,一个非常简单的解决scheme就是在shell中写入: ps2pdf14 -dPDFSETTINGS=/prepress -dEPSCrop image.eps

我也遇到过这个问题。 最后,我用下面的方法解决了它可以自动保存一个matlabgraphics作为一个合适的pdf大小。

你可以在MATLAB中做到这一点:

 h = figure; % For example, h = openfig('sub_fig.fig'); Or you just ploted one figure: plot(1:10); set(h,'Units','Inches'); pos = get(h,'Position'); set(h,'PaperPositionMode','Auto','PaperUnits','Inches','PaperSize',[pos(3),pos(4)]); print(h,'your_filename','-dpdf','-r0'); 

希望能帮助到你。

我最喜欢@ Antonio的方法,但是它仍然留下了太多的空白,我正在寻找一个单一的阴谋解决scheme,导出到vectorPDF格式的LaTeX使用。

  • 我做了一些基于他的脚本的东西,并添加了只导出图块的选项(省略坐标轴)。

  • 请注意,不像安东尼奥的脚本,这只适用于没有子图的数字。

  • 下面的代码将导出任何单个绘图图柄作为vectorPDF,无论是否有轴。


 function SaveFigureAsVectorPDF(InputFigureHandle, OutFileName, ShouldPrintAxes) %% Check input parameters [NumberOfFigures, ~] = size(InputFigureHandle); if(NumberOfFigures ~= 1) error('This function only supports one figure handle.'); end if(isempty(OutFileName)) error('No file path provided to save the figure to.'); end cUnit = 'centimeters'; %% Copy the input figure so we can mess with it %Make a copy of the figure so we don't modify the properties of the %original. FigureHandleCopy = copy(InputFigureHandle); %NOTE: Do not set this figure to be invisible, for some bizarre reason % it must be visible otherwise Matlab will just ignore attempts % to set its properties. % % I would prefer it if the figure did not briefly flicker into % view but I am not sure how to prevent that. %% Find the axis handle ChildAxisHandles = get(FigureHandleCopy, 'Children'); NumberOfChildFigures = length(ChildAxisHandles); if(NumberOfChildFigures ~= 1) %note that every plot has at least one child figure error('This function currently only supports plots with one child figure.'); end AxisHandle = ChildAxisHandles(1); %% Set Units % It doesn't matter what unit you choose as long as it's the same for % the figure, axis, and paper. Note that 'PaperUnits' unfortunately % does not support 'pixels' units. set(FigureHandleCopy, 'PaperUnits', cUnit); set(FigureHandleCopy, 'Unit', cUnit); set(AxisHandle, 'Unit', cUnit); %% Get old axis position and inset offsets %Note that the axes and title are contained in the inset OldAxisPosition = get(AxisHandle, 'Position'); OldAxisInset = get(AxisHandle, 'TightInset'); OldAxisWidth = OldAxisPosition(3); OldAxisHeight = OldAxisPosition(4); OldAxisInsetLeft = OldAxisInset(1); OldAxisInsetBottom = OldAxisInset(2); OldAxisInsetRight = OldAxisInset(3); OldAxisInsetTop = OldAxisInset(4); %% Set positions and size of the figure and the Axis if(~ShouldPrintAxes) FigurePosition = [0.0, 0.0, OldAxisWidth, OldAxisHeight]; PaperSize = [OldAxisWidth, OldAxisHeight]; AxisPosition = FigurePosition; else WidthWithInset = OldAxisWidth + OldAxisInsetLeft + OldAxisInsetRight; HeightWithInset = OldAxisHeight + OldAxisInsetTop + OldAxisInsetBottom; FigurePosition = [0.0, 0.0, WidthWithInset, HeightWithInset]; PaperSize = [WidthWithInset, HeightWithInset]; AxisPosition = [OldAxisInsetLeft, OldAxisInsetBottom, OldAxisWidth, OldAxisHeight]; end set(FigureHandleCopy, 'Position', FigurePosition); set(AxisHandle, 'Position', AxisPosition); %Note: these properties do not effect the preview but they are % absolutely necessary for the pdf!! set(FigureHandleCopy, 'PaperSize', PaperSize); set(FigureHandleCopy, 'PaperPosition', FigurePosition); %% Write the figure to the PDF file print('-dpdf', OutFileName); set(FigureHandleCopy, 'name', 'PDF Figure Preview', 'numbertitle', 'off'); %If you want to see the figure (eg, for debugging purposes), comment %the line below out. close(FigureHandleCopy); end 

示例图像:

用轴:

带有坐标轴的示例PDF截图

没有轴:

示例PDF截图没有轴

testing代码:

 %% Generates a graph and saves it to pdf FigureHandle = figure; plot(1:100); title('testing'); %with axes SaveFigureAsVectorPDF(FigureHandle, 'withaxes.pdf', true); %without axes SaveFigureAsVectorPDF(FigureHandle, 'withoutaxes.pdf', false); 

这适用于显示目的:

 set(gca, 'LooseInset', get(gca, 'TightInset')); 

也应该为印刷工作。

你也可以在乳胶本身。

  1. 用notpad打开eps文件
  2. 去第一行(%!PS-Adobe-3.1 EPSF- 3.0

如果最后一个数字是3,则search“rf”,然后在该行的开头注释该行input%

否则,如果最后一个数字是2,则search“pr”,然后在该行的开头注释该行input%

对于Linux用户,以下命令可能会有所帮助

ps2epsi <input.eps> <output.eps>

如这里所build议的

如果您打算使用乳胶,请使用Latex命令\includegraphics*而不是\includegraphics

以下2步方法为我工作(使用pdfcrop)。 假设你已经安装了所有的PDF工具和PDFcrop( http://pdfcrop.sourceforge.net/

在MATLAB中input

 print -deps -r600 figure.eps 

然后在命令行

 ./cropEpsFigure.sh figure 

使用以下文件:cropEpsFigure.sh

 #!/bin/bash /usr/texbin/epstopdf "$1.eps" /usr/texbin/pdfcrop "$1.pdf" /usr/local/bin/pdftops -eps "$1-crop.pdf"` 

在Matlab中将绘图保存为.eps格式,然后在Linux下执行esptopdf命令。 这不需要任何额外的编码。 只需要一台Linux机器。

如果你有现有的PDF格式,并且有额外的空格,你可以在Inkscape中使用这个命令。

 Inkscape.exe --file SomePDFWithWhitespace.pdf --export-pdf=SomePDFWithWhitespaceRemoved.pdf --export-area-drawing 

虽然请注意,这种方法仍然留下一些空白,因为导出graphics的“绘图区域”本身包含一些空白。

使用Inkscape的效果如下所示:

之前:(使用文件 – >另存为保存图)

PDF使用文件 - >另存为保存

之后:(使用Inkscape)

PDF使用上面显示的inkscape命令裁剪