Code covered by the BSD License  

Highlights from
AnimateLaTeX

image thumbnail
from AnimateLaTeX by Per Bergström
Generates animation source code in LaTeX.

AniLaTeX=animategraphicsLaTeX(var1,var2,var3,var4,var5,var6)
function AniLaTeX=animategraphicsLaTeX(var1,var2,var3,var4,var5,var6)
% animategraphicsLaTeX generates LaTeX code for animations
% using animategraphics in the LaTeX animate package.
%
%     The animation code is done in two steps.
%         i,  A data structure is created.
%         ii, The code is written.
%
%     i, The data structure AniLaTeX is initiated by an
%         empty matrix [] (AniLaTeX=[];)
%
%         Frames are inserted using the command:
%
%     AniLaTeX=animategraphicsLaTeX(AniLaTeX,fileName,figureDirectory, ...
%     includegraphicsOptions,frameRate,star);
%
%     (4 or more in arguments)
%
%     AniLaTeX - data structure
%     fileName - name of graphics file (string) (for example "fig1.eps")
%                The numbering must be increasing with one for each added
%                graphics file, (ex "fig1.eps", "fig2.eps", "fig3.eps" ...)
%     figureDirectory - directory where the graphics file is. (string)
%                     Information to LaTeX. (same directory, empty string '')
%     frameContent - (optional) Information to \animategraphics. (string)
%                             (default <transparency ID>).
%                             (information in 3:rd column in timeline file)
%                             (no infromation, emty string '')
%                             (see documentation for \animategraphics, timeline)
%     frameRate - (optional) framerate (default 1)
%                 (see documentation for \animategraphics, timeline)
%     star - (optional) '*' or '' (default). If the frame should be marked with a star.
%            That means a pause is inserted into the animation.
%            (see documentation for \animategraphics, timeline)
%
%
%     ii, The LaTeX code is generated using the command:
%
%     animategraphicsLaTeX(AniLaTeX,fileName,animategraphicsOptions);
%
%     (3 in arguments)
%
%     AniLaTeX - data structure
%     fileName - name of TEX-file (no extension like .tex) (string)
%     animategraphicsOptions - Information to animategraphics. (string)
%                         (information inside [] like autoplay, loop etc.)
%                         (see documentation for animategraphics)
%
%     The LaTeX code with name fileName.tex for the animation will be created.
%     If a 'timeline' file is needed it will be created and has to be placed in the same
%     directory as the graphics files.
%     A batch file with name fileName.bat will be created for converting
%     eps figures to pdf using epstopdf.
%
%     See animateEx (examples) for more information about the usage.
%
%     written by Per Bergstrm 2007-12-12
%
%     Free for download at
%
%     http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objectId=17813&objectType=file
%
%     e-mail: per.bergstrom 'at' ltu.se

if nargin>3
    fileName=var2;
    figureDirectory=var3;
    if nargin<6
        var6='';
        if nargin<5
            var5=1;
        end
    end

    includegraphicsOptions=var4;
    frameRate=var5;
    star=var6;

    if isempty(var1)
        AniLaTeX=cell(1,5);
    else
        AniLaTeX=var1;
        AniLaTeX=[AniLaTeX;cell(1,5)];
    end

    AniLaTeX{end,1}=includegraphicsOptions;
    AniLaTeX{end,2}=figureDirectory;
    AniLaTeX{end,3}=fileName;

    if ischar(frameRate)
        AniLaTeX{end,4}=str2num(frameRate);
    else
        AniLaTeX{end,4}=frameRate;
    end

    if length(star)==1
        if or(or(star=='*',star=='p'),star=='P')
            AniLaTeX{end,5}='*';
        end
    elseif length(star)==5
        if or(or(star=='pause',star=='PAUSE'),star=='Pause')
            AniLaTeX{end,5}='*';
        end
    end

elseif nargin==3

    AniLaTeX=var1;
    fileName=var2;
    animategraphicsOptions=var3;

    if length(fileName)>4
        if or(or(fileName(end-3:end)=='.tex',fileName(end-3:end)=='.eps'),fileName(end-3:end)=='.pdf')
            fileName=fileName(1:end-4);
        end
    end

    fileName(find(fileName==' '))='';
    fileName(find(fileName=='.'))='';

    % .tex fil

    fid = fopen([fileName,'.tex'],'w');

    fprintf(fid,['%% Insert\n']);
    fprintf(fid,['%% \\input{',fileName,'}\n']);
    fprintf(fid,['%% in your original TEX file.\n']);
    fprintf(fid,'%% Requires the animate package!\n');
    fprintf(fid,'%% http://www.ctan.org/tex-archive/macros/latex/contrib/animate/\n');
    fprintf(fid,'%% Include the packages \n');
    fprintf(fid,'%% \\usepackage{animate} \n');
    fprintf(fid,'%% \\usepackage{graphicx} \n');
    fprintf(fid,'%% in the preamble of the original LaTeX document.\n');
    
    needsTimeline=0;
    
    if isempty(AniLaTeX{1,4})
        frmrt0=1;
    else
        frmrt0=AniLaTeX{1,4};
    end

    for i=1:size(AniLaTeX,1)

        if isempty(AniLaTeX{i,4})
            frmrt=1;
        else
            frmrt=AniLaTeX{i,4};
        end

        if abs(frmrt-frmrt0)>1e-4
            needsTimeline=1;
        elseif or(not(isempty(AniLaTeX{i,1})),not(isempty(AniLaTeX{i,5})))
            needsTimeline=1;
        end
        if needsTimeline
            break
        end
    end

    if needsTimeline
        fprintf(fid,'%% The timeline file has to be placed in the same\n');
        fprintf(fid,'%% directory as the graphics files.\n');
        fprintf(fid,'%% The timeline file is:\n');
        fprintf(fid,['%% ',fileName,'TimeLine.txt \n']);
    end

    fprintf(fid,'%% \n');
    fprintf(fid,'%% TEX file generated by animategraphicsLaTeX.\n');
    fprintf(fid,'%% animategraphicsLaTeX is written by Per Bergstrom \n');
    fprintf(fid,'%% and is free for download at \n');
    fprintf(fid,'%% http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objectId=17813&objectType=file\n\n\n');

    disp(' ');
    disp('Creates a TEX file containing the animation to be inserted into the original TEX file.');
    disp([fileName,'.tex']);
    disp(' ');

    mile=9999999999999999999;
    for i=1:size(AniLaTeX,1)
        if length(AniLaTeX{i,3})<mile
            mile=length(AniLaTeX{i,3});
        end
    end

    filefigname=AniLaTeX{1,3};
    
    if size(AniLaTeX,1)>1
        bol=0;

        for j=1:mile
            for i=2:size(AniLaTeX,1)
                filefigname2=AniLaTeX{i,3};
                if filefigname2(j)~=filefigname(j)
                    bol=1;
                    break
                end
            end
            if bol
                break
            end
        end
        
        for ii=(j-1):-1:1
            if any(filefigname(ii)=='0123456789')
                j=j-1;
            else
                break;
            end            
        end

        file_basname=filefigname(1:(j-1));
        
        if length(filefigname)>4
           if(filefigname(end-3)=='.')
               endind=length(filefigname)-4;
           else
               endind=length(filefigname);
           end
        else
            endind=length(filefigname);
        end

        fir=filefigname(j:endind);

        filefigname2=AniLaTeX{end,3};

        if length(filefigname2)>4
           if(filefigname2(end-3)=='.')
               endind=length(filefigname2)-4;
           else
               endind=length(filefigname2);
           end
        else
            endind=length(filefigname2);
        end

        las=filefigname2(j:endind);
    else
        siffror=logical(zeros(1,mile));
        for ii=1:mile
            if any(filefigname(ii)=='0123456789')
                siffror(ii)=1;
            end
        end
        endind=find(siffror,1,'last');
        for ii=endind:-1:1
            if not(siffror(ii))
                j=ii+1;
                break;
            end
        end
        
        file_basname=filefigname(1:(j-1));
        
        fir=filefigname(j:endind);
        las=filefigname(j:endind);
    end

    figureDirectory=AniLaTeX{1,2};

    if length(figureDirectory)>1
        if figureDirectory(1)=='\'
            if figureDirectory(2)~='\'
                figureDirectory=['\',figureDirectory,' '];
            else
                figureDirectory=[figureDirectory,' '];
            end
        else
            figureDirectory(find(figureDirectory=='\'))='/';
            if figureDirectory(end)~='/'
                figureDirectory=[figureDirectory,'/'];
            end
        end
    elseif length(figureDirectory)>0
        figureDirectory(find(figureDirectory=='\'))='/';
        if figureDirectory(end)~='/'
            figureDirectory=[figureDirectory,'/'];
        end
    end
        
    if needsTimeline
        if isempty(animategraphicsOptions)
            animategraphicsOptions=['timeline=',figureDirectory,fileName,'TimeLine.txt'];
        else
            animategraphicsOptions=[animategraphicsOptions,',timeline=',figureDirectory,fileName,'TimeLine.txt'];
        end
    end

    if isempty(AniLaTeX{1,4})
        frrt=1;
    else
        frrt=AniLaTeX{1,4};
    end

    fprintf(fid,['\\animategraphics[',animategraphicsOptions,']{',num2str(frrt),'}{',figureDirectory,file_basname,'}{',fir,'}{',las,'}\n\n']);

    fclose(fid);

    if needsTimeline

        disp('Creates a TXT file with TimeLine options.');
        disp([fileName,'TimeLine.txt']);
        disp('Must be placed in the same directory as the graphics files.');
        disp(' ');

        % TimeLine .txt fil

        fid = fopen([fileName,'TimeLine.txt'],'w');

        for i=1:size(AniLaTeX,1)

            if isempty(AniLaTeX{i,4})
                frrt=1;
            else
                frrt=AniLaTeX{i,4};
            end

            frm_cnt=AniLaTeX{i,1};
            if isempty(frm_cnt)
                frm_cnt=num2str(i-1);
            end
            fprintf(fid,[AniLaTeX{i,5},':',num2str(frrt),':',frm_cnt,'       %% ',AniLaTeX{i,3},'\n']);
        end

        fclose(fid);

    end

    % .bat fil

    fid = fopen([fileName,'.bat'],'w');

    fprintf(fid,'@ECHO OFF\n');
    fprintf(fid,['ECHO ',fileName,'\n']);
    disp('Creates a batch file for converting EPS files to PDF files.');
    disp([fileName,'.bat']);
    disp('Requires epstopdf!');
    disp(' ');

    for i=1:size(AniLaTeX,1)

        graphicsfile=AniLaTeX{i,3};
        if length(graphicsfile)>4
            if graphicsfile((end-3):end)=='.pdf'
                graphicsfile=[graphicsfile(1:(end-3)),'eps'];
            end
        end

        fprintf(fid,['epstopdf ',graphicsfile,' \n']);

    end

    fclose(fid);

elseif nargin==1

    AniLaTeX=var1;

    for i=1:size(AniLaTeX,1)

        graphicsfile=AniLaTeX{i,3};

        if length(graphicsfile)>4
            if graphicsfile((end-3):end)=='.eps'
                graphicsfile=[graphicsfile(1:(end-3)),'pdf'];
            else
                graphicsfile=[graphicsfile,'.pdf'];
            end
        else
            graphicsfile=[graphicsfile,'.pdf'];
        end

        AniLaTeX{i,3}=graphicsfile;

    end

else
    error('Wrong numbers of input variables.');
end

try
    if nargout==0
        clear AniLaTeX;
    end
end

Contact us at files@mathworks.com