Ask Your Question
1

SageMath export to latex Document

asked 2019-01-21 14:21:20 +0200

thetha gravatar image

Is there a way automatically create a latex document from within the sagemath notebook. It is possible to push one line to latex output, but its very anoying to callect its by hand and copy and paste it into latex document. In my opiniton its much fast to write in sagemath.

edit retag flag offensive close merge delete

3 Answers

Sort by » oldest newest most voted
3

answered 2019-01-23 15:52:13 +0200

slelievre gravatar image

Working with SageTeX

The best if you want to produce a LaTeX document in the end is to work in a LaTeX document from the start and to use SageTeX with its Sage commands and environments including sage, sagesilent, sageblock, sageexample, sagecommandline, etc.

Note that LaTeX and SageTeX are especially easy to use on CoCalc.

The LaTeX document template provided by CoCalc already has \usepackage{sagetex}, just commented out, so it's just a matter of uncommenting it to start using sagetex.

The SageTeX example file gives good indications on getting started, and you can learn more tips by reading the sagetex.dtx file.

There is good documentation on the LaTeX functionality in CoCalc at:

Lots of SageTeX-related questions get answered either here on Ask Sage or at TeX Stack Exchange:

Writing to a LaTeX file from the notebook

The annoyance you mention of having to copy-paste LaTeX output from the notebook to a LaTeX document:

its very anoying to collect it by hand and copy and paste it into latex document

can be worked around by using Python commands to write to a file.

To give an example, suppose in a Sage session (whether in the Sage REPL in the Terminal or in a Jupyter Notebook) you had this code:

sage: A = identity_matrix(ZZ, 2)
sage: A
[1 0]
[0 1]
sage: latex(A)
\left(\begin{array}{rr}
1 & 0 \\
0 & 1
\end{array}\right)

then, instead of copy-pasting by hand to a LaTeX document, you could run the following in Sage:

with open('/path/to/file.txt', 'a') as f:
    f.write(latex(A))

and this would append the lines

\left(\begin{array}{rr}
1 & 0 \\
0 & 1
\end{array}\right)

to your LaTeX document.

edit flag offensive delete link more
2

answered 2019-01-21 17:52:15 +0200

Emmanuel Charpentier gravatar image

updated 2019-01-24 09:38:16 +0200

Foreword :

  • Basic LaTeX literacy is a must for almost any "hard science" curriculum at one point or another. Gaining this ability as soon and as painlessly as possible is a plus for your students. Since many (most ?) tools/markup languages offer the possibility of using LaTeX markup for various tasks, a basic LaTeX knowledge (structure, maths markup, cross-references) is useful "almost everywhere".

  • In 40 years, solution for a myriad of document preparation problems have been developed for LaTeX (mostly in view of paper printing). Using LaTeX allows you (and your students) to tap this resource...

That said, LaTeX care and feeding may be considered superfluous at the beginning of a curriculum. A few possibility come to mind :

  • Pythontex can be used to mix text and formal computation, not only in sage but also in Python and a few other languages, à la LaTeX+Sagetex. But the markup language is still LaTeX. I know tha Geoffer Poore, pythontex's author, is planning a similar tool for markdown-based tdocuments, but I haven't the foggiest notion of the advancement of this project...

  • rmarkdown ( and knitr) are a tool giving the ability to mix R code and LaTeX or markdown text ; Markdown is lighter (and less powerful) than LaTeX. and the reticulate package, which allows to use Python source blocks, may be coaxed to accept Sage blocks (note : this is not (yet) implemended...).

Another possibility is to use sage blocks on org mode's Babel. Org mode`'s markup is less verbose and may be considered by some as lighter than LaTeX (although less powerful and less consistent), possibly easier to use.

Its largest drawback (and, possibly, advantage) is that it is (currently) Emacs-bound (although ports to other editors have been started, I'm not sure that Org-mode can be effectively compiled by anything else than emacs) : using it entails the Matterhorn-like learning curve (and benefits) of Emacs...

But the real points to Babel are :

  • the ability to use more than one language for code blocks. For example, sage or Mathematica for formal computation, R, Matlab or octave for statistics and numerical computation, graphviz for graphs, etc... About 70 languages are currently (more or less) supported by Babel.

  • The ability to get output as LaTeX/PDF, but also HTML, Beamer slides, HTML slides, LibreOffice's .odt and even Word's .docx. Quite useful when working in low-computer-literacy environment (read "administration"). Add pandoc to the mix, and the possibilities are almost boundless...

So this may be an interesting tool for the instructor, but probably less so for the students.

Note that, in any case, your students will have to use a markup language to be able to do about anything ; learning to leave the damned mouse alone (i. e. renounce WYSIWYG) is probably the hardest step of the learning process. Th exact markup language is probably not too important : markdown is less verbose than LaTeX, but has less possibilities (and, as far as I know, is not Turing-complete...).

edit flag offensive delete link more
2

answered 2019-01-21 15:26:15 +0200

tmonteil gravatar image

updated 2019-01-21 16:52:56 +0200

Jupyter has the possibity to export a workheet to latex (see File > Download to format... > Latex) but the rendering will look exactly like the worksheet, that is with a monospaced font, not as nice latex formulas.

Let me suggest the converse approach: you can embed Sage computation directly within Latex document with sagetex, so that when you enter some Sage code, it will be replaced with a latexed version of the Sage result in the compilation. See the following part for more details: https://doc.sagemath.org/html/en/tuto...

EDIT : If the goal is to avoid using LaTeX because it is too complicated for students, why not directly work with jupyter notebooks ? It is possible to add some text (markdown) cells, add images, export/import them from almost everywhere, etc. If the students do not want/have to learn latex, they could just share and print jupyter worksheets.

edit flag offensive delete link more

Comments

That the workflow i am trying to avoid. I am working a math tutor. I and I use cocal its the online version of sagemath. It very easy to use for students. Sagemath i mean. Latex not so much

thetha gravatar imagethetha ( 2019-01-21 15:34:40 +0200 )edit

SageMath is much better then jupyter,..in my opinion.

thetha gravatar imagethetha ( 2019-01-23 20:01:42 +0200 )edit

But Sagemath uses jupyter for its notebook !

tmonteil gravatar imagetmonteil ( 2019-01-24 12:27:56 +0200 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2019-01-21 14:21:20 +0200

Seen: 3,429 times

Last updated: Jan 24 '19