compile.m 4.17 KB
Newer Older
1
function o = compile(o, varargin)
2
3
4
5
%function o = compile(o)
% Compile Report Object
%
% INPUTS
6
7
8
%   o            [report]  report object
%   varargin     [char]    allows user to change report compiler for a
%                          given run of compile.
9
10
11
12
13
14
15
%
% OUTPUTS
%   o     [report]  report object
%
% SPECIAL REQUIREMENTS
%   none

16
% Copyright (C) 2013-2019 Dynare Team
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
%
% 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/>.

33
opts.compiler = o.compiler;
34
opts.showReport = true;
35
opts.showOutput = o.showOutput;
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53

if nargin > 1
    if round((nargin-1)/2) ~= (nargin-1)/2
        error('@report.compile: options must be supplied in name/value pairs');
    end

    optNames = fieldnames(opts);

    % overwrite default values
    for pair = reshape(varargin, 2, [])
        ind = find(strcmpi(optNames, pair{1}));
        assert(isempty(ind) || length(ind) == 1);
        if ~isempty(ind)
            opts.(optNames{ind}) = pair{2};
        else
            error('@report.compile: %s is not a recognized option.', pair{1});
        end
    end
54
55
end

56
assert(ischar(opts.compiler), '@report.compile: compiler file must be a string');
57
assert(islogical(opts.showReport), '@report.compile: showReport must be either true or false');
58
assert(islogical(opts.showOutput), '@report.compile: showOutput must be either true or false');
59

Houtan Bastani's avatar
Houtan Bastani committed
60
if exist([o.directory '/' o.fileName], 'file') ~= 2
61
62
63
    o.write();
end

Houtan Bastani's avatar
Houtan Bastani committed
64
middle = ' ./';
65
if isempty(opts.compiler)
Houtan Bastani's avatar
Houtan Bastani committed
66
67
    status = 1;
    if ismac
Houtan Bastani's avatar
Houtan Bastani committed
68
        % Add most likely places for pdflatex to exist outside of default $PATH
Houtan Bastani's avatar
Houtan Bastani committed
69
        [status, opts.compiler] = ...
70
            system('PATH=$PATH:/usr/texbin:/usr/local/bin:/usr/local/sbin:/Library/TeX/texbin;which pdflatex');
Houtan Bastani's avatar
Houtan Bastani committed
71
72
    elseif ispc
        [status, opts.compiler] = system('findtexmf --file-type=exe pdflatex');
73
74
75
76
77
78
        if status == 1
            [status] = system('pdflatex.exe --version');
            if status == 0
                opts.compiler = 'pdflatex.exe';
            end
        end
79
        middle = ' ';
80
        opts.compiler = ['"' strtrim(opts.compiler) '"'];
Houtan Bastani's avatar
Houtan Bastani committed
81
82
    elseif isunix
        [status, opts.compiler] = system('which pdflatex');
83
84
85
    end
    assert(status == 0, ...
           '@report.compile: Could not find a tex compiler on your system');
86
87
    opts.compiler = strtrim(opts.compiler);
    o.compiler = opts.compiler;
Houtan Bastani's avatar
Houtan Bastani committed
88
89
90
    if opts.showOutput
        disp(['Using compiler: ' o.compiler]);
    end
91
end
92

93
94
orig_dir = pwd;
cd(o.directory)
Houtan Bastani's avatar
Houtan Bastani committed
95
options = '-synctex=1 -halt-on-error';
Houtan Bastani's avatar
Houtan Bastani committed
96
97
98
99
[~, rfn] = fileparts(o.fileName);
if ~isempty(o.maketoc)
    % TOC compilation requires two passes
    compile_tex(o, orig_dir, opts, [options ' -draftmode'], middle);
100
end
101
if status ~= 0
102
    cd(orig_dir)
103
    error(['@report.compile: There was an error in compiling ' rfn '.pdf.' ...
Stéphane Adjemian's avatar
Stéphane Adjemian committed
104
           '  ' opts.compiler ' returned the error code: ' num2str(status)]);
105
end
Houtan Bastani's avatar
Houtan Bastani committed
106
107
compile_tex(o, orig_dir, opts, options, middle);

108
if o.showOutput || opts.showOutput
Houtan Bastani's avatar
Houtan Bastani committed
109
    fprintf('Done.\n\nYour compiled report is located here:\n  %s.pdf\n\n\n', [pwd '/' rfn])
110
end
111
if opts.showReport && ~isoctave
112
    open([rfn '.pdf']);
113
end
114
cd(orig_dir)
115
end
Houtan Bastani's avatar
Houtan Bastani committed
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134

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