latexfigure: the code

note, this is only the code of the initial release – very likely the code has changed already.

function latexfigure(h,FileName,outputFormat,Packages)
 
debugOutput = false;
 
if nargin < 2
    FileName='LaTeXfile';
end
 
if nargin < 1
    h=gcf;
end
 
if (~any( strcmpi(outputFormat,{'pdf','eps','png'})))
    error(['latexfigure: can not handle grahics format: ' outputFormat]);
end
 
FragFigureName = strcat('TEMP',num2str(round(rand*10000))); %Generate random file name
 
if ~exist('matlabfrag.m', 'file')
    disp('MatLabFrag M-file does not exist or is not in the MATLAB''s search path.');
    disp('This file can be downloaded at: http://www.mathworks.com/matlabcentral/fileexchange');
    disp('                                                         Try again...');
    return;
end
 
%-------------------------------------------------------
% call matlabfrag to export figure to .eps and .tex file.
%
% create two files 
% - 'FragFigureName.eps' <- image
% - 'FragFigureName.tex' <- frag text
%-------------------------------------------------------
matlabfrag(FragFigureName,'handle',h);
fix_lines([FragFigureName '.eps']);
 
%-------------------------------------------------------
% Temporary LaTeX file (including the figure)
%-------------------------------------------------------
% creates 'FragFigureName-main.tex'
createLaTeXFile();
 
%-------------------------------------------------------
% run LaTeX with document file including the figure
%-------------------------------------------------------
success = runLaTeX();
if (~success), return, end
 
% we now have these file
% - FragFigureName-main.tex
% - FragFigureName.tex
% - FragFigureName.eps
% - .. many FragFigureName-main.* files
 
 
%-------------------------------------------------------
% convert to destiny format
% (only for eps or png, not for pdf)
%-------------------------------------------------------
% convert to eps 
if (any( strcmpi(outputFormat,{'eps'})))
    ghostscriptConvertTo('eps', [FragFigureName '.pdf'], [FragFigureName '.eps']);
end
% convert from pdf to png
if (any( strcmpi(outputFormat,{'png'})))
    ghostscriptConvertTo(outputFormat, [FragFigureName '.pdf'], [FragFigureName '.png']);
end
 
%-------------------------------------------------------
% Rename FragFigureName to FileName and delete FileName 
% before (if it exists)
%-------------------------------------------------------
% delete any previous output file
if (exist([FileName '.' outputFormat], 'file'))
    delete([FileName '.' outputFormat]);
end
 
% rename Temporary file (FragFigureName) to target FileName
if isunix==0
   dos(['ren ' FragFigureName '.' outputFormat ' ' FileName '.' outputFormat]);
else 
   unix(['ren ' FragFigureName '.' outputFormat ' ' FileName '.' outputFormat]);
end
 
%-------------------------------------------------------
% Delete all the temporary files
%-------------------------------------------------------
if (~debugOutput)
    delete([FragFigureName '*']);
end
%-------------------------------------------------------
% functions...
%-------------------------------------------------------
 
    function createLaTeXFile()
        fid = fopen(strcat(FragFigureName,'-main.tex'),'w');
 
        fprintf(fid,'\\documentclass[11pt, oneside]{article}\n');
        fprintf(fid,'\\usepackage{graphicx}\n');
        fprintf(fid,'\\usepackage{amsmath}\n');
        fprintf(fid,'\\usepackage[T1]{fontenc}\n');
        fprintf(fid,'\\usepackage[latin1]{inputenc}\n');
        fprintf(fid,'\\usepackage{xcolor}\n');
 
        if (any( strcmpi(outputFormat,{'pdf','eps','png'})))
            fprintf(fid,'\\usepackage[]{pstool}\n'); % crop=pdfcrop
        end
 
        split=stringsplit(Packages,'\n');
        for i=1:size(split,2)
            fprintf(fid,'%s\n',split{i}); % Suplementary LaTeX Code
        end
 
        fprintf(fid,'\\pagestyle{empty}\n');
        fprintf(fid,' \n');
        fprintf(fid,'\\begin{document}\n');
        fprintf(fid,'   \\centering\n');
        fprintf(fid,'   \\psfragfig{%s}\n',FragFigureName);
        fprintf(fid,' \n');
        fprintf(fid,'\\end{document}\n');
        fclose(fid);
    end
 
 
    function success = runLaTeX()
        Str=sprintf([latexInterpreter() ' -shell-escape --src -interaction=nonstopmode %s-main.tex'], FragFigureName);
 
        if (debugOutput)
            disp(sprintf('\n[LaTeX Command] %s',Str));
        end
        [status, result]=system(Str);
 
        if (status == 0)
            success = true;
        else
            success = false;
        end
        if (success == false)
            if (~debugOutput)
                delete([FragFigureName '*']);
            end
            error('Error %d -- LATEX:\n%s',status ,result);
            return;
        end
    end
 
    function s = latexInterpreter()
        if (any( strcmpi(outputFormat,{'pdf', 'eps', 'png'})))
            s = 'pdflatex';
        else
            s = 'latex';
        end
    end
end