compile.m 3.61 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-2015 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

60
if ~exist(o.fileName, 'file')
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
70
71
72
        [status, opts.compiler] = ...
               system('PATH=$PATH:/usr/texbin:/usr/local/bin:/usr/local/sbin;which pdflatex');
    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

Houtan Bastani's avatar
Houtan Bastani committed
93
94
95
96
97
98
options = '-synctex=1 -halt-on-error';
if isoctave
    echo = 1;
else
    echo = '-echo';
end
99
100
101
if opts.showOutput
    status = system([opts.compiler ' ' options middle o.fileName], echo);
else
102
    [status, junk] = system([opts.compiler ' -interaction=batchmode ' options middle o.fileName]);
103
end
104
[junk, rfn, junk] = fileparts(o.fileName);
105
106
107

if status ~= 0
    error(['@report.compile: There was an error in compiling ' rfn '.pdf.' ...
ferhat's avatar
ferhat committed
108
          '  ' opts.compiler ' returned the error code: ' num2str(status)]);
109
end
110
if o.showOutput || opts.showOutput
111
    fprintf(1, ['Done.\nYour compiled report is located here:\n     ' pwd filesep rfn '.pdf\n\n']);
112
end
113
if opts.showReport && ~isoctave
114
115
    open([pwd filesep rfn '.pdf']);
end
116
end