Skip to main content

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