@@ -1407,6 +1407,163 @@ function MatricesForHomalg.ReducedSyzygiesOfRows(A::Singular.smatrix, N::Singula
14071407 return Singular. transpose (MatricesForHomalg. ReducedSyzygiesOfColumns (Singular. transpose (A), Singular. transpose (N)))
14081408end
14091409
1410+ # # LaTeX output for Singular rings and matrices
1411+
1412+ """
1413+ LaTeXOutputOfHomalgRing(R::Singular.Integers)
1414+
1415+ Return `"\\ mathbb{Z}"` for the Singular integer ring.
1416+
1417+ ```jldoctest
1418+ julia> using Singular, MatricesForHomalg
1419+
1420+ julia> LaTeXOutputOfHomalgRing(HomalgRingOfIntegersInSingular())
1421+ "\\ mathbb{Z}"
1422+ ```
1423+ """
1424+ function MatricesForHomalg. LaTeXOutputOfHomalgRing (:: Singular.Integers ):: String
1425+ return " \\ mathbb{Z}"
1426+ end
1427+
1428+ """
1429+ LaTeXOutputOfHomalgRing(R::Singular.Rationals)
1430+
1431+ Return `"\\ mathbb{Q}"` for the Singular rational field.
1432+
1433+ ```jldoctest
1434+ julia> using Singular, MatricesForHomalg
1435+
1436+ julia> LaTeXOutputOfHomalgRing(HomalgFieldOfRationalsInSingular())
1437+ "\\ mathbb{Q}"
1438+ ```
1439+ """
1440+ function MatricesForHomalg. LaTeXOutputOfHomalgRing (:: Singular.Rationals ):: String
1441+ return " \\ mathbb{Q}"
1442+ end
1443+
1444+ """
1445+ LaTeXOutputOfHomalgRing(R::Singular.PolyRing)
1446+
1447+ Return a LaTeX string for the Singular polynomial ring R.
1448+
1449+ ```jldoctest
1450+ julia> using Singular, MatricesForHomalg
1451+
1452+ julia> R, _ = Singular.polynomial_ring(Singular.QQ, ["x", "y"]);
1453+
1454+ julia> LaTeXOutputOfHomalgRing(R)
1455+ "\\\\ mathbb{Q}[x, y]"
1456+
1457+ julia> S = R / [R("x^2 + y^2 - 1")];
1458+
1459+ julia> LaTeXOutputOfHomalgRing(S)
1460+ "\\\\ mathbb{Q}[x, y] / ( x^2 + y^2 - 1 )"
1461+ ```
1462+ """
1463+ function MatricesForHomalg. LaTeXOutputOfHomalgRing (R:: Singular.PolyRing ):: String
1464+ base_latex = LaTeXOutputOfHomalgRing (Singular. base_ring (R))
1465+ vars = join (map (s -> string (s), R. S), " , " )
1466+ name = base_latex * " [" * vars * " ]"
1467+ if Singular. is_quotient_ring (R)
1468+ I = Singular. quotient_ideal (R)
1469+ rels = join ([string (Singular. gens (I)[k]) for k in 1 : Singular. ngens (I)], " , " )
1470+ name *= " / ( " * rels * " )"
1471+ end
1472+ return name
1473+ end
1474+
1475+ function MatricesForHomalg. _latex_entry (x:: Singular.spoly ):: String
1476+ Singular. iszero (x) && return " \\ cdot"
1477+ return MatricesForHomalg. LaTeXOutputOfHomalgRingElement (x)
1478+ end
1479+
1480+ # Build a monomial string from variable symbols and exponent vector.
1481+ # Uses ^{n} for exponents > 1, no separator between variables.
1482+ function _latex_monomial (syms:: Vector{Symbol} , exps:: Vector{Int} ):: String
1483+ factors = String[]
1484+ for (s, e) in zip (syms, exps)
1485+ e == 0 && continue
1486+ e == 1 ? push! (factors, string (s)) : push! (factors, " $(s) ^{$(e) }" )
1487+ end
1488+ return join (factors, " " )
1489+ end
1490+
1491+ # Returns (absolute_value_latex, is_negative) for a Singular integer coefficient.
1492+ function _latex_singular_coeff (c:: Singular.n_Z )
1493+ n = BigInt (c)
1494+ return (string (abs (n)), n < 0 )
1495+ end
1496+
1497+ # Returns (absolute_value_latex, is_negative) for a Singular rational coefficient.
1498+ function _latex_singular_coeff (c:: Singular.n_Q )
1499+ n = BigInt (numerator (c))
1500+ d = BigInt (denominator (c))
1501+ neg = n < 0
1502+ an = abs (n)
1503+ d == 1 && return (string (an), neg)
1504+ return (" \\ frac{$(an) }{$(d) }" , neg)
1505+ end
1506+
1507+ # Fallback for other coefficient types.
1508+ function _latex_singular_coeff (c)
1509+ s = string (c)
1510+ startswith (s, " -" ) && return (s[2 : end ], true )
1511+ return (s, false )
1512+ end
1513+
1514+ """
1515+ LaTeXOutputOfHomalgRingElement(f::Singular.spoly)
1516+
1517+ Return a LaTeX string for the Singular polynomial f, built from its internal
1518+ term structure: monomials use `^{n}` for exponents, rational coefficients use
1519+ `\\ frac`, coefficient ±1 on non-constant monomials is suppressed, and terms
1520+ are joined with ` + ` / ` - `.
1521+
1522+ ```jldoctest
1523+ julia> using Singular, MatricesForHomalg
1524+
1525+ julia> R, (x, y) = Singular.polynomial_ring(Singular.QQ, ["x", "y"]);
1526+
1527+ julia> LaTeXOutputOfHomalgRingElement(x^2 + 2*y)
1528+ "x^{2} + 2y"
1529+
1530+ julia> LaTeXOutputOfHomalgRingElement(QQ(1,3)*x - QQ(1,2)*y^2)
1531+ "\\\\ frac{1}{3}x - \\\\ frac{1}{2}y^{2}"
1532+
1533+ julia> LaTeXOutputOfHomalgRingElement(zero(R))
1534+ "0"
1535+
1536+ julia> LaTeXOutputOfHomalgRingElement(one(R))
1537+ "1"
1538+ ```
1539+ """
1540+ function MatricesForHomalg. LaTeXOutputOfHomalgRingElement (f:: Singular.spoly ):: String
1541+ Singular. iszero (f) && return " 0"
1542+ R = parent (f)
1543+ syms = R. S
1544+ result = " "
1545+ first_term = true
1546+ for (c, exps) in zip (Singular. coefficients (f), Singular. exponent_vectors (f))
1547+ mon = _latex_monomial (syms, exps)
1548+ coeff_latex, neg = _latex_singular_coeff (c)
1549+ is_one = (coeff_latex == " 1" )
1550+ term = if isempty (mon)
1551+ coeff_latex # constant term: show the absolute coefficient
1552+ elseif is_one
1553+ mon # coefficient ±1: show only the monomial
1554+ else
1555+ coeff_latex * mon # general: coefficient immediately before monomial
1556+ end
1557+ if first_term
1558+ result = neg ? " -$(term) " : term
1559+ first_term = false
1560+ else
1561+ result *= neg ? " - $(term) " : " + $(term) "
1562+ end
1563+ end
1564+ return result
1565+ end
1566+
14101567# # Ring membership for Singular polynomial rings
14111568#
14121569# In GAP, `r in Ring` checks if r is an element of Ring. Julia's generic `in`
0 commit comments