write.m 5.93 KB
Newer Older
Houtan Bastani's avatar
Houtan Bastani committed
1
2
function o = write(o)
%function o = write(o)
Houtan Bastani's avatar
Houtan Bastani committed
3
4
5
% Write Report object
%
% INPUTS
Houtan Bastani's avatar
Houtan Bastani committed
6
%   o     [report]  report object
Houtan Bastani's avatar
Houtan Bastani committed
7
8
%
% OUTPUTS
Houtan Bastani's avatar
Houtan Bastani committed
9
%   o     [report]  report object
Houtan Bastani's avatar
Houtan Bastani committed
10
11
12
13
%
% SPECIAL REQUIREMENTS
%   none

14
% Copyright (C) 2013-2019 Dynare Team
Houtan Bastani's avatar
Houtan Bastani committed
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
%
% This file is part of Dynare.
%
% Dynare is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% Dynare is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with Dynare.  If not, see <http://www.gnu.org/licenses/>.

31
32
if exist(o.directory, 'dir') ~= 7
    mkdir(o.directory);
33
end
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
if exist([o.directory '/' o.reportDirName], 'dir') ~= 7
    mkdir([o.directory '/' o.reportDirName]);
end
idx = strfind(o.fileName, '.tex');
if isempty(idx) || ~strcmp(o.fileName(idx:end), '.tex')
    preamble_file_name = [o.reportDirName '/' o.fileName '_preamble.tex'];
    document_file_name = [o.reportDirName '/' o.fileName '_document.tex'];
    page_file_name_root = [o.reportDirName '/' o.fileName '_pg'];
    o.fileName = [o.fileName '.tex'];
else
    preamble_file_name = [o.reportDirName '/' o.fileName(1:idx-1) '_preamble.tex'];
    document_file_name = [o.reportDirName '/' o.fileName(1:idx-1) '_document.tex'];
    page_file_name_root = [o.reportDirName '/' o.fileName(1:idx-1) '_pg'];
end

%% Create preamble
[fid_preamble, msg] = fopen([o.directory '/' preamble_file_name], 'w');
if fid_preamble == -1
Houtan Bastani's avatar
Houtan Bastani committed
52
    error(['@report.write: ' msg]);
Houtan Bastani's avatar
Houtan Bastani committed
53
54
end

55
fprintf(fid_preamble, '%% Report Object written %s\n', datestr(now));
Houtan Bastani's avatar
Houtan Bastani committed
56
fprintf(fid_preamble, '\\documentclass[11pt,notitlepage]{article}\n');
Houtan Bastani's avatar
Houtan Bastani committed
57

58
fprintf(fid_preamble, '\\usepackage[%spaper,margin=%f%s', o.paper, o.margin, o.marginUnit);
Houtan Bastani's avatar
Houtan Bastani committed
59
if strcmpi(o.orientation, 'landscape')
60
    fprintf(fid_preamble, ',landscape');
Houtan Bastani's avatar
Houtan Bastani committed
61
end
62
63
64
65
fprintf(fid_preamble, ']{geometry}\n');
fprintf(fid_preamble, '\\usepackage{pdflscape, booktabs, pgfplots, colortbl, adjustbox, multicol}\n');
fprintf(fid_preamble, '\\pgfplotsset{compat=1.5.1}');
fprintf(fid_preamble, ['\\makeatletter\n' ...
Houtan Bastani's avatar
Houtan Bastani committed
66
67
              '\\def\\blfootnote{\\gdef\\@thefnmark{}\\@footnotetext}\n' ...
              '\\makeatother\n']);
68

69
if isoctave && isempty(regexpi(computer, '.*apple.*', 'once'))
70
71
    fprintf(fid_preamble, '\\usepackage[T1]{fontenc}\n');
    fprintf(fid_preamble, '\\usepackage[utf8x]{inputenc}\n');
72
end
73
if ispc || ismac
74
    fprintf(fid_preamble, '\\usepgfplotslibrary{fillbetween}\n');
75
end
76
77
fprintf(fid_preamble, '\\definecolor{LightCyan}{rgb}{0.88,1,1}\n');
fprintf(fid_preamble, '\\definecolor{Gray}{gray}{0.9}\n');
78
if o.showDate
79
80
81
82
83
84
    fprintf(fid_preamble, '\\usepackage{fancyhdr, datetime}\n');
    fprintf(fid_preamble, '\\newdateformat{reportdate}{\\THEDAY\\ \\shortmonthname\\ \\THEYEAR}\n');
    fprintf(fid_preamble, '\\pagestyle{fancy}\n');
    fprintf(fid_preamble, '\\renewcommand{\\headrulewidth}{0pt}\n');
    fprintf(fid_preamble, '\\renewcommand{\\footrulewidth}{0.5pt}\n');
    fprintf(fid_preamble, '\\rfoot{\\scriptsize\\reportdate\\today\\ -- \\currenttime}\n');
Houtan Bastani's avatar
Houtan Bastani committed
85
end
Houtan Bastani's avatar
Houtan Bastani committed
86
fprintf(fid_preamble, '\\rhead{}\n\\lhead{}\n');
Houtan Bastani's avatar
Houtan Bastani committed
87
88

% May not need these.....
89
90
91
92
93
94
fprintf(fid_preamble, '\\renewcommand{\\textfraction}{0.05}\n');
fprintf(fid_preamble, '\\renewcommand{\\topfraction}{0.8}\n');
fprintf(fid_preamble, '\\renewcommand{\\bottomfraction}{0.8}\n');
fprintf(fid_preamble, '\\setlength{\\parindent}{0in}\n');
fprintf(fid_preamble, '\\setlength{\\tabcolsep}{1em}\n');
fprintf(fid_preamble, '\\newlength\\sectionheight\n');
95
if ~isempty(o.header)
96
97
98
99
100
101
102
    fprintf(fid_preamble, '%s\n', o.header);
end

%% Write body of document
[fid_document, msg] = fopen([o.directory '/' document_file_name], 'w');
if fid_document == -1
    error(['@report.write: ' msg]);
103
end
104
105

if ~isempty(o.title)
Houtan Bastani's avatar
Houtan Bastani committed
106
107
108
109
110
111
112
113
114
115
    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');
116
117
end

118
if isunix && ~ismac
119
120
121
    fprintf(fid_document, '\\pgfdeclarelayer{axis background}\n');
    fprintf(fid_document, '\\pgfdeclarelayer{axis lines}\n');
    fprintf(fid_document, '\\pgfsetlayers{axis background,axis lines,main}\n');
122
end
123
124
fprintf(fid_document, '\\pgfplotsset{tick scale binop={\\times},\ntrim axis left}\n');
fprintf(fid_document, '\\centering\n');
Houtan Bastani's avatar
Houtan Bastani committed
125

126
for i = 1:length(o.pages)
127
128
129
    if o.showOutput
        fprintf(1, 'Writing Page: %d\n', i);
    end
130
131
132
133
134
135
136
137
138
139
140
141
    page_file_name = [page_file_name_root num2str(i) '.tex'];
    [fid_pg, msg] = fopen([o.directory '/' page_file_name], 'w');
    if fid_pg == -1
        error(['@report.write: ' msg]);
    end
    o.pages{i}.write(fid_pg, i, o.directory);
    status = fclose(fid_pg);
    if status == -1
        error('@report.write: closing %s\n', o.fileName);
    end
    fprintf(fid_document, '\\input{%s}\n', page_file_name);
end
Houtan Bastani's avatar
Houtan Bastani committed
142
143
144
145
146
147
148
149

%% 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

150
151
152
status = fclose(fid_document);
if status == -1
    error('@report.write: closing %s\n', document_file_name);
Houtan Bastani's avatar
Houtan Bastani committed
153
end
Houtan Bastani's avatar
Houtan Bastani committed
154

155
156
157
158
159
160
161
%% Create report comprised of preamble and document
[fid, msg] = fopen([o.directory '/' o.fileName], 'w');
if fid == -1
    error(['@report.write: ' msg]);
end
fprintf(fid, '\\input{%s}\n\\input{%s}\n', ...
    preamble_file_name, document_file_name);
Houtan Bastani's avatar
Houtan Bastani committed
162
163
164
fprintf(fid, '\\end{document}\n');
status = fclose(fid);
if status == -1
165
    error('@report.write: closing %s\n', o.fileName);
Houtan Bastani's avatar
Houtan Bastani committed
166
end
167

168
169
170
if o.showOutput
    disp('Finished Writing Report!');
end
171
end