1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
use crate::objects::{Measure, Style};
/// Object to create a table with all required parameters, either in latex or
/// typst format.
pub struct Table<'a> {
data: Vec<Measure>,
header: Vec<&'a str>,
transpose: bool,
caption: &'a str,
label: &'a str,
}
impl<'a> Table<'a> {
/// Constructs a new Table with some default values that can be changed.
pub fn new(data: Vec<Measure>, header: Vec<&str>) -> Table {
Table {
data,
header,
transpose: true,
caption: "caption",
label: "label",
}
}
/// Changes table disposal, true for vertical and false for horizontal, by
/// default is set to true.
pub fn transpose(mut self, transpose: bool) -> Self {
self.transpose = transpose;
self
}
/// Set a caption for latex table, "caption" by default.
pub fn caption(mut self, caption: &'a str) -> Self {
self.caption = caption;
self
}
/// Set a label for latex table, "label" by default.
pub fn label(mut self, label: &'a str) -> Self {
self.label = label;
self
}
/// Creates a table using your measures in typst format.
///
/// # Examples
///
/// ```rust
/// # use ferrilab::{measure, Measure, tables::typst};
/// let time = measure!([0.2, 0.3, 0.40, 0.5], [0.01, 0.02, 0.02, 0.04]);
/// let position = measure!([2.4, 3.4, 5.1, 7.2], [0.2, 0.4, 0.5, 0.8]);
/// let speed = &position / &time;
///
/// println!("{}", typst(vec![time, position, speed], vec!["t/s", "x/m", "v/ms-1"], true))
///
/// // Output
///
/// /*
/// table(
/// columns: 3,
/// align: center,
/// [t/s], [x/m], [v/ms-1]
/// [$0.2 plus.minus 0.01$], [$2.4 plus.minus 0.2$], [$2.6 plus.minus 0.2$]
/// [$0.3 plus.minus 0.02$], [$3.4 plus.minus 0.4$], [$3.7 plus.minus 0.4$]
/// [$0.4 plus.minus 0.02$], [$5.1 plus.minus 0.5$], [$5.5 plus.minus 0.5$]
/// [$0.5 plus.minus 0.04$], [$7.2 plus.minus 0.8$], [$7.7 plus.minus 0.8$]
/// )
/// */
/// ```
pub fn typst(self) -> String {
typst(self.data, self.header, self.transpose)
}
/// Creates a table using your measures in latex format.
///
/// # Examples
///
/// ```rust
/// # use ferrilab::{measure, Measure, tables::latex};
/// let time = measure!([0.2, 0.3, 0.40, 0.5], [0.01, 0.02, 0.02, 0.04]);
/// let position = measure!([2.4, 3.4, 5.1, 7.2], [0.2, 0.4, 0.5, 0.8]);
/// let speed = &position / &time;
///
/// println!("{}", latex(vec![time, position, speed], vec!["t/s", "x/m", "v/ms-1"], "Caption", "label", true))
///
/// // Output
///
/// /*
/// \begin{table}[ht]
/// \centering
/// \caption{Caption}
/// \label{label}
/// \begin{tabular}{|c|c|c|}
/// t/s & x/m & v/ms-1\\
/// $0.2 \pm 0.01$ & $2.4 \pm 0.2$ & $2.6 \pm 0.2$\\
/// $0.3 \pm 0.02$ & $3.4 \pm 0.4$ & $3.7 \pm 0.4$\\
/// $0.4 \pm 0.02$ & $5.1 \pm 0.5$ & $5.5 \pm 0.5$\\
/// $0.5 \pm 0.04$ & $7.2 \pm 0.8$ & $7.7 \pm 0.8$\\
/// \end{tabular}
/// \end{table}
/// */
/// ```
pub fn latex(self) -> String {
latex(
self.data,
self.header,
self.caption,
self.label,
self.transpose,
)
}
}
fn typst(data: Vec<Measure>, header: Vec<&str>, transpose: bool) -> String {
let mut data = create_table_list(data, header, transpose, Style::TypstTable);
data = data
.into_iter()
.map(|vec| vec.into_iter().map(|str| format!("[{}]", str)).collect())
.collect();
let tabular: String = data
.iter()
.map(|vec| format!("\n \t\t{}", vec.join(", ")))
.collect::<Vec<String>>()
.join("");
let width = data.into_iter().map(|vec| vec.len()).max().unwrap();
format!(
"\t table(\n\t columns: {}, \n\t align: center, \n\t\t{} \n)",
width, tabular
)
}
fn latex(
data: Vec<Measure>,
header: Vec<&str>,
caption: &str,
label: &str,
transpose: bool,
) -> String {
let data = create_table_list(data, header, transpose, Style::LatexTable);
let tabular: Vec<String> = data
.iter()
.map(|vec| format!("{}\\\\ \n\t\t", vec.join(" & ")))
.collect();
let width: usize = data.into_iter().map(|vec| vec.len()).max().unwrap();
let tabular = format!(
"\t \\begin{{tabular}}{{{}|}}\n\t\t{}\n\t\\end{{tabular}}",
vec!["|c"; width].join(""),
tabular.join("")
);
format!("\\begin{{table}}[ht]\n \\centering \n\n\\caption{{{}}}\n\\label{{{}}}\n\n{}\n\n\\end{{table}}", caption, label, tabular)
}
fn transpose<T>(v: Vec<Vec<T>>) -> Vec<Vec<T>> {
assert!(!v.is_empty());
let len = v[0].len();
let mut iters: Vec<_> = v.into_iter().map(|n| n.into_iter()).collect();
(0..len)
.map(|_| {
iters
.iter_mut()
.map(|n| n.next().unwrap())
.collect::<Vec<T>>()
})
.collect()
}
fn create_table_list(
data: Vec<Measure>,
mut header: Vec<&str>,
transposed: bool,
style: Style,
) -> Vec<Vec<String>> {
let mut data: Vec<Vec<String>> = data
.into_iter()
.map(|measure| {
measure
.list_of_measures()
.into_iter()
.map(|measure| format!("{}", measure.change_style(style)))
.collect()
})
.collect();
let max_len = data.iter().map(|vec| vec.len()).max().unwrap();
data = data
.into_iter()
.map(|mut vec| {
if max_len > vec.len() {
vec.extend(vec!["".to_string(); max_len - vec.len()]);
vec
} else {
vec
}
})
.collect();
if !header.is_empty() {
if header.len() < data.len() {
header.extend(vec![""; data.len() - header.len()]);
}
data = data
.into_iter()
.zip(header)
.map(|(mut data, head)| {
data.insert(0, head.to_string());
data
})
.collect::<Vec<Vec<String>>>();
}
if transposed {
return transpose(data);
}
data
}