Skip to main content

malachite_base/unsigned_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::num::basic::unsigneds::PrimitiveUnsigned;
10use crate::strings::latex::ToLatex;
11use crate::unsigned_vector::UnsignedVector;
12use core::fmt::{Display, Formatter, Result};
13
14impl<T: PrimitiveUnsigned> ToLatex for UnsignedVector<T> {
15    /// Writes an [`UnsignedVector`] as a LaTeX math-mode fragment.
16    ///
17    /// The elements, written in decimal, are separated by commas and enclosed in parentheses that
18    /// grow to fit their contents, as a vector's coordinates are written by hand. The 0-dimensional
19    /// vector is `\left(\right)`.
20    ///
21    /// # Worst-case complexity
22    /// $T(n) = O(n)$
23    ///
24    /// $M(n) = O(1)$
25    ///
26    /// where $T$ is time, $M$ is additional memory, and $n$ is `self.dimension()`.
27    ///
28    /// # Examples
29    /// ```
30    /// use malachite_base::strings::latex::ToLatex;
31    /// use malachite_base::unsigned_vector::UnsignedVector;
32    ///
33    /// assert_eq!(
34    ///     UnsignedVector {
35    ///         elements: vec![1u32, 2, 3]
36    ///     }
37    ///     .to_latex_string(),
38    ///     "\\left(1, 2, 3\\right)"
39    /// );
40    /// assert_eq!(
41    ///     UnsignedVector {
42    ///         elements: vec![5u8]
43    ///     }
44    ///     .to_latex_string(),
45    ///     "\\left(5\\right)"
46    /// );
47    /// assert_eq!(
48    ///     UnsignedVector::<u32> {
49    ///         elements: Vec::new()
50    ///     }
51    ///     .to_latex_string(),
52    ///     "\\left(\\right)"
53    /// );
54    /// ```
55    ///
56    /// The value column holds each vector as [`Display`] writes it.
57    ///
58    /// | value       | fragment               | renders as             |
59    /// |-------------|------------------------|------------------------|
60    /// | `(1, 2, 3)` | `\left(1, 2, 3\right)` | $\left(1, 2, 3\right)$ |
61    /// | `(5)`       | `\left(5\right)`       | $\left(5\right)$       |
62    /// | `()`        | `\left(\right)`        | $\left(\right)$        |
63    fn fmt_latex(&self, f: &mut Formatter) -> Result {
64        f.write_str("\\left(")?;
65        for (i, x) in self.elements.iter().enumerate() {
66            if i != 0 {
67                f.write_str(", ")?;
68            }
69            Display::fmt(x, f)?;
70        }
71        f.write_str("\\right)")
72    }
73}