Code covered by the BSD License  

Highlights from
publish2latex

image thumbnail

publish2latex

by

 

09 Apr 2013 (Updated )

Using full Latex markup in m-files to generate high quality documentation. Version 1.3

generateLatex( fObjPOL )
function generateLatex( fObjPOL )
%
% Purpose : Give the object defining the necessary files, this procedure
% starts the process of generating the TEX file
%
% Use (syntax): generateLatex( fObjPOL )
%
% Input Parameters :
%   fObjPOL: Object containing the paths of the reqired files
%
% Return Parameters :
%
% Description and algorithms:
%
% References : 
%
% Author :  Matther Harker and Paul O'Leary
% Date :    29. Jan 2013
% Version : 1.0
%
% (c) 2013 Matther Harker and Paul O'Leary
% url: www.harkeroleary.org
% email: office@harkeroleary.org
%
% History:
%   Date:           Comment:
%
tempDirPOL = cd;
cd( fObjPOL.mPath );
temp = fObjPOL.mFile{1};
eolCharPOL = temp(end);
%
publishFigureFormat;
%
% Setup the fileNames
%
graphicNumberPOL = 0;
figureNumberPOL = 0;
%
% load the defined files
%
fObjPOL.loadFiles;
%
% Identify the cells in each matlab file
%
[mCellsPOL, noCellsPOL] = mFileCells( fObjPOL.mFile );
%------------------------------------------------------------------------
% The first cell from the MATLAB File defines the File header
%
cellStart = mCellsPOL(1,1);
cellEnd = mCellsPOL(1,2);
cellLines = fObjPOL.mFile( cellStart : cellEnd );
[preCodeLatex, codeLines, postCodeLatex] = analyzeCellLines( cellLines );
%
if ~(isempty(codeLines))&&(isempty(postCodeLatex))
    error('The first cell should contain only the LATEX header and no code');
end;
%
if ~isempty(preCodeLatex)
    noPre = length( preCodeLatex );
    fObjPOL.resultFile = cell(1);
    fObjPOL.resultFile{1} = ['%',eolCharPOL];
    for kPOL3=1:noPre
        fObjPOL.resultFile = [fObjPOL.resultFile;...
            removeLatexMarker(cellLines{preCodeLatex(kPOL3)})];
    end;
end;
%-------------------------------------------------------------------------
% Add the definitions file
%
fObjPOL.resultFile = [fObjPOL.resultFile; fObjPOL.latexDefsFile];
%-------------------------------------------------------------------------
% Now process the remaining cells
%
%
if fObjPOL.TOCRequired
    fObjPOL.resultFile = [fObjPOL.resultFile; ['%---------------',eolCharPOL] ];
    fObjPOL.resultFile = [fObjPOL.resultFile; ['\tableofcontents',eolCharPOL] ];
    fObjPOL.resultFile = [fObjPOL.resultFile; ['%',eolCharPOL] ];
    %fObjPOL.resultFile = [fObjPOL.resultFile; ['\clearpage',eolCharPOL] ];
    fObjPOL.resultFile = [fObjPOL.resultFile; ['%',eolCharPOL] ];
end;
%
for kPOL0=2:noCellsPOL
    fObjPOL.resultFile = [fObjPOL.resultFile; ['%-----------------',eolCharPOL] ];
    fObjPOL.resultFile = [fObjPOL.resultFile; [['% Cell ',int2str(kPOL0)],eolCharPOL] ];
    fObjPOL.resultFile = [fObjPOL.resultFile; ['%',eolCharPOL] ];
    screenOutput = [];
    graphicHandels = [];
    % process each cell
    cellStart = mCellsPOL(kPOL0,1);
    cellEnd = mCellsPOL(kPOL0,2);
    cellLines = fObjPOL.mFile( cellStart : cellEnd );
    [preCodeLatex, codeLines, postCodeLatex] = analyzeCellLines( cellLines );
    %------------------------------------------------------------------------
    % concatinate pre material
    %
    noPre = length( preCodeLatex );
    for kPOL3=1:noPre
        fObjPOL.resultFile = [fObjPOL.resultFile; removeLatexMarker(cellLines{preCodeLatex(kPOL3)}), eolCharPOL];
    end;
    %----------------------------------------------------------------------------------------
    % add the code lines as a listing
    %
    noLinesPOL = length( codeLines );
    if noLinesPOL > 0
        fObjPOL.resultFile = [fObjPOL.resultFile; ['%',eolCharPOL] ];
        fObjPOL.resultFile = [fObjPOL.resultFile; ['\begin{footnotesize}',eolCharPOL] ];
        fObjPOL.resultFile = [fObjPOL.resultFile;...
            ['\begin{lstlisting}[name=',fObjPOL.mName,']',eolCharPOL] ];
        fObjPOL.resultFile = [fObjPOL.resultFile; cellLines{codeLines} ];
        fObjPOL.resultFile = [fObjPOL.resultFile; ['\end{lstlisting}',eolCharPOL] ];
        fObjPOL.resultFile = [fObjPOL.resultFile; ['\end{footnotesize}',eolCharPOL] ];
        fObjPOL.resultFile = [fObjPOL.resultFile; ['\normalsize',eolCharPOL] ];
        fObjPOL.resultFile = [fObjPOL.resultFile; ['%',eolCharPOL] ];
    end;
    %
    codeLines = cellLines(codeLines);
    %
    line = fObjPOL.resultFile{1};
    eolCharPOL = line(end);
    %
    resultDir = fObjPOL.resultLatexPath;
    resultDirFigures = [resultDir, fObjPOL.publishName , 'Figures\'];
    [~,~,~] = mkdir( resultDirFigures );
    %--------------------------------------------------------------------
    % Execute the code
    %
    executableMCode = prepareMCode( codeLines );
    screenOutput = [];
    if ~isempty( executableMCode );
        TextFromM = evalc( executableMCode );
        outputLines = charArray2cells( TextFromM, eolCharPOL );
        screenOutput = [outputLines];
    end;
    graphicHandels = unique( graphicHandels );
    %
    %------------------------------------------------------------------
    % Add the screen output to the Latex
    %
    if ~isempty(screenOutput)
        fObjPOL.resultFile = [fObjPOL.resultFile; ['%',eolCharPOL] ];
        fObjPOL.resultFile = [fObjPOL.resultFile; ['\begin{footnotesize}',eolCharPOL] ];
        fObjPOL.resultFile = [fObjPOL.resultFile; ['\begin{verbatim}',eolCharPOL] ];
        fObjPOL.resultFile = [fObjPOL.resultFile; screenOutput ];
        fObjPOL.resultFile = [fObjPOL.resultFile; ['\end{verbatim}',eolCharPOL] ];
        fObjPOL.resultFile = [fObjPOL.resultFile; ['\end{footnotesize}',eolCharPOL] ];
        fObjPOL.resultFile = [fObjPOL.resultFile; ['\normalsize',eolCharPOL] ];
        fObjPOL.resultFile = [fObjPOL.resultFile; ['%',eolCharPOL] ];
    end;
    %-----------------------------------------------------------------
    % Add the figures
    %
    numberOfGraphics = length( graphicHandels );
    if numberOfGraphics > 0
        figureNumberPOL = figureNumberPOL + 1;
        %
        % the add the figures
        %-------------
        fObjPOL.resultFile = [fObjPOL.resultFile; ['%',eolCharPOL] ];
        fObjPOL.resultFile = [fObjPOL.resultFile;...
            ['\begin{figure}[H]\label{Figure',int2str(figureNumberPOL),'}',eolCharPOL] ];
        fObjPOL.resultFile = [fObjPOL.resultFile; ['   \centering',eolCharPOL] ];
        %
        for kPOL1=1:numberOfGraphics
            figHandel = graphicHandels(kPOL1);
            %
            % get the figure width
            %
            temppHandel = get(figHandel);
            pFigurePosition = temppHandel.Position;
            % Possible later extension to get the units from the figure and
            % use them here.
            pFigureunits = temppHandel.Units;
            pFigureWidth = pFigurePosition(3);
            %
            graphicNumberPOL = graphicNumberPOL + 1;
            fObjPOL.resultFile = [fObjPOL.resultFile;...
                ['   \includegraphics[width=',...
                num2str(pFigureWidth),'cm, clip = true]{',fObjPOL.publishName,'Figures/'] ];
            % select the figure
            figName = ['grachic',int2str(graphicNumberPOL)];
            figPathName = [resultDirFigures, figName];
            %
            figure2eps(figHandel,[figPathName,'temp']);
%            printFigure( figHandel, [figPathName,'temp'] , 'eps2' );
            %
            temp = cd;
            cd(resultDirFigures)
            [status, result] = system( ['ps2epsi ', [figPathName,'temp.eps '], [figPathName,'.eps']] );
            cd(temp);
            %
            fObjPOL.resultFile = [fObjPOL.resultFile; [figName] ];
            %
            fObjPOL.resultFile = [fObjPOL.resultFile; ['}',eolCharPOL] ];
        end;
        %
        % test for caption
        %
        noPost = length( postCodeLatex );
        for kPOL2=1:noPost
            fObjPOL.resultFile = [fObjPOL.resultFile; ['   ',removeLatexMarker(cellLines{postCodeLatex(kPOL2)})]];
        end;
        %
        fObjPOL.resultFile = [fObjPOL.resultFile; ['\end{figure}',eolCharPOL] ];
        fObjPOL.resultFile = [fObjPOL.resultFile; ['%',eolCharPOL] ];
        %
    end;
end;
%
fObjPOL.resultFile = [fObjPOL.resultFile; '\end{document}', eolCharPOL];
fObjPOL.resultFile = [fObjPOL.resultFile; '%----------------------------------------------------------------', eolCharPOL];
fObjPOL.resultFile = [fObjPOL.resultFile; '% This file was automatically generated by the tool publish2latex', eolCharPOL];
fObjPOL.resultFile = [fObjPOL.resultFile; '% written by:', eolCharPOL];
fObjPOL.resultFile = [fObjPOL.resultFile; '%', eolCharPOL];
fObjPOL.resultFile = [fObjPOL.resultFile; '% Matthew Harker and Paul O''Leary', eolCharPOL];
fObjPOL.resultFile = [fObjPOL.resultFile; '% email: office@harkeroleary.org', eolCharPOL];
fObjPOL.resultFile = [fObjPOL.resultFile; '% (c) 2013', eolCharPOL];
fObjPOL.resultFile = [fObjPOL.resultFile; '%----------------------------------------------------------------', eolCharPOL];

%
writeOutFile( fObjPOL.resultFile, fObjPOL.resultLatexPath, fObjPOL.resultLatexName );
cd(tempDirPOL)

Contact us