ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Wed, 13 May 2020 15:43:35 +0200Latex and SageMathhttps://ask.sagemath.org/question/51380/latex-and-sagemath/ I donot know how to exactly state my question. I gonna give an example.
Step 1. Type in A=matrix([[1,-3,-4,3],[-4,6,-2,3],[-3,7,6,-4]])
Step 2. latex(A) gives the latex code for this matrix.
My question is that given a latex code, is there a way to converts to SageMath code?
I am new to SageMath. As an instructor of math for 5 years, I have many resources prepared in latex code.
So I really wanna converts them to SageMath instead of type it again.
Thank you in advance.Tue, 12 May 2020 03:31:27 +0200https://ask.sagemath.org/question/51380/latex-and-sagemath/Comment by rburing for <p>I donot know how to exactly state my question. I gonna give an example.</p>
<p>Step 1. Type in A=matrix([[1,-3,-4,3],[-4,6,-2,3],[-3,7,6,-4]])</p>
<p>Step 2. latex(A) gives the latex code for this matrix.</p>
<p>My question is that given a latex code, is there a way to converts to SageMath code? </p>
<p>I am new to SageMath. As an instructor of math for 5 years, I have many resources prepared in latex code. </p>
<p>So I really wanna converts them to SageMath instead of type it again.</p>
<p>Thank you in advance.</p>
https://ask.sagemath.org/question/51380/latex-and-sagemath/?comment=51381#post-id-51381Well, yes, but there are many ways to typeset matrices in LaTeX. Can you give some example(s) which you want to convert?Tue, 12 May 2020 08:05:25 +0200https://ask.sagemath.org/question/51380/latex-and-sagemath/?comment=51381#post-id-51381Comment by tanyanlan for <p>I donot know how to exactly state my question. I gonna give an example.</p>
<p>Step 1. Type in A=matrix([[1,-3,-4,3],[-4,6,-2,3],[-3,7,6,-4]])</p>
<p>Step 2. latex(A) gives the latex code for this matrix.</p>
<p>My question is that given a latex code, is there a way to converts to SageMath code? </p>
<p>I am new to SageMath. As an instructor of math for 5 years, I have many resources prepared in latex code. </p>
<p>So I really wanna converts them to SageMath instead of type it again.</p>
<p>Thank you in advance.</p>
https://ask.sagemath.org/question/51380/latex-and-sagemath/?comment=51383#post-id-51383Thanks a lot for replying my question.
\left(\begin{array}{rrr}0 & 1 & 2 \\ 1 & 0 & 3 \\ 4 & -3 & 8 \end{array}\right)Tue, 12 May 2020 10:03:08 +0200https://ask.sagemath.org/question/51380/latex-and-sagemath/?comment=51383#post-id-51383Comment by tanyanlan for <p>I donot know how to exactly state my question. I gonna give an example.</p>
<p>Step 1. Type in A=matrix([[1,-3,-4,3],[-4,6,-2,3],[-3,7,6,-4]])</p>
<p>Step 2. latex(A) gives the latex code for this matrix.</p>
<p>My question is that given a latex code, is there a way to converts to SageMath code? </p>
<p>I am new to SageMath. As an instructor of math for 5 years, I have many resources prepared in latex code. </p>
<p>So I really wanna converts them to SageMath instead of type it again.</p>
<p>Thank you in advance.</p>
https://ask.sagemath.org/question/51380/latex-and-sagemath/?comment=51396#post-id-51396Are you able to give me some reference to read?Tue, 12 May 2020 19:20:56 +0200https://ask.sagemath.org/question/51380/latex-and-sagemath/?comment=51396#post-id-51396Answer by slelievre for <p>I donot know how to exactly state my question. I gonna give an example.</p>
<p>Step 1. Type in A=matrix([[1,-3,-4,3],[-4,6,-2,3],[-3,7,6,-4]])</p>
<p>Step 2. latex(A) gives the latex code for this matrix.</p>
<p>My question is that given a latex code, is there a way to converts to SageMath code? </p>
<p>I am new to SageMath. As an instructor of math for 5 years, I have many resources prepared in latex code. </p>
<p>So I really wanna converts them to SageMath instead of type it again.</p>
<p>Thank you in advance.</p>
https://ask.sagemath.org/question/51380/latex-and-sagemath/?answer=51397#post-id-51397Here is a function to convert the LaTeX string of a matrix
into a Sage matrix.
The function includes a bit of documentation with an example.
def matrix_from_latex(mat):
r"""
Return a Sage matrix corresponding to this LaTeX matrix.
Convert the LaTeX string for a matrix with integer entries
into a Sage matrix.
EXAMPLE:
Convert a Sage matrix to LaTeX and back::
sage: a = matrix([[0, 1, 2], [1, 0, 3], [4, -3, 8]])
sage: a
[ 0 1 2]
[ 1 0 3]
[ 4 -3 8]
sage: b = latex(a)
sage: print(b)
\left(\begin{array}{rrr}
0 & 1 & 2 \\
1 & 0 & 3 \\
4 & -3 & 8
\end{array}\right)
sage: c = matrix_from_latex(b)
sage: c
[ 0 1 2]
[ 1 0 3]
[ 4 -3 8]
"""
bits = [r'\left(\begin{array}', r'\end{array}\right)', 'r', '{}']
for b in bits:
mat = mat.replace(b, '')
row = mat.split(r'\\')
return matrix([[ZZ(a) for a in row.split('&')] for row in rows])
To start directly from a string such as in a comment to the question:
sage: z = r'\left(\begin{array}{rrr}0 & 1 & 2 \\ 1 & 0 & 3 \\ 4 & -3 & 8 \end{array}\right)'
sage: matrix_from_latex(z)
[ 0 1 2]
[ 1 0 3]
[ 4 -3 8]
Note that the function provided above works for matrices with integer entries.
It could be adapted to more general entries.Tue, 12 May 2020 20:14:38 +0200https://ask.sagemath.org/question/51380/latex-and-sagemath/?answer=51397#post-id-51397Comment by tanyanlan for <p>Here is a function to convert the LaTeX string of a matrix
into a Sage matrix.</p>
<p>The function includes a bit of documentation with an example.</p>
<pre><code>def matrix_from_latex(mat):
r"""
Return a Sage matrix corresponding to this LaTeX matrix.
Convert the LaTeX string for a matrix with integer entries
into a Sage matrix.
EXAMPLE:
Convert a Sage matrix to LaTeX and back::
sage: a = matrix([[0, 1, 2], [1, 0, 3], [4, -3, 8]])
sage: a
[ 0 1 2]
[ 1 0 3]
[ 4 -3 8]
sage: b = latex(a)
sage: print(b)
\left(\begin{array}{rrr}
0 & 1 & 2 \\
1 & 0 & 3 \\
4 & -3 & 8
\end{array}\right)
sage: c = matrix_from_latex(b)
sage: c
[ 0 1 2]
[ 1 0 3]
[ 4 -3 8]
"""
bits = [r'\left(\begin{array}', r'\end{array}\right)', 'r', '{}']
for b in bits:
mat = mat.replace(b, '')
row = mat.split(r'\\')
return matrix([[ZZ(a) for a in row.split('&')] for row in rows])
</code></pre>
<p>To start directly from a string such as in a comment to the question:</p>
<pre><code>sage: z = r'\left(\begin{array}{rrr}0 & 1 & 2 \\ 1 & 0 & 3 \\ 4 & -3 & 8 \end{array}\right)'
sage: matrix_from_latex(z)
[ 0 1 2]
[ 1 0 3]
[ 4 -3 8]
</code></pre>
<p>Note that the function provided above works for matrices with integer entries.</p>
<p>It could be adapted to more general entries.</p>
https://ask.sagemath.org/question/51380/latex-and-sagemath/?comment=51401#post-id-51401Thank you so much. It seems that I'd better type the matrix into the system since I just occasionally use sagemath.Wed, 13 May 2020 15:43:35 +0200https://ask.sagemath.org/question/51380/latex-and-sagemath/?comment=51401#post-id-51401