Thegerdfather
Thegerdfather

Reputation: 409

Print Latex for system of equations in SymPy?

How would I write a system of equations in SymPy and output the equivalent Latex? The latex function seems to accept only one expression at a time.

import sympy as sp

x, y, z = sp.symbols('x, y, z')
eq1 = sp.Eq(x + y + z, 1)
eq2 = sp.Eq(x + y + 2 * z, 3)
output = sp.latex() # Do something here?

Upvotes: 0

Views: 575

Answers (1)

Davide_sd
Davide_sd

Reputation: 13185

One way is to create a function and combine the latex output of each equation.

def system_to_latex(*equations):
    n = len(equations)
    if n == 0:
        return ""
    l1 = r"\left\{\begin{matrix}%s\end{matrix}\right."
    l2 = r" \\ ".join(sp.latex(eq) for eq in equations)
    return l1 % l2

print(system_to_latex(eq1, eq2))
# out: \left\{\begin{matrix}x + y + z = 1 \\ x + y + 2 z = 3\end{matrix}\right.

Upvotes: 1

Related Questions