Skip to main content

malachite_base/unsigned_polynomial/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_polynomial::UnsignedPolynomial;
12use crate::unsigned_polynomial::conversion::string::to_string::Language;
13use crate::vars::Var;
14use crate::vars::xyz::XyzVars;
15use core::fmt::{Formatter, Result};
16
17impl<T: PrimitiveUnsigned> ToLatex for UnsignedPolynomial<T> {
18    /// Writes a [`UnsignedPolynomial`] as a LaTeX math-mode fragment.
19    ///
20    /// The variable is called `x`.
21    /// [`to_latex_string_with`](crate::polynomial::Polynomial::to_latex_string_with) is the way to
22    /// call it something else.
23    ///
24    /// The fragment is the polynomial as it would be written by hand: the terms in order of
25    /// decreasing degree, joined with `+`, each one its coefficient followed by its variable and
26    /// then a superscript. A coefficient of 1 is left off, and so is an exponent of 1; the constant
27    /// term is its coefficient alone, and the zero polynomial, which has no terms, is `0`. Nothing
28    /// stands between a coefficient and its variable, since a number written against a variable can
29    /// only be multiplying it.
30    ///
31    /// A superscript is braced only when the exponent has more than one digit, since a superscript
32    /// of one character needs nothing to hold it together.
33    ///
34    /// # Worst-case complexity
35    /// $T(n) = O(n \log n \log\log n)$
36    ///
37    /// $M(n) = O(n \log n)$
38    ///
39    /// where $T$ is time, $M$ is additional memory, and $n$ is the sum of the bits of the
40    /// coefficients.
41    ///
42    /// # Examples
43    /// ```
44    /// use core::str::FromStr;
45    /// use malachite_base::strings::latex::ToLatex;
46    /// use malachite_base::unsigned_polynomial::UnsignedPolynomial;
47    ///
48    /// assert_eq!(
49    ///     UnsignedPolynomial::<u64>::from_str("x^2+3*x+2")
50    ///         .unwrap()
51    ///         .to_latex_string(),
52    ///     "x^2+3x+2"
53    /// );
54    /// assert_eq!(
55    ///     UnsignedPolynomial::<u64>::from_str("0")
56    ///         .unwrap()
57    ///         .to_latex_string(),
58    ///     "0"
59    /// );
60    /// assert_eq!(
61    ///     UnsignedPolynomial::<u64>::from_str("5")
62    ///         .unwrap()
63    ///         .to_latex_string(),
64    ///     "5"
65    /// );
66    /// assert_eq!(
67    ///     UnsignedPolynomial::<u64>::from_str("x")
68    ///         .unwrap()
69    ///         .to_latex_string(),
70    ///     "x"
71    /// );
72    /// assert_eq!(
73    ///     UnsignedPolynomial::<u64>::from_str("2*x^3")
74    ///         .unwrap()
75    ///         .to_latex_string(),
76    ///     "2x^3"
77    /// );
78    ///
79    /// // An exponent of more than one digit is braced.
80    /// assert_eq!(
81    ///     UnsignedPolynomial::<u64>::from_str("x^12+x^2")
82    ///         .unwrap()
83    ///         .to_latex_string(),
84    ///     "x^{12}+x^2"
85    /// );
86    /// ```
87    ///
88    /// The value column holds each polynomial as [`Display`](core::fmt::Display) writes it.
89    ///
90    /// | value       | fragment     | renders as   |
91    /// |-------------|--------------|--------------|
92    /// | `x^2+3*x+2` | `x^2+3x+2`   | $x^2+3x+2$   |
93    /// | `0`         | `0`          | $0$          |
94    /// | `5`         | `5`          | $5$          |
95    /// | `x`         | `x`          | $x$          |
96    /// | `2*x^3`     | `2x^3`       | $2x^3$       |
97    /// | `x^12+x^2`  | `x^{12}+x^2` | $x^{12}+x^2$ |
98    #[inline]
99    fn fmt_latex(&self, f: &mut Formatter) -> Result {
100        self.write_with_var(Var::new(&XyzVars, 0), Language::Latex, f)
101    }
102}