Commit f6e94801 authored by Houtan Bastani's avatar Houtan Bastani

add `directory` option to report class

parent f896ac82
......@@ -203,18 +203,15 @@ classdef graph < handle
end
o.seriesToUse = '';
o.data = '';
if ~exist(o.graphDirName, 'file')
mkdir(o.graphDirName);
end
end
end
methods (Access = ?section, Hidden = true)
o = addSeries(o, varargin)
write(o, fid, pg, sec, row, col)
write(o, fid, pg, sec, row, col, rep_dir)
end
methods (Access = private)
% Methods defined in separate files
lastIndex = end(o, k, n)
graphName = writeGraphFile(o, pg, sec, row, col)
graphName = writeGraphFile(o, pg, sec, row, col, rep_dir)
end
end
function write(o, fid, pg, sec, row, col)
%function write(o, fid, pg, sec, row, col)
function write(o, fid, pg, sec, row, col, rep_dir)
%function write(o, fid, pg, sec, row, col, rep_dir)
% Write a Graph object
%
% INPUTS
% o [graph] graph object
% fid [integer] file id
% pg [integer] this page number
% sec [integer] this section number
% row [integer] this row number
% col [integer] this col number
% o [graph] graph object
% fid [integer] file id
% pg [integer] this page number
% sec [integer] this section number
% row [integer] this row number
% col [integer] this col number
% rep_dir [string] directory containing report.tex
%
% OUTPUTS
% o [graph] graph object
......@@ -34,6 +35,6 @@ function write(o, fid, pg, sec, row, col)
% along with Dynare. If not, see <http://www.gnu.org/licenses/>.
assert(fid ~= -1);
graphName = writeGraphFile(o, pg, sec, row, col);
graphName = writeGraphFile(o, pg, sec, row, col, rep_dir);
fprintf(fid, '\\input{%s}', graphName);
end
\ No newline at end of file
function graphName = writeGraphFile(o, pg, sec, row, col)
%function graphName = writeGraphFile(o, pg, sec, row, col)
function graphName = writeGraphFile(o, pg, sec, row, col, rep_dir)
%function graphName = writeGraphFile(o, pg, sec, row, col, rep_dir)
% Write the tikz file that contains the graph
%
% INPUTS
% o [graph] graph object
% pg [integer] this page number
% sec [integer] this section number
% row [integer] this row number
% col [integer] this col number
% o [graph] graph object
% pg [integer] this page number
% sec [integer] this section number
% row [integer] this row number
% col [integer] this col number
% rep_dir [string] directory containing report.tex
%
% OUTPUTS
% graphName [string] name of graph written
......@@ -38,13 +39,16 @@ if ne < 1
return
end
if exist([rep_dir filesep o.graphDirName], 'file') ~= 7
mkdir([rep_dir filesep o.graphDirName])
end
if isempty(o.graphName)
graphName = sprintf('%s/graph_pg%d_sec%d_row%d_col%d.tex', o.graphDirName, pg, sec, row, col);
graphName = sprintf([o.graphDirName filesep 'graph_pg%d_sec%d_row%d_col%d.tex'], pg, sec, row, col);
else
graphName = [o.graphDirName '/' o.graphName];
graphName = [o.graphDirName filesep o.graphName];
end
[fid, msg] = fopen(graphName, 'w');
[fid, msg] = fopen([rep_dir filesep graphName], 'w');
if fid == -1
error(['@graph.writeGraphFile: ' msg]);
end
......
......@@ -117,7 +117,7 @@ classdef page < handle
o = addSection(o, varargin)
o = addVspace(o, varargin)
o = addTable(o, varargin)
write(o, fid, pg)
write(o, fid, pg, rep_dir)
lastIndex = end(o, k, n)
ns = numSections(p)
end
......
function write(o, fid, pg)
%function write(o, fid, pg)
function write(o, fid, pg, rep_dir)
%function write(o, fid, pg, rep_dir)
% Write a Page object
%
% INPUTS
% o [page] page object
% fid [integer] file id
% pg [integer] this page number
% rep_dir [string] directory containing report.tex
%
% OUTPUTS
% o [page] page object
......@@ -46,7 +47,11 @@ if ~isempty(o.latex)
if ~exist(o.pageDirName, 'dir')
mkdir(o.pageDirName)
end
pagename = [o.pageDirName '/page_' num2str(pg) '.tex'];
dir = [rep_dir filesep o.pageDirName];
if exist(dir, 'file') ~= 7
mkdir(dir)
end
pagename = [dir filesep 'page_' num2str(pg) '.tex'];
[fidp, msg] = fopen(pagename, 'w');
if fidp == -1
error(['@page.write: ' msg]);
......@@ -68,7 +73,7 @@ else
end
for i = 1:length(o.sections)
o.sections{i}.write(fid, pg, i);
o.sections{i}.write(fid, pg, i, rep_dir);
end
fprintf(fid, '\\end{tabular}\n');
end
......
......@@ -13,7 +13,7 @@ function o = compile(o, varargin)
% SPECIAL REQUIREMENTS
% none
% Copyright (C) 2013-2017 Dynare Team
% Copyright (C) 2013-2019 Dynare Team
%
% This file is part of Dynare.
%
......@@ -57,7 +57,7 @@ assert(ischar(opts.compiler), '@report.compile: compiler file must be a string')
assert(islogical(opts.showReport), '@report.compile: showReport must be either true or false');
assert(islogical(opts.showOutput), '@report.compile: showOutput must be either true or false');
if ~exist(o.fileName, 'file')
if exist([o.directory filesep o.fileName], 'file') ~= 2
o.write();
end
......@@ -90,6 +90,8 @@ if isempty(opts.compiler)
end
end
orig_dir = pwd;
cd(o.directory)
options = '-synctex=1 -halt-on-error';
if opts.showOutput
if isoctave
......@@ -104,6 +106,7 @@ 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
......@@ -115,6 +118,7 @@ if o.showOutput || opts.showOutput
disp('');
end
if opts.showReport && ~isoctave
open([pwd filesep rfn '.pdf']);
open([rfn '.pdf']);
end
cd(orig_dir)
end
......@@ -21,6 +21,7 @@ classdef report < handle
pages = {}
end
properties (SetAccess = private)
directory = '.' % Directory in which to write/compile the report. Default: '.'
title = '' % Report Title. Default: none.
orientation = 'portrait' % Paper orientation: Default: `portrait'.
paper = 'a4' % Paper size. Default: `a4'.
......@@ -77,6 +78,7 @@ classdef report < handle
end
% Check options provided by user
assert(ischar(o.directory), '@report.report: directory must be a string');
assert(ischar(o.title), '@report.report: title must be a string');
assert(ischar(o.fileName), '@report.report: fileName must be a string');
assert(ischar(o.compiler), '@report.report: compiler file must be a string');
......
......@@ -28,7 +28,10 @@ function o = write(o)
% You should have received a copy of the GNU General Public License
% along with Dynare. If not, see <http://www.gnu.org/licenses/>.
[fid, msg] = fopen(o.fileName, 'w');
if exist(o.directory, 'file') ~= 7
mkdir(o.directory)
end
[fid, msg] = fopen([o.directory filesep o.fileName], 'w');
if fid == -1
error(['@report.write: ' msg]);
end
......@@ -88,7 +91,7 @@ for i = 1:length(o.pages)
if o.showOutput
fprintf(1, 'Writing Page: %d\n', i);
end
o.pages{i}.write(fid, i);
o.pages{i}.write(fid, i, o.directory);
end
fprintf(fid, '\\end{document}\n');
......
......@@ -148,18 +148,15 @@ classdef report_table < handle
end
o.data = '';
o.seriesToUse = '';
if ~exist(o.tableDirName, 'file')
mkdir(o.tableDirName);
end
end
end
methods (Access = ?section, Hidden = true)
o = addData(o, varargin)
o = addSeries(o, varargin)
write(o, fid, pg, sec, row, col)
write(o, fid, pg, sec, row, col, rep_dir)
end
methods (Access = private)
o = writeTableFile(o, pg, sec, row, col)
o = writeTableFile(o, pg, sec, row, col, rep_dir)
end
end
......
function write(o, fid, pg, sec, row, col)
%function write(o, fid, pg, sec, row, col)
function write(o, fid, pg, sec, row, col, rep_dir)
%function write(o, fid, pg, sec, row, col, rep_dir)
% Write a Table object
%
% INPUTS
% o [table] table object
% fid [integer] file id
% pg [integer] this page number
% sec [integer] this section number
% row [integer] this row number
% col [integer] this col number
% o [table] table object
% fid [integer] file id
% pg [integer] this page number
% sec [integer] this section number
% row [integer] this row number
% col [integer] this col number
% rep_dir [string] directory containing report.tex
%
% OUTPUTS
% o [table] table object
......@@ -34,6 +35,6 @@ function write(o, fid, pg, sec, row, col)
% along with Dynare. If not, see <http://www.gnu.org/licenses/>.
assert(fid ~= -1);
tableName = writeTableFile(o, pg, sec, row, col);
tableName = writeTableFile(o, pg, sec, row, col, rep_dir);
fprintf(fid, '\\input{%s}', tableName);
end
function tableName = writeTableFile(o, pg, sec, row, col)
%function tableName = writeTableFile(o, pg, sec, row, col)
function tableName = writeTableFile(o, pg, sec, row, col, rep_dir)
%function tableName = writeTableFile(o, pg, sec, row, col, rep_dir)
% Write a Report_Table object
%
% INPUTS
% o [report_table] report_table object
% pg [integer] this page number
% sec [integer] this section number
% row [integer] this row number
% col [integer] this col number
% o [report_table] report_table object
% pg [integer] this page number
% sec [integer] this section number
% row [integer] this row number
% col [integer] this col number
% rep_dir [string] directory containing report.tex
%
% OUTPUTS
% tableName [string] name of table written
......@@ -39,13 +40,16 @@ if ne == 0 && ~is_data_table
return
end
if exist([rep_dir filesep o.tableDirName], 'file') ~= 7
mkdir([rep_dir filesep o.tableDirName])
end
if isempty(o.tableName)
tableName = sprintf('%s/table_pg%d_sec%d_row%d_col%d.tex', o.tableDirName, pg, sec, row, col);
tableName = sprintf([o.tableDirName filesep 'table_pg%d_sec%d_row%d_col%d.tex'], pg, sec, row, col);
else
tableName = [o.tableDirName '/' o.tableName];
tableName = [o.tableDirName filesep o.tableName];
end
[fid, msg] = fopen(tableName, 'w');
[fid, msg] = fopen([rep_dir filesep tableName], 'w');
if fid == -1
error(['@report_table.writeTableFile: ' msg]);
end
......
......@@ -81,6 +81,6 @@ classdef section < handle
o = addVspace(o, varargin)
lastIndex = end(o, k, n)
n = numElements(o)
write(o, fid, pg, sec)
write(o, fid, pg, sec, rep_dir)
end
end
function write(o, fid, pg, sec)
%function write(o, fid, pg, sec)
function write(o, fid, pg, sec, rep_dir)
%function write(o, fid, pg, sec, rep_dir)
% Write Section object
%
% INPUTS
......@@ -7,6 +7,7 @@ function write(o, fid, pg, sec)
% fid [integer] file id
% pg [integer] this page number
% sec [integer] this section number
% rep_dir [string] directory containing report.tex
%
% OUTPUTS
% o [section] section object
......@@ -77,7 +78,7 @@ for i=1:ne
if isa(o.elements{i}, 'paragraph')
o.elements{i}.write(fid);
else
o.elements{i}.write(fid, pg, sec, row, col);
o.elements{i}.write(fid, pg, sec, row, col, rep_dir);
end
if col ~= o.cols
fprintf(fid, ' & ');
......
......@@ -34,7 +34,7 @@ longNames = {'Coca Cola', 'Kinder Bueno', 'Pizza', ...
'Vegetarianism Is Good', 'OS X', 'Dothraki'};
%% Begin Report
rep = report();
rep = report('directory', 'my/report/dir');
%% Page 1: GDP
......@@ -107,7 +107,7 @@ rep.addVspace('number', 2);
% Table 2
rep.addTable('title', 'Output Gap', 'range', larange, ...
'vlineAfter', dates('2011y'));
'vlineAfter', dates('2011y'), 'tableDirName', 'page4table2');
db_a = db_a.tex_rename('Y_WORLD', 'World');
rep.addSeries('data', db_a{'Y_WORLD'});
delta = db_a{'Y_WORLD'}-dc_a{'Y_WORLD'};
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment