1 | :- module(pltables_export_latex, [ | |
2 | latex_header/2, | |
3 | latex_footer/2, | |
4 | latex_table_header/2, | |
5 | latex_table_footer/2, | |
6 | latex_table_rows/2 | |
7 | ]). | |
8 | ||
9 | :- use_module(pltables_export_tools). | |
10 | :- use_module(pltables). | |
11 | ||
12 | :- use_module(probsrc(module_information)). | |
13 | ||
14 | :- module_info(group,pltables). | |
15 | :- module_info(description,'This module is used to export coverage tables to latex.'). | |
16 | ||
17 | % ---- | |
18 | % table headers | |
19 | % ---- | |
20 | latex_header(S, TableName) :- | |
21 | pltable(TableName, Attributes), | |
22 | write(S,'\\documentclass[a4paper,10pt]{article}\n'), | |
23 | write(S,'\\usepackage{a4wide}\n\\usepackage{longtable}\n\\usepackage{xcolor}\n\\begin{document}\n'), | |
24 | ( member(headline(XHU), Attributes) -> | |
25 | latex_escape(XHU,XH), | |
26 | format(S, '\\textbf{~w} \\\\\\\\\n', [XH]) | |
27 | ; true | |
28 | ). | |
29 | ||
30 | % ---- | |
31 | % table footers | |
32 | % ---- | |
33 | latex_footer(S, _TableName) :- | |
34 | write(S,'\\end{document}'). | |
35 | ||
36 | % ---- | |
37 | % table header / footer | |
38 | % ---- | |
39 | latex_table_header(S, TableName) :- | |
40 | pltable(TableName, TableAttributes), | |
41 | pltables:pltable_header(TableName, Columns, Attributes), | |
42 | ( | |
43 | member(description(XDU, DescAttrs), TableAttributes) | |
44 | -> latex_escape(XDU,XD), | |
45 | (member(bold, DescAttrs) -> write(S, '\\textbf{') ; true), | |
46 | format(S, '~w\n', [XD]), | |
47 | (member(bold, DescAttrs) -> write(S, '}') ; true) | |
48 | ; true | |
49 | ), | |
50 | write(S, '\\begin{longtable}'), | |
51 | write(S, '{|'), | |
52 | write_latex_table_header1(S, Attributes), | |
53 | write(S, '}\n\\hline\n'), | |
54 | write_latex_table_header2(S, Columns, Attributes). | |
55 | ||
56 | write_latex_table_header1(_S, []). | |
57 | write_latex_table_header1(S, [Attributes|TAttributes]) :- | |
58 | (member(align(left), Attributes) -> write(S, 'l|') ; true), | |
59 | (member(align(right), Attributes) -> write(S, 'r|') ; true), | |
60 | (member(align(center), Attributes) -> write(S, 'c|') ; true), | |
61 | (\+member(align(_), Attributes) -> write(S, 'l|') ; true), | |
62 | write_latex_table_header1(S, TAttributes). | |
63 | ||
64 | write_latex_table_header2(S, [Column], _Attributes) :- | |
65 | latex_escape(Column, EscapedColumn), | |
66 | format(S, '~w \\\\\n\\hline\n', [EscapedColumn]). | |
67 | write_latex_table_header2(S, [Column | Columns], Attributes) :- | |
68 | latex_escape(Column, EscapedColumn), | |
69 | format(S, '~w & ', [EscapedColumn]), | |
70 | write_latex_table_header2(S, Columns, Attributes). | |
71 | ||
72 | latex_table_footer(S, _TableName) :- | |
73 | write(S,'\\end{longtable}\n'). | |
74 | ||
75 | ||
76 | % ---- | |
77 | % table rows | |
78 | % ---- | |
79 | latex_table_rows(S, TableName) :- | |
80 | findall(Entries, pltables:pltable_row(TableName, Entries, Attributes), ListOfEntries), | |
81 | findall(Attributes, pltables:pltable_row(TableName, Entries, Attributes), ListOfAttributes), | |
82 | pltables:pltable(TableName, TableAttributes), | |
83 | pltables:pltable_header(TableName, _, ColumnsAttributes), | |
84 | write_latex_table_rows(S, ListOfEntries, ListOfAttributes, TableAttributes, ColumnsAttributes). | |
85 | ||
86 | write_latex_table_rows(_S, [], [], _TableAttributes, _ColumnsAttributes). | |
87 | write_latex_table_rows(S, [Entries | MoreEntries], [RowAttributes | MoreAttributes], TableAttributes, ColumnsAttributes) :- | |
88 | write_latex_table_row(S, Entries, RowAttributes, TableAttributes, ColumnsAttributes), | |
89 | write(S, ' \\\\\n\\hline\n'), | |
90 | write_latex_table_rows(S, MoreEntries, MoreAttributes, TableAttributes, ColumnsAttributes). | |
91 | ||
92 | write_latex_table_row(S, [Entry], RowAttributes, TableAttributes, [ColumnAttribute]) :- | |
93 | merge_attributes(TableAttributes, ColumnAttribute, TmpAttributes), | |
94 | merge_attributes(TmpAttributes, RowAttributes, MergedAttributes), | |
95 | (member(color(C), MergedAttributes) -> format(S, '\\textcolor[HTML]{~w}{', [C]) ; true), | |
96 | (member(bold, MergedAttributes) -> write(S, '\\textbf{') ; true), | |
97 | latex_escape(Entry, EscapedEntry), | |
98 | format(S, '~w', [EscapedEntry]), | |
99 | (member(bold, MergedAttributes) -> write(S, '}') ; true), | |
100 | (member(color(C), MergedAttributes) -> write(S, '}') ; true). | |
101 | write_latex_table_row(S, [Entry | Entries], RowAttributes, TableAttributes, [ColumnAttribute | ColumnsAttributes]) :- | |
102 | merge_attributes(TableAttributes, ColumnAttribute, TmpAttributes), | |
103 | merge_attributes(TmpAttributes, RowAttributes, MergedAttributes), | |
104 | (member(color(C), MergedAttributes) -> format(S, '\\textcolor[HTML]{~w}{', [C]) ; true), | |
105 | (member(bold, MergedAttributes) -> write(S, '\\textbf{') ; true), | |
106 | latex_escape(Entry, EscapedEntry), | |
107 | format(S, '~w', [EscapedEntry]), | |
108 | (member(bold, MergedAttributes) -> write(S, '}') ; true), | |
109 | (member(color(C), MergedAttributes) -> write(S, '}') ; true), | |
110 | write(S, ' & '), | |
111 | write_latex_table_row(S, Entries, RowAttributes, TableAttributes, ColumnsAttributes). | |
112 | ||
113 | % ---- | |
114 | % escaping characters | |
115 | % ---- | |
116 | latex_escape(ToEscape, Escaped) :- | |
117 | atom(ToEscape), !, | |
118 | atom_codes(ToEscape, CodeList), | |
119 | latex_escape2(CodeList, EscapedCodeList), | |
120 | atom_codes(Escaped, EscapedCodeList). | |
121 | latex_escape(ToEscape, ToEscape). | |
122 | ||
123 | latex_escape2([],[]). | |
124 | latex_escape2([95|T], [92,95|EscT]) :- !, | |
125 | latex_escape2(T, EscT). | |
126 | latex_escape2([35|T], [92,35|EscT]) :- !, | |
127 | latex_escape2(T, EscT). | |
128 | latex_escape2([36|T], [92,36|EscT]) :- !, | |
129 | latex_escape2(T, EscT). | |
130 | latex_escape2([37|T], [92,37|EscT]) :- !, | |
131 | latex_escape2(T, EscT). | |
132 | latex_escape2([H|T], [H|EscT]) :- | |
133 | latex_escape2(T, EscT). |