Home > . > prt_latex.m

prt_latex

PURPOSE ^

SYNOPSIS ^

function prt_latex(opt)

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.

 FORMAT prt_latex(opt)

 INPUT
   opt:  option structure
     .tex_cfg : turn the config files help into a tex file (1), or not (0)
     .tex_fct : turn the functions help into a tex file (1), or not (0)

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

Generated on Tue 10-Feb-2015 18:16:33 by m2html © 2005