1function dyn_latex_table(M_, options_, title, LaTeXtitle, headers, labels, values, label_width, val_width, val_precis, optional_header)
2%function dyn_latex_table(M_, options_, title, LaTeXtitle, headers, labels, values, label_width, val_width, val_precis, optional_header)
3
4% Copyright (C) 2015-2020 Dynare Team
5%
6% This file is part of Dynare.
7%
8% Dynare is free software: you can redistribute it and/or modify
9% it under the terms of the GNU General Public License as published by
10% the Free Software Foundation, either version 3 of the License, or
11% (at your option) any later version.
12%
13% Dynare is distributed in the hope that it will be useful,
14% but WITHOUT ANY WARRANTY; without even the implied warranty of
15% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16% GNU General Public License for more details.
17%
18% You should have received a copy of the GNU General Public License
19% along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
20
21if options_.noprint
22    return
23end
24
25if length(headers) < 2
26    error('headers length must be >= 2')
27end
28
29OutputDirectoryName = CheckPath('Output',M_.dname);
30
31% Set width of label column
32if isempty(label_width)
33    label_width = cellofchararraymaxlength(vertcat(headers{1}, labels))+2;
34else
35    label_width = max(cellofchararraymaxlength(vertcat(headers{1}, labels))+2, label_width);
36end
37label_format_leftbound = sprintf('$%%-%ds$', label_width);
38
39% Set width of other columns
40if all(~isfinite(values))
41    values_length = 4;
42else
43    values_length = max(ceil(max(max(log10(abs(values(isfinite(values))))))),1)+val_precis+1;
44end
45if any(values < 0) %add one character for minus sign
46    values_length = values_length+1;
47end
48headers_length = cellofchararraymaxlength(headers(2:end));
49if isempty(val_width)
50    val_width = max(headers_length, values_length)+4;
51else
52    val_width = max(max(headers_length, values_length)+4, val_width);
53end
54value_format = sprintf('%%%d.%df', val_width, val_precis);
55header_string_format = sprintf('$%%%ds$', val_width);
56
57% Create and print header string
58header_string = sprintf(label_format_leftbound, strrep(headers{1}, '\', '\\'));
59header_code_string = ['l' repmat('c', 1, length(headers)-1)];
60for i=2:length(headers)
61    header_string = [header_string '\t & \t ' sprintf(header_string_format, strrep(headers{i},'\','\\'))];
62end
63header_string = [header_string '\\\\\n'];
64
65filename = [OutputDirectoryName '/' M_.fname '_' LaTeXtitle '.tex'];
66fidTeX = fopen(filename,'w');
67
68stack = dbstack;
69fprintf(fidTeX, ['%% ' datestr(now,0) ', created by ' stack(2).file]);
70fprintf(fidTeX, ' \n');
71fprintf(fidTeX, ' \n');
72fprintf(fidTeX, '\\begin{center}\n');
73fprintf(fidTeX, '\\begin{longtable}{%s} \n', header_code_string);
74fprintf(fidTeX, ['\\caption{',title,'}\\\\\n ']);
75
76fprintf(fidTeX, ['\\label{Table:',LaTeXtitle,'}\\\\\n']);
77fprintf(fidTeX, '\\toprule \n');
78if nargin==11
79    for ii = 1:length(optional_header)
80        fprintf(fidTeX,'%s\n',optional_header{ii});
81    end
82end
83fprintf(fidTeX, header_string);
84fprintf(fidTeX, '\\midrule \\endfirsthead \n');
85fprintf(fidTeX, '\\caption{(continued)}\\\\\n ');
86fprintf(fidTeX, '\\toprule \\\\ \n');
87if nargin==11
88    for ii = 1:length(optional_header)
89        fprintf(fidTeX, '%s\n', optional_header{ii});
90    end
91end
92fprintf(fidTeX, header_string);
93fprintf(fidTeX, '\\midrule \\endhead \n');
94fprintf(fidTeX, ['\\midrule \\multicolumn{',num2str(size(headers,1)),'}{r}{(Continued on next page)} \\\\ \\bottomrule \\endfoot \n']);
95fprintf(fidTeX, '\\bottomrule \\endlastfoot \n');
96for i = 1:size(values,1)
97    fprintf(fidTeX, label_format_leftbound, labels{i});
98    fprintf(fidTeX, ['\t & \t' value_format], values(i,:));
99    fprintf(fidTeX, ' \\\\ \n');
100end
101
102fprintf(fidTeX, '\\end{longtable}\n ');
103fprintf(fidTeX, '\\end{center}\n');
104fprintf(fidTeX, '%% End of TeX file.\n');
105fclose(fidTeX);