Ask Your Question

SageMath export to latex Document

asked 2019-01-21 07:21:20 -0500

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

answered 2019-01-21 10:52:15 -0500

Emmanuel Charpentier gravatar image

updated 2019-01-24 02:38:16 -0500

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

answered 2019-01-23 08:52:13 -0500

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)
1 & 0 \\
0 & 1

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:

and this would append the lines

1 & 0 \\
0 & 1

to your LaTeX document.

edit flag offensive delete link more

answered 2019-01-21 08:26:15 -0500

tmonteil gravatar image

updated 2019-01-21 09:52:56 -0500

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:

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


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 08:34:40 -0500 )edit

SageMath is much better then jupyter, my opinion.

thetha gravatar imagethetha ( 2019-01-23 13:01:42 -0500 )edit

But Sagemath uses jupyter for its notebook !

tmonteil gravatar imagetmonteil ( 2019-01-24 05:27:56 -0500 )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


Asked: 2019-01-21 07:21:20 -0500

Seen: 242 times

Last updated: Jan 24