Home > . > prt_latex.m

prt_latex

PURPOSE ^

Extract information from the toolbox m-files and output them as usable

SYNOPSIS ^

function prt_latex(c)

DESCRIPTION ^

 Extract information from the toolbox m-files and output them as usable
 .tex files which can be directly included in the manual.

 There are 2 types of m2tex operations:
 1. converting the job configuration tree, i.e. *_cfg_* files defining the
    batching interface into a series of .tex files. 
    NOTE: Only generate .tex files for each exec_branch of prt_batch. 
 2. converting the help header of the functions into .tex files.

 These files are then included in a manually written prt_manual.tex file, 
 which also includes chapter/sections written manually.

 File derived from that of the SPM8 distribution.
 http://www.fil.ion.ucl.ac.uk/spm
_______________________________________________________________________
 Copyright (C) 2011 Machine Learning & Neuroimaging Laboratory

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function prt_latex(c)
0002 % Extract information from the toolbox m-files and output them as usable
0003 % .tex files which can be directly included in the manual.
0004 %
0005 % There are 2 types of m2tex operations:
0006 % 1. converting the job configuration tree, i.e. *_cfg_* files defining the
0007 %    batching interface into a series of .tex files.
0008 %    NOTE: Only generate .tex files for each exec_branch of prt_batch.
0009 % 2. converting the help header of the functions into .tex files.
0010 %
0011 % These files are then included in a manually written prt_manual.tex file,
0012 % which also includes chapter/sections written manually.
0013 %
0014 % File derived from that of the SPM8 distribution.
0015 % http://www.fil.ion.ucl.ac.uk/spm
0016 %_______________________________________________________________________
0017 % Copyright (C) 2011 Machine Learning & Neuroimaging Laboratory
0018 
0019 % Written by John Ashburner & Christophe Phillips
0020 % $Id: prt_latex.m 518 2012-04-17 10:01:12Z cphillip $
0021 
0022 
0023 %% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0024 % 1. Turning the cfg files into a .tex file
0025 if ~nargin,
0026     if ~exist('prt_cfg_batch.m','file'), prt; end
0027     c = prt_cfg_batch;
0028 end
0029 % if nargin && ischar(c),
0030 %     clean_latex_compile;
0031 %     return;
0032 % end
0033 
0034 for i=1:numel(c.values),
0035     bn = c.values{i}.tag;
0036     fp = fopen(fullfile(prt('dir'),'manual',['batch_',bn,'.tex']),'w');
0037     if fp==-1, sts = false; return; end;
0038     chapter(c.values{i},fp);
0039 end;
0040 
0041 %% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0042 % 2. picking all the functions help files and put them into functions.tex
0043 fp = fopen(fullfile(prt('dir'),'manual','adv_functions.tex'),'w');
0044 if fp==-1, sts = false; return; end;
0045 % l_subdirs = {'machines','batch'};
0046 l_subdirs = {'machines','utils'};
0047 excl_files = {'Contents.m'};
0048 PRTdir = prt('dir');
0049 
0050 % Heading part
0051 fprintf(fp,'\\chapter{%s  \\label{Chap:%s}}\n\\minitoc\n\n\\vskip 1.5cm\n\n',...
0052     texify('List of PRoNTo functions'),'sec:functions');
0053 fprintf(fp,'This is the list of PRoNTo functions, including the subdirectories: ');
0054 for ii=1:numel(l_subdirs)
0055     fprintf(fp,'%s',texify(['{\tt ',l_subdirs{ii},'}']));
0056     if ii<numel(l_subdirs)-1
0057         fprintf(fp,', ');
0058     elseif ii==numel(l_subdirs)-1
0059         fprintf(fp,' and ');
0060     else
0061         fprintf(fp,'.\n\n');
0062     end
0063 end
0064 
0065 % Deal with main directoy 1st
0066 f = spm_select('List',PRTdir,'.*\.m$');
0067 for ii=1:numel(excl_files)
0068     f(strcmp(cellstr(f),excl_files{ii}),:) = [];
0069 end
0070 write_mfiles_help(f,fp);
0071 
0072 % Deal with subdirectories
0073 for ii = 1:numel(l_subdirs)
0074     p = fullfile(PRTdir,l_subdirs{ii});
0075     fprintf(fp,'\n\\%s{%s}\n','section',texify(l_subdirs{ii}));
0076     f = spm_select('List',p,'.*\.m$');
0077     write_mfiles_help(f,fp,l_subdirs(ii));
0078 end
0079 
0080 return;
0081 
0082 %==========================================================================
0083 function write_mfiles_help(f,fp,base_dir)
0084 
0085 if nargin<3,
0086     base_dir = '';
0087     lev = 1;
0088 else
0089     lev = numel(base_dir)+1;
0090     tmp = base_dir{1};
0091     ltmp = tmp;
0092     for ii=2:numel(base_dir)
0093         ltmp = [tmp,'\textbackslash ',base_dir{ii}];
0094         tmp = fullfile(tmp,base_dir{ii});
0095     end
0096     lbase_dir = ltmp;
0097     base_dir = tmp;
0098 end
0099 
0100 sec = {'section','subsection','subsubsection','paragraph','subparagraph', ...
0101             'textbf','textsc','textsl','textit'};
0102 
0103 for ii=1:size(f,1)
0104     % section
0105     if isempty(base_dir)
0106         lfunc_name = deblank(f(ii,:));
0107     else
0108         lfunc_name = [lbase_dir,'\textbackslash ',deblank(f(ii,:))];
0109     end
0110     func_name = fullfile(base_dir,deblank(f(ii,:)));
0111     fprintf(fp,'\n\\%s{%s}\n',sec{min(lev,length(sec))},texify(lfunc_name));
0112     fprintf(fp,'%s\n\n',texify('\begin{alltt}'));
0113     
0114     % help text, minus copyrights
0115     htxt = textscan(fopen(func_name),'%s','delimiter','\n','whitespace','');
0116     htxt = htxt{1};
0117     i_beg = find(strncmp('% ',htxt,2)); i_beg = i_beg(1);
0118     i_end = find(strncmp('% Copyright (C)',htxt,15))-2;
0119     htxt = htxt(i_beg:i_end);
0120     
0121     for jj=1:numel(htxt)
0122         if strcmp(htxt{jj},'%')
0123             fprintf(fp,'%s\n',' ');
0124         else
0125             fprintf(fp,'%s\n',texify(htxt{jj}(2:end)));
0126         end
0127     end
0128     fprintf(fp,'%s\n\n',texify('\end{alltt}'));
0129 end
0130 
0131 return
0132 
0133 %==========================================================================
0134 function sts = chapter(c,fp)
0135 bn = c.tag;
0136 if nargin<2
0137     fp = fopen(fullfile(pwd,'manual',[bn,'.tex']),'w');
0138     if fp==-1, sts = false; return; end;
0139 end
0140 
0141 fprintf(fp,'%% $Id: prt_latex.m 518 2012-04-17 10:01:12Z cphillip $ \n\n');
0142 fprintf(fp, ...
0143     '\\chapter{%s  \\label{Chap:%s}}\n\n\\vskip 1.5cm\n\n', ...
0144     texify(c.name),c.tag);
0145 write_help(c,fp);
0146 
0147 switch class(c),
0148     case {'cfg_branch','cfg_exbranch'},
0149         for i=1:numel(c.val),
0150             section(c.val{i},fp);
0151         end;
0152     case {'cfg_repeat','cfg_choice'},
0153         for i=1:numel(c.values),
0154             section(c.values{i},fp);
0155         end;
0156 end;
0157 fclose(fp);
0158 sts = true;
0159 return;
0160 
0161 %==========================================================================
0162 function section(c,fp,lev)
0163 if nargin<3, lev = 1; end;
0164 sec = {'section','subsection','subsubsection','paragraph','subparagraph', ...
0165             'textbf','textsc','textsl','textit'};
0166 % if lev<=length(sec),
0167     fprintf(fp,'\n\\%s{%s}\n',sec{min(lev,length(sec))},texify(c.name));
0168     write_help(c,fp);
0169     switch class(c),
0170         case {'cfg_branch','cfg_exbranch'},
0171             for i=1:numel(c.val),
0172                 section(c.val{i},fp,lev+1);
0173             end;
0174         case {'cfg_repeat','cfg_choice'},
0175             for i=1:numel(c.values),
0176                 section(c.values{i},fp,lev+1);
0177             end;
0178     end;
0179 % else
0180 if lev>length(sec),
0181     warning(['Too many nested levels... ',c.name]); %#ok<WNTAG>
0182 end;
0183 return;
0184 
0185 %==========================================================================
0186 function write_help(hlp,fp)
0187 if isa(hlp, 'cfg_item'),
0188     if ~isempty(hlp.help),
0189         hlp = hlp.help;
0190     else
0191         return;
0192     end;
0193 end;
0194 if iscell(hlp),
0195     for i=1:numel(hlp),
0196         write_help(hlp{i},fp);
0197     end;
0198     return;
0199 end;
0200 str = texify(hlp);
0201 fprintf(fp,'%s\n\n',str);
0202 return;
0203 
0204 %==========================================================================
0205 function str = texify(str0)
0206 st1  = strfind(str0,'/*');
0207 en1  = strfind(str0,'*/');
0208 st = [];
0209 en = [];
0210 for i=1:numel(st1),
0211     en1  = en1(en1>st1(i));
0212     if ~isempty(en1),
0213         st  = [st st1(i)];
0214         en  = [en en1(1)];
0215         en1 = en1(2:end);
0216     end;
0217 end;
0218 
0219 str = [];
0220 pen = 1;
0221 for i=1:numel(st),
0222     str = [str clean_latex(str0(pen:st(i)-1)) str0(st(i)+2:en(i)-1)];
0223     pen = en(i)+2;
0224 end;
0225 str = [str clean_latex(str0(pen:numel(str0)))];
0226 return;
0227 
0228 %==========================================================================
0229 function str = clean_latex(str)
0230 str  = strrep(str,'$','\$');
0231 str  = strrep(str,'&','\&');
0232 str  = strrep(str,'^','\^');
0233 str  = strrep(str,'_','\_');
0234 str  = strrep(str,'#','\#');
0235 % str  = strrep(str,'\','$\\$');
0236 str  = strrep(str,'|','$|$');
0237 str  = strrep(str,'>','$>$');
0238 str  = strrep(str,'<','$<$');
0239 return;
0240 
0241 %==========================================================================
0242 function bibcstr = get_bib(bibdir)
0243 biblist = dir(fullfile(bibdir,'*.bib'));
0244 bibcstr={};
0245 for k = 1:numel(biblist)
0246     [p n e v] = spm_fileparts(biblist(k).name);
0247     bibcstr{k}  = fullfile(bibdir,n);
0248 end
0249 
0250 %==========================================================================
0251 function clean_latex_compile
0252 PRTdir = prt('dir');
0253 p = fullfile(PRTdir,'manual');
0254 [f, d] = spm_select('FPlist',p,'.*\.aux$');
0255 f = strvcat(f, spm_select('FPlist',p,'.*\.tex$'));
0256 f = strvcat(f, spm_select('FPlist',p,'^manual\..*$'));
0257 f(strcmp(cellstr(f),fullfile(PRTdir,'manual','prt_manual.tex')),:) = [];
0258 f(strcmp(cellstr(f),fullfile(PRTdir,'manual','prt_manual.pdf')),:) = [];
0259 for i=1:size(d,1)
0260     f = strvcat(f, spm_select('FPlist',deblank(d(i,:)),'.*\.aux$'));
0261 end
0262 f(strcmp(cellstr(f),filesep),:) = [];
0263 disp(f); pause
0264 for i=1:size(f,1)
0265     spm_unlink(deblank(f(i,:)));
0266 end

Generated on Mon 03-Sep-2012 18:07:18 by m2html © 2005