%PLOT2EPS Function to streamline the workflow of converting plots to eps
% files for inclusion in LaTeX. Requires plotepstex.m and laprint.m
% available at http://www.mathworks.com/matlabcentral/fileexchange/
%
% PLOT2EPS() If all input arguments are omitted, the function will run in
% user input mode prompting the user for the appropriate inputs.
%
% PLOT2EPS(FIGNAME) Converts all figures in the FIGNAME.FIG file. Other
% arguments are set to defaults. eps2pdf.bat is generated to convert the
% eps files to pdf if the user wishes.
%
% PLOT2EPS(FIGHANDLE) Converts all figures in the FIGHANDLE vector. Other
% arguments are set to defaults. eps2pdf.bat is generated to convert the
% eps files to pdf if the user wishes.
%
% PLOT2EPS(FIG,'Option',value,...) Specifies one or more of the following
% options:
% 'filename': Specify a cell row vector with filenames for the eps
% figures. The cell vector should have the same number of cells as there
% are figures to be converted. If not specified, plot2eps will attempt to
% derive filenames from the figure titles. When more than one subplot is
% present, the title of the last subplot is used.
% 'width': Specify the width (in cm) for all figures. This can be changed
% to allow more room for label texts. Default is 15.
% 'ratio': Specify the figure width/height ratio as a scalar if all are
% the same or as a vector to set different values for each figure.
% 'deletetitle': Boolean value to be able to delete the title from
% figures. This may be desirable in a LaTeX document where captions are
% used to describe the figures.
% 'dir': Specify a full path to the location where the eps files should
% be saved. Leave blank for saving in the current directory.
% 'fontsize': specify the font size for labels in points, default is 10.
%
% plot2eps uses plotepstex by J.C. Olivier (http://www.jc-olivier.2007.fr)
% and laprint by Arno Linnemann (linnemann@uni-kassel.de).
%
% Copyright (c) 2009, Mark J. Verveld
% All rights reserved.
%
% Redistribution and use in source and binary forms, with or without
% modification, are permitted provided that the following conditions are
% met:
%
% * Redistributions of source code must retain the above copyright
% notice, this list of conditions and the following disclaimer.
% * Redistributions in binary form must reproduce the above copyright
% notice, this list of conditions and the following disclaimer in
% the documentation and/or other materials provided with the distribution
%
% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
% AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
% IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
% ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
% LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
% CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
% SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
% INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
% CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
% ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
% POSSIBILITY OF SUCH DAMAGE.
function [] = plot2eps(varargin)
% close all;clear;clc
set(0,'defaulttextinterpreter','none')
warning('off', 'MATLAB:tex');
PACKAGES = 'amsmath, amssymb';
dir = '';
disp('===============================')
disp('= plot2eps by Mark J. Verveld =')
disp('===============================')
disp('---------------------------------------------------------------------')
disp('This workflow function can convert a number of figures stored in a')
disp('.fig file or currently opened to .eps files for inclusion in LaTeX.')
disp('Options for file naming are given. It is based on plotepstex and')
disp('laprint. These can be found on http://www.mathworks.com/matlabcentral')
disp('---------------------------------------------------------------------')
disp(' ')
switch nargin
case 0
disp('Running in user input mode')
disp(' ')
openfigfile = input('Do you wish to open a .fig file? (y/n)\notherwise the presently opened figures will be converted\n\n','s');
if strcmp(openfigfile,'y')
disp('Warning: save all open figures, they will be closed.')
disp(' ')
pause
close all
figname = input('Enter the filename of the figure set you wish to convert:\n\n','s');
hgload(figname)
end
h = findobj('Type','figure');
findfigs
width = input('Enter the figure width (in cm) for all figures, default is 15:\n\n');
if isempty(width)
width = 15;
end
ratio = input('Enter the width/height ratios or leave blank for default (15/12):\n\n');
if isempty(ratio)
ratio = 15/12*ones(1,length(h));
elseif length(ratio) == 1
ratio = ratio*ones(1,length(h));
end
fontsize = input('Enter the fontsize for labels, default is 10 points:\n\n');
if isempty(fontsize)
fontsize = 10;
end
usetitlename = input('Do you wish to use the figure titles as filenames? (y/n)\n(They may not contain forbidden characters):\n\n','s');
if strcmp(usetitlename,'y')
filename = cell(1,length(h));
for index = 1:length(h)
filename{index} = get(get(get(h(index),'CurrentAxes'),'Title'),'String');
if isempty(filename{index})
warning('PLOT2EPS:FileNaming','Found an empty title, defaulting to latexfigure%s.eps',num2str(index))
filename{index} = ['latexfigure' num2str(index)];
end
end
deletetitle = input('Do you wish to delete the title from the figures\n after copying it to the filename? (y/n):\n\n','s');
if strcmp(deletetitle,'y')
for index = 1:length(h)
set(get(get(h(index),'CurrentAxes'),'Title'),'String','');
end
end
else
deletetitle = input('Do you wish to delete the title from the figures? (y/n):\n\n','s');
if strcmp(deletetitle,'y')
for index = 1:length(h)
set(get(get(h(index),'CurrentAxes'),'Title'),'String','');
end
end
filename = input('Enter a cell array of strings for the output filenames {\''...\'',\''...\''}:\n\n');
assert(isa(filename,'cell'),'The input is not a cell array.')
r = size(filename,1);
assert(r == 1,'The cell array is not a row vector.')
assert(length(filename) == length(h),'The cell array contains an incorrect number of names.')
end
dir = input('Specify the full path to the location where the eps files should be saved.\nLeave blank for saving in the current directory.\n\n','s');
if ~isempty(dir)
assert(exist(dir,'dir') == 7,'The directory path does not exist.')
end
case 1
disp('Running in command line mode')
disp(' ')
% Handle the input argument
if isa(varargin{1},'char')
figname = varargin{1};
close all
hgload(figname)
h = findobj('Type','figure');
findfigs;
elseif isa(varargin{1},'double')
h = varargin{1};
findfigs;
else
error('The first input should be either a filename or figure handle vector.')
end
% Set the rest to defaults
width = 15;
ratio = 15/12*ones(1,length(h));
fontsize = 10;
filename = cell(1,length(h));
for index = 1:length(h)
filename{index} = get(get(get(h(index),'CurrentAxes'),'Title'),'String');
if isempty(filename{index})
warning('PLOT2EPS:FileNaming','Found an empty title, defaulting to latexfigure%s.eps',num2str(index))
filename{index} = ['latexfigure' num2str(index)];
end
end
case {3, 5, 7, 9, 11, 13}
disp('Running in command line mode')
disp(' ')
% Handle the first input argument
if isa(varargin{1},'char')
figname = varargin{1};
close all
hgload(figname)
h = findobj('Type','figure');
findfigs
elseif isa(varargin{1},'double')
h = varargin{1};
findfigs
else
error('The first input should be either a filename or figure handle vector.')
end
% Routine to determine which arguments are set
filename = {};
ratio = [];
width = [];
fontsize = [];
deletetitle = false;
dir = '';
for index = 2:2:nargin-1
switch varargin{index}
case 'filename'
assert(isa(varargin{index+1},'cell'),'filename should be a cell array.')
filename = varargin{index+1};
case 'ratio'
assert(isa(varargin{index+1},'numeric'),'ratio should be a numeric vector.')
ratio = varargin{index+1};
case 'deletetitle'
assert(isa(varargin{index+1},'logical'),'deletetitle should be a logical.')
deletetitle = varargin{index+1};
case 'dir'
assert(isa(varargin{index+1},'char'),'dir should be a string of chars.')
dir = varargin{index+1};
if ~isempty(dir)
assert(exist(dir,'dir') == 7,'The directory path does not exist.')
end
case 'width'
assert((isa(varargin{index+1},'numeric') && length(varargin{index+1})==1),'width should be a numeric scalar.')
width = varargin{index+1};
case 'fontsize'
assert((isa(varargin{index+1},'numeric') && length(varargin{index+1})==1),'fontsize should be a numeric scalar.')
fontsize = varargin{index+1};
otherwise
error('Unknown option identifier string encountered.')
end
end
if isempty(filename)
filename = cell(1,length(h));
for index = 1:length(h)
filename{index} = get(get(get(h(index),'CurrentAxes'),'Title'),'String');
if isempty(filename{index})
warning('PLOT2EPS:FileNaming','Found an empty title, defaulting to latexfigure%s.eps',num2str(index))
filename{index} = ['latexfigure' num2str(index)];
end
end
else
r = size(filename,1);
assert(r == 1,'The filename cell array is not a row vector.')
assert(length(filename) == length(h),'The filename cell array contains an incorrect number of names.')
end
if isempty(width)
width = 15;
end
if isempty(fontsize)
fontsize = 10;
end
if isempty(ratio)
ratio = 15/12*ones(1,length(h));
elseif length(ratio) == 1
ratio = ratio*ones(1,length(h));
else
assert(length(ratio) == length(h),'The vector of ratios has incorrect length.')
end
if deletetitle
for index = 1:length(h)
set(get(get(h(index),'CurrentAxes'),'Title'),'String','');
end
end
otherwise
error('plot2eps takes up to 7 input arguments, with option identifer strings from the second argument on.')
end
if ~isempty(dir) && ~(dir(end) == '\')
dir = [dir '\'];
end
fid = fopen([dir 'epstopdf.bat'], 'wt');
h = sort(h);
for index = 1:length(h)
% run the plotepstex command
plotepstex(h(index),[dir filename{1,index}],[width ratio(index) fontsize],PACKAGES)
fprintf('\nFigure %1.0f has been saved as: %s.eps',h(index),filename{1,index})
if ~isempty(dir)
fprintf('\nin: %s',dir)
end
fprintf(fid, 'epstopdf.exe %s.eps\n', filename{1,index});
end
fprintf('\n')
fclose(fid);