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
use super::*;
pub struct Math;
impl Format for Math {
fn mime() -> mime::Mime {
return "text/x-latex".parse().unwrap();
}
fn this_format() -> Self {
Math
}
fn escape(f: &mut Formatter, mut s: &str) -> Result<(), Error> {
let badstuff = "&{}#%\\~$_^";
while let Some(idx) = s.find(|c| badstuff.contains(c)) {
let (first, rest) = s.split_at(idx);
let (badchar, tail) = rest.split_at(1);
f.write_str(first)?;
f.write_str(match badchar {
"&" => r"\&",
"{" => r"\{",
"}" => r"\}",
"#" => r"\#",
"%" => r"\%",
"\\" => r"\textbackslash{}",
"~" => r"\textasciitilde{}",
"$" => r"\$",
"_" => r"\_",
"^" => r"\^",
_ => unreachable!(),
})?;
s = tail;
}
f.write_str(s)
}
}
display_integers_as!(Math);
display_floats_as!(Math, r"\times10^{", "}", 3, Some("10^{"));
#[test]
fn escaping() {
assert_eq!(&format_as!(Math, ("&")), r"\&");
assert_eq!(
&format_as!(Math, ("hello &>this is cool")),
r"hello \&>this is cool"
);
assert_eq!(
&format_as!(Math, ("hello &>this is 'cool")),
r"hello \&>this is 'cool"
);
}
#[test]
fn floats() {
assert_eq!(&format_as!(Math, 3.0), "3");
assert_eq!(&format_as!(Math, 3e5), r"3\times10^{5}");
assert_eq!(&format_as!(Math, 1e5), r"10^{5}");
assert_eq!(&format_as!(Math, 3e4), "30000");
}