malachite_nz/integer_vector/conversion/string/latex.rs
1// Copyright © 2026 Mikhail Hogrefe
2//
3// This file is part of Malachite.
4//
5// Malachite is free software: you can redistribute it and/or modify it under the terms of the GNU
6// Lesser General Public License (LGPL) as published by the Free Software Foundation; either version
7// 3 of the License, or (at your option) any later version. See <https://www.gnu.org/licenses/>.
8
9use crate::integer_vector::IntegerVector;
10use core::fmt::{Formatter, Result};
11use malachite_base::strings::latex::ToLatex;
12
13impl ToLatex for IntegerVector {
14 /// Writes an [`IntegerVector`] as a LaTeX math-mode fragment.
15 ///
16 /// The elements' fragments are separated by commas and enclosed in parentheses that grow to fit
17 /// their contents, as a vector's coordinates are written by hand. The 0-dimensional vector is
18 /// `\left(\right)`.
19 ///
20 /// # Worst-case complexity
21 /// $T(n) = O(n (\log n)^2 \log\log n)$
22 ///
23 /// $M(n) = O(n \log n)$
24 ///
25 /// where $T$ is time, $M$ is additional memory, and $n$ is the sum of the bits of the elements.
26 ///
27 /// # Examples
28 /// ```
29 /// use malachite_base::strings::latex::ToLatex;
30 /// use malachite_nz::integer::Integer;
31 /// use malachite_nz::integer_vector::IntegerVector;
32 ///
33 /// let v = IntegerVector {
34 /// elements: vec![Integer::from(1u32), Integer::from(2u32), Integer::from(3u32)],
35 /// };
36 /// assert_eq!(v.to_latex_string(), "\\left(1, 2, 3\\right)");
37 /// assert_eq!(
38 /// IntegerVector {
39 /// elements: vec![Integer::from(5u32)]
40 /// }
41 /// .to_latex_string(),
42 /// "\\left(5\\right)"
43 /// );
44 /// assert_eq!(
45 /// IntegerVector {
46 /// elements: Vec::new()
47 /// }
48 /// .to_latex_string(),
49 /// "\\left(\\right)"
50 /// );
51 /// ```
52 ///
53 /// The value column holds each vector as [`Display`](core::fmt::Display) writes it.
54 ///
55 /// | value | fragment | renders as |
56 /// |-------------|------------------------|------------------------|
57 /// | `(1, 2, 3)` | `\left(1, 2, 3\right)` | $\left(1, 2, 3\right)$ |
58 /// | `(5)` | `\left(5\right)` | $\left(5\right)$ |
59 /// | `()` | `\left(\right)` | $\left(\right)$ |
60 fn fmt_latex(&self, f: &mut Formatter) -> Result {
61 f.write_str("\\left(")?;
62 for (i, x) in self.elements.iter().enumerate() {
63 if i != 0 {
64 f.write_str(", ")?;
65 }
66 x.fmt_latex(f)?;
67 }
68 f.write_str("\\right)")
69 }
70}