Home > . > prt_struct2latex.m

prt_struct2latex

PURPOSE ^

Function that takes in a structure S and writes down the latex code

SYNOPSIS ^

function prt_struct2latex(S)

DESCRIPTION ^

 Function that takes in a structure S and writes down the latex code
 describing the whole structure and substructures recursively.
 The routine specifically generates the 'adv_PRTstruct.tex' file that is
 included, in the prt_manual.

 Bits of the code and copied/inspired by spm_latex.m from 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_struct2latex(S)
0002 % Function that takes in a structure S and writes down the latex code
0003 % describing the whole structure and substructures recursively.
0004 % The routine specifically generates the 'adv_PRTstruct.tex' file that is
0005 % included, in the prt_manual.
0006 %
0007 % Bits of the code and copied/inspired by spm_latex.m from the SPM8
0008 % distribution: http://www.fil.ion.ucl.ac.uk/spm
0009 %_______________________________________________________________________
0010 % Copyright (C) 2011 Machine Learning & Neuroimaging Laboratory
0011 
0012 % Written by Christophe Phillips
0013 % $Id: prt_struct2latex.m 395 2011-11-17 22:34:59Z cphillip $
0014 
0015 if nargin<1,
0016     f = spm_select(1,'^PRT\.mat','Select a PRT.mat file');
0017     tmp = load(f);
0018     S = tmp.PRT;
0019 end
0020 
0021 fp = fopen(fullfile(prt('dir'),'manual','adv_PRTstruct.tex'),'w');
0022 if fp==-1, sts = false; return; end;
0023 
0024 %% Heading part
0025 % fprintf(fp,'%% $Id: prt_struct2latex.m 395 2011-11-17 22:34:59Z cphillip $\n\n');
0026 fprintf(fp,'\\chapter{%s}\n\\label{sec:%s}\n\\minitoc\n\n',...
0027     texify('PRT structure'),'PRTstruct');
0028 fprintf(fp,'This is how the main {\\tt PRT} structure is organised.\n\n');
0029 
0030 %% Deal with structure
0031 fprintf(fp,'{\\tt PRT}\n');
0032 h_skip = .2; % horizontal skip increment
0033 
0034 i_nest = 1;
0035 struct2tex(fp,S,h_skip,i_nest)
0036 
0037 fclose(fp);
0038 
0039 return
0040 
0041 %==========================================================================
0042 function struct2tex(fp,S,h_skip,i_nest)
0043 
0044 if nargin<3, h_skip = .5; end
0045 
0046 fieldn = fieldnames(S);
0047 if i_nest<7
0048     % Begin list
0049     beg_txt = sprintf(['\\begin{list}{$\\bullet$}\n' ...
0050         '\t{\\setlength{\\labelsep}{.2cm}' ...
0051         '\\setlength{\\itemindent}{0cm}' ...
0052         '\\setlength{\\leftmargin}{%2.1fcm}}\n'],h_skip);
0053     fprintf(fp,'%s',beg_txt);
0054     
0055     % List of fields
0056     for ii=1:numel(fieldn)
0057         fprintf(fp,'\\item %s',texify(fieldn{ii}));
0058         if numel(S)>1
0059             fprintf(fp,'()\n');
0060         else
0061             fprintf(fp,'\n');
0062         end
0063         if isstruct(S(1).(fieldn{ii})) & ~isempty(S(1).(fieldn{ii}))
0064             struct2tex(fp,S(1).(fieldn{ii}),h_skip+.5,i_nest+1);
0065         end
0066     end
0067     
0068     % End list
0069     end_txt = '\end{list}';
0070     fprintf(fp,'%s\n',end_txt);
0071 else
0072     % Too many nested lists for Latex to handle!
0073     
0074     % TODO: deal with this case!
0075 end
0076 return
0077 
0078 %==========================================================================
0079 function str = texify(str0)
0080 st1  = strfind(str0,'/*');
0081 en1  = strfind(str0,'*/');
0082 st = [];
0083 en = [];
0084 for i=1:numel(st1),
0085     en1  = en1(en1>st1(i));
0086     if ~isempty(en1),
0087         st  = [st st1(i)];
0088         en  = [en en1(1)];
0089         en1 = en1(2:end);
0090     end;
0091 end;
0092 
0093 str = [];
0094 pen = 1;
0095 for i=1:numel(st),
0096     str = [str clean_latex(str0(pen:st(i)-1)) str0(st(i)+2:en(i)-1)];
0097     pen = en(i)+2;
0098 end;
0099 str = [str clean_latex(str0(pen:numel(str0)))];
0100 return;
0101 
0102 %==========================================================================
0103 function str = clean_latex(str)
0104 str  = strrep(str,'$','\$');
0105 str  = strrep(str,'&','\&');
0106 str  = strrep(str,'^','\^');
0107 str  = strrep(str,'_','\_');
0108 str  = strrep(str,'#','\#');
0109 %str  = strrep(str,'\','$\\$');
0110 str  = strrep(str,'|','$|$');
0111 str  = strrep(str,'>','$>$');
0112 str  = strrep(str,'<','$<$');
0113 return;
0114 
0115 %==========================================================================

Generated on Sun 20-May-2012 13:24:48 by m2html © 2005