Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

I will provide a parcial answer to the question. Let us consider the following LaTeX code:



def NumWrap(number):
    if number in ZZ or number.parent()==QQ:
        return r"\num{"+ str(number) + "}"
        return r"\num{"+ str(float(number)) + "}" 

def MatWrap(m):
    s = r"\begin{pmatrix} " 
    for nrow in range(m.nrows()):
        for ncol in range(m.ncols()-1):
            s += NumWrap(m[nrow,ncol]) + " & "
        s += NumWrap(m[nrow,-1])
        if nrow<m.nrows()-1: s += r" \\ "
    s += r" \end{pmatrix}"
    return s

Let us do a few operations with integers:
   \num{125}\times\num{8750}+\num{93251} = \sagestr{NumWrap(125*8750+93251)}.
Now, we operate with fractions, first in display math mode:
   \sisetup{quotient-mode = fraction}
   \num{12985/3425} + \num{100000/625}
   = \sagestr{NumWrap(12985/3425+100000/625)},
and now in inline math mode:
$\num{12985/3425} + \num{100000/625}=\sagestr{NumWrap(12985/3425+100000/625)}$.

It is the turn for floating point numbers and matrices.
m = matrix([[12.3456, 2.3453456],[-987654.32456, 23145.5342]])
   M = \sagestr{MatWrap(m)}.
   M^{-1} = \sagestr{MatWrap(m.inverse())}.


Suppose that x is a number coming from a Sage computation that we want to include in the LaTeX document. The Sage function NumWrap takes a string representation of this number and returns the string \num{str(x)}. This string is passed from Sage to LaTeX by the \sagestr macro, so it can be typeset. For a matrix, MatWrap applies NumWrap to each element and adds LaTeX code to get a pmatrix environment (defined in the amsmath package). I adjoin a screen capture of the document obtained after typesetting the above LaTeX code. image description