From cd483463b5ca483dcc07f47d9eb907ecd780d2c0 Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Thu, 28 Nov 2019 18:13:10 +0100 Subject: [PATCH] Create TOC closes #12 --- matlab/reporting/.gitignore | 1 + matlab/reporting/src/@page/write.m | 3 +++ matlab/reporting/src/@report/compile.m | 36 ++++++++++++++++++-------- matlab/reporting/src/@report/report.m | 2 ++ matlab/reporting/src/@report/write.m | 30 ++++++++++++++------- matlab/reporting/test/createReport.m | 4 +-- 6 files changed, 53 insertions(+), 23 deletions(-) diff --git a/matlab/reporting/.gitignore b/matlab/reporting/.gitignore index 949038fdc..ff8fcdabe 100644 --- a/matlab/reporting/.gitignore +++ b/matlab/reporting/.gitignore @@ -6,6 +6,7 @@ *.out *.tex *.csv +*.toc *synctex.gz .DS_Store test/tmpRepDir diff --git a/matlab/reporting/src/@page/write.m b/matlab/reporting/src/@page/write.m index a6ef0c293..61a4c3143 100644 --- a/matlab/reporting/src/@page/write.m +++ b/matlab/reporting/src/@page/write.m @@ -63,6 +63,9 @@ if ~isempty(o.latex) end fprintf(fid, '\\input{%s}', pagename); else + if ~isempty(o.title) + fprintf(fid, '\\addcontentsline{toc}{subsection}{%s}\n', o.title{1}); + end fprintf(fid, '\\begin{tabular}[t]{c}\n'); for i = 1:length(o.title) if isint(o.titleTruncate) diff --git a/matlab/reporting/src/@report/compile.m b/matlab/reporting/src/@report/compile.m index a2e62a7e6..c851e1323 100644 --- a/matlab/reporting/src/@report/compile.m +++ b/matlab/reporting/src/@report/compile.m @@ -93,23 +93,18 @@ end orig_dir = pwd; cd(o.directory) options = '-synctex=1 -halt-on-error'; -if opts.showOutput - if isoctave - system([opts.compiler ' ' options middle o.fileName]); - status = 0; - else - status = system([opts.compiler ' ' options middle o.fileName], '-echo'); - end -else - [status, ~] = system([opts.compiler ' -interaction=batchmode ' options middle o.fileName]); +[~, rfn] = fileparts(o.fileName); +if ~isempty(o.maketoc) + % TOC compilation requires two passes + compile_tex(o, orig_dir, opts, [options ' -draftmode'], middle); end -[~, rfn, ~] = fileparts(o.fileName); - if status ~= 0 cd(orig_dir) error(['@report.compile: There was an error in compiling ' rfn '.pdf.' ... ' ' opts.compiler ' returned the error code: ' num2str(status)]); end +compile_tex(o, orig_dir, opts, options, middle); + if o.showOutput || opts.showOutput fprintf('Done.\n\nYour compiled report is located here:\n %s.pdf\n\n\n', [pwd '/' rfn]) end @@ -118,3 +113,22 @@ if opts.showReport && ~isoctave end cd(orig_dir) end + +function compile_tex(o, orig_dir, opts, options, middle) +if opts.showOutput + if isoctave + system([opts.compiler ' ' options middle o.fileName]); + status = 0; + else + status = system([opts.compiler ' ' options middle o.fileName], '-echo'); + end +else + status = system([opts.compiler ' -interaction=batchmode ' options middle o.fileName]); +end + +if status ~= 0 + cd(orig_dir) + error(['@report.compile: There was an error in compiling ' rfn '.pdf.' ... + ' ' opts.compiler ' returned the error code: ' num2str(status)]); +end +end \ No newline at end of file diff --git a/matlab/reporting/src/@report/report.m b/matlab/reporting/src/@report/report.m index 71870e1e0..72a7dfd31 100644 --- a/matlab/reporting/src/@report/report.m +++ b/matlab/reporting/src/@report/report.m @@ -33,6 +33,7 @@ classdef report < handle showOutput = true % Print report creation progress to screen. Shows you the page number as it is created and as it is written. This is useful to see where a potential error occurs in report creation. Default: true. header = '' % The valid LATEX code to be included in the report before \begin{document}. Default: empty. reportDirName = 'tmpRepDir' % The name of the folder in which to store the component parts of the report (preamble, document, end). Default: tmpRepDir. + maketoc = false % Whether or not to make a table of contents end methods function o = report(varargin) @@ -99,6 +100,7 @@ classdef report < handle valid_orientation = {'portrait', 'landscape'}; assert(any(strcmp(o.orientation, valid_orientation)), ... ['@report.report: orientation must be one of ' addCommasToCellStr(valid_orientation)]); + assert(islogical(o.maketoc), '@report.report: maketock must be logical'); end o = addPage(o, varargin) o = addSection(o, varargin) diff --git a/matlab/reporting/src/@report/write.m b/matlab/reporting/src/@report/write.m index 3e0b10661..16e7808c2 100644 --- a/matlab/reporting/src/@report/write.m +++ b/matlab/reporting/src/@report/write.m @@ -53,7 +53,7 @@ if fid_preamble == -1 end fprintf(fid_preamble, '%% Report Object written %s\n', datestr(now)); -fprintf(fid_preamble, '\\documentclass[11pt]{article}\n'); +fprintf(fid_preamble, '\\documentclass[11pt,notitlepage]{article}\n'); fprintf(fid_preamble, '\\usepackage[%spaper,margin=%f%s', o.paper, o.margin, o.marginUnit); if strcmpi(o.orientation, 'landscape') @@ -83,6 +83,7 @@ if o.showDate fprintf(fid_preamble, '\\renewcommand{\\footrulewidth}{0.5pt}\n'); fprintf(fid_preamble, '\\rfoot{\\scriptsize\\reportdate\\today\\ -- \\currenttime}\n'); end +fprintf(fid_preamble, '\\rhead{}\n\\lhead{}\n'); % May not need these..... fprintf(fid_preamble, '\\renewcommand{\\textfraction}{0.05}\n'); @@ -94,11 +95,6 @@ fprintf(fid_preamble, '\\newlength\\sectionheight\n'); if ~isempty(o.header) fprintf(fid_preamble, '%s\n', o.header); end -fprintf(fid_preamble, '\\begin{document}\n'); -status = fclose(fid_preamble); -if status == -1 - error('@report.write: closing %s\n', preamble_file_name); -end %% Write body of document [fid_document, msg] = fopen([o.directory '/' document_file_name], 'w'); @@ -107,10 +103,16 @@ if fid_document == -1 end if ~isempty(o.title) - fprintf(fid_document, ['\\begin{titlepage}\n\\centering\n' ... - '\\vspace*{0.5cm}\n\\huge\\bfseries\n%s\n' ... - '\\vspace*{\\fill}\n\\end{titlepage}\n\\clearpage\n'], ... - o.title); + fprintf(fid_preamble, '\\newdateformat{reportdatelong}{\\THEDAY\\ \\monthname\\ \\THEYEAR}\n'); + fprintf(fid_document, '\\title{\\huge\\bfseries %s\\vspace{-1em}}\n\\author{}\n\\date{\\reportdatelong\\today}\n\\maketitle\n', o.title); +end + +if o.maketoc + fprintf(fid_document, '\\tableofcontents\n'); +end + +if ~isempty(o.title) || o.maketoc + fprintf(fid_document, '\\clearpage\n'); end if isunix && ~ismac @@ -137,6 +139,14 @@ for i = 1:length(o.pages) end fprintf(fid_document, '\\input{%s}\n', page_file_name); end + +%% Close preamble, document +fprintf(fid_preamble, '\\begin{document}\n'); +status = fclose(fid_preamble); +if status == -1 + error('@report.write: closing %s\n', preamble_file_name); +end + status = fclose(fid_document); if status == -1 error('@report.write: closing %s\n', document_file_name); diff --git a/matlab/reporting/test/createReport.m b/matlab/reporting/test/createReport.m index 2f165eb0d..486e963f4 100644 --- a/matlab/reporting/test/createReport.m +++ b/matlab/reporting/test/createReport.m @@ -34,8 +34,8 @@ longNames = {'Coca Cola', 'Kinder Bueno', 'Pizza', ... 'Vegetarianism Is Good', 'OS X', 'Dothraki'}; %% Begin Report -rep = report('directory', 'my/report/dir', 'title', 'Report Title'); - +rep = report('directory', 'my/report/dir', 'title', 'Report Title', ... + 'maketoc', true); %% Page 1: GDP rep.addPage('title', 'Jan1 vs Jan2', ...