This notebook can be useful to quickly make a latex representation of linear systems of 2 variables.


In [ ]:
pre_str = r"""
$
\left\{
\begin{align}
"""

post_str = r"""\end{align}
\right.
$
"""

def complete_matrix_to_latex(Ab):
    #6 x_1 + 7 x_2 &  = 11

    sys_str = ""
    
    for eq in Ab:
        
        eq_str = ""
        
        for coef_index, coef in enumerate(eq[:-1]):
            
            if coef != 0:
                if len(eq_str) > 0:
                    eq_str += "+ "
                if coef == 1:
                    eq_str += "x_{" + str(coef_index + 1) + "} "
                elif coef == -1:
                    eq_str += "-x_{" + str(coef_index + 1) + "} "
                else:
                    eq_str += str(coef) + " x_{" + str(coef_index + 1) + "} "
                        
        eq_str = eq_str.replace("+ -", "- ")
        eq_str += "& = {} \\\\\n".format(eq[-1])
        sys_str += eq_str
    
    return pre_str + sys_str + post_str

In [ ]:
Ab = np.array([[0, -1, 1],
               [2, 0, 1],
               [1, -1, 2]])

print(complete_matrix_to_latex(Ab))