dynare/matlab/reporting/@report/compile.m

137 lines
4.3 KiB
Matlab
Raw Normal View History

function o = compile(o, varargin)
2013-04-10 18:08:14 +02:00
%function o = compile(o)
% Compile Report Object
%
% INPUTS
% o [report] report object
% varargin [char] allows user to change report compiler for a
% given run of compile.
2013-04-10 18:08:14 +02:00
%
% OUTPUTS
% o [report] report object
%
% SPECIAL REQUIREMENTS
% none
% Copyright © 2013-2019 Dynare Team
2013-04-10 18:08:14 +02:00
%
% 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 <https://www.gnu.org/licenses/>.
2013-04-10 18:08:14 +02:00
opts.compiler = o.compiler;
opts.showReport = true;
opts.showOutput = o.showOutput;
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
end
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.directory '/' o.fileName], 'file') ~= 2
2013-04-10 18:08:14 +02:00
o.write();
end
2013-06-24 12:12:26 +02:00
middle = ' ./';
if isempty(opts.compiler)
2015-06-11 11:43:03 +02:00
status = 1;
if ismac
2013-05-15 13:04:12 +02:00
% Add most likely places for pdflatex to exist outside of default $PATH
2015-06-11 11:43:03 +02:00
[status, opts.compiler] = ...
2017-06-16 16:40:01 +02:00
system('PATH=$PATH:/usr/texbin:/usr/local/bin:/usr/local/sbin:/Library/TeX/texbin;which pdflatex');
2015-06-11 11:43:03 +02:00
elseif ispc
[status, opts.compiler] = system('findtexmf --file-type=exe pdflatex');
if status == 1
[status] = system('pdflatex.exe --version');
if status == 0
opts.compiler = 'pdflatex.exe';
end
end
middle = ' ';
opts.compiler = ['"' strtrim(opts.compiler) '"'];
2015-06-11 11:43:03 +02:00
elseif isunix
% “which” has been deprecated in Debian Bookworm; rather use “command -v”
% which should work with any reasonably recent GNU/Linux shell
[status, opts.compiler] = system('command -v pdflatex');
2013-04-10 18:08:14 +02:00
end
assert(status == 0, ...
'@report.compile: Could not find a tex compiler on your system');
opts.compiler = strtrim(opts.compiler);
o.compiler = opts.compiler;
2015-06-11 11:43:03 +02:00
if opts.showOutput
disp(['Using compiler: ' o.compiler]);
end
2013-04-10 18:08:14 +02:00
end
2019-09-02 16:25:13 +02:00
orig_dir = pwd;
cd(o.directory)
2015-06-11 11:43:03 +02:00
options = '-synctex=1 -halt-on-error';
2019-11-28 18:13:10 +01:00
[~, rfn] = fileparts(o.fileName);
if ~isempty(o.maketoc)
% TOC compilation requires two passes
compile_tex(o, orig_dir, opts, [options ' -draftmode'], middle, rfn);
end
2013-04-10 18:08:14 +02:00
if status ~= 0
2019-09-02 16:25:13 +02:00
cd(orig_dir)
2013-04-10 18:08:14 +02:00
error(['@report.compile: There was an error in compiling ' rfn '.pdf.' ...
2017-05-19 00:04:08 +02:00
' ' opts.compiler ' returned the error code: ' num2str(status)]);
2013-04-10 18:08:14 +02:00
end
compile_tex(o, orig_dir, opts, options, middle, rfn);
2019-11-28 18:13:10 +01:00
if o.showOutput || opts.showOutput
fprintf('Done.\n\nYour compiled report is located here:\n %s.pdf\n\n\n', [pwd '/' rfn])
end
if opts.showReport && ~isoctave
2019-09-02 16:25:13 +02:00
open([rfn '.pdf']);
end
2019-09-02 16:25:13 +02:00
cd(orig_dir)
end
2019-11-28 18:13:10 +01:00
function compile_tex(o, orig_dir, opts, options, middle, rfn)
2019-11-28 18:13:10 +01:00
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