ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Thu, 24 Jan 2019 05:27:56 -0600SageMath export to latex Documenthttp://ask.sagemath.org/question/45119/sagemath-export-to-latex-document/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. Mon, 21 Jan 2019 07:21:20 -0600http://ask.sagemath.org/question/45119/sagemath-export-to-latex-document/Answer by Emmanuel Charpentier for <p>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. </p>
http://ask.sagemath.org/question/45119/sagemath-export-to-latex-document/?answer=45133#post-id-45133Foreword :
* 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](https://github.com/gpoore/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`](https://orgmode.org/)'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`](https://pandoc.org/) 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...).Mon, 21 Jan 2019 10:52:15 -0600http://ask.sagemath.org/question/45119/sagemath-export-to-latex-document/?answer=45133#post-id-45133Answer by slelievre for <p>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. </p>
http://ask.sagemath.org/question/45119/sagemath-export-to-latex-document/?answer=45149#post-id-45149## 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](https://github.com/sagemath/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](https://cocalc.com/index.html).
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](https://github.com/sagemath/sagetex/blob/master/example.tex)
gives good indications on getting started, and you can learn more tips by reading the
[sagetex.dtx file](https://github.com/sagemath/sagetex/blob/master/sagetex.dtx).
There is good documentation on the LaTeX functionality in CoCalc at:
- [https://doc.cocalc.com/latex.html](https://doc.cocalc.com/latex.html)
- [https://cocalc.com/doc/latex-editor.html](https://cocalc.com/doc/latex-editor.html)
Lots of SageTeX-related questions get answered either here on Ask Sage
or at TeX Stack Exchange:
- [SageTeX questions on Ask Sage](https://ask.sagemath.org/questions/scope:all/sort:activity-desc/page:1/query:sagetex/)
- [SageTeX questions on TeX Stack Exchange](https://tex.stackexchange.com/questions/tagged/sagetex?sort=newest)
## 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.
Wed, 23 Jan 2019 08:52:13 -0600http://ask.sagemath.org/question/45119/sagemath-export-to-latex-document/?answer=45149#post-id-45149Answer by tmonteil for <p>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. </p>
http://ask.sagemath.org/question/45119/sagemath-export-to-latex-document/?answer=45125#post-id-45125Jupyter 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/tutorial/sagetex.html
**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.Mon, 21 Jan 2019 08:26:15 -0600http://ask.sagemath.org/question/45119/sagemath-export-to-latex-document/?answer=45125#post-id-45125Comment by tmonteil for <p>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.</p>
<p>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: <a href="https://doc.sagemath.org/html/en/tutorial/sagetex.html">https://doc.sagemath.org/html/en/tuto...</a></p>
<p><strong>EDIT</strong> : 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.</p>
http://ask.sagemath.org/question/45119/sagemath-export-to-latex-document/?comment=45161#post-id-45161But Sagemath uses jupyter for its notebook !Thu, 24 Jan 2019 05:27:56 -0600http://ask.sagemath.org/question/45119/sagemath-export-to-latex-document/?comment=45161#post-id-45161Comment by thetha for <p>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.</p>
<p>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: <a href="https://doc.sagemath.org/html/en/tutorial/sagetex.html">https://doc.sagemath.org/html/en/tuto...</a></p>
<p><strong>EDIT</strong> : 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.</p>
http://ask.sagemath.org/question/45119/sagemath-export-to-latex-document/?comment=45157#post-id-45157SageMath is much better then jupyter,..in my opinion.Wed, 23 Jan 2019 13:01:42 -0600http://ask.sagemath.org/question/45119/sagemath-export-to-latex-document/?comment=45157#post-id-45157Comment by thetha for <p>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.</p>
<p>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: <a href="https://doc.sagemath.org/html/en/tutorial/sagetex.html">https://doc.sagemath.org/html/en/tuto...</a></p>
<p><strong>EDIT</strong> : 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.</p>
http://ask.sagemath.org/question/45119/sagemath-export-to-latex-document/?comment=45126#post-id-45126That 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 muchMon, 21 Jan 2019 08:34:40 -0600http://ask.sagemath.org/question/45119/sagemath-export-to-latex-document/?comment=45126#post-id-45126Answer by tmonteil for <p>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. </p>
http://ask.sagemath.org/question/45119/sagemath-export-to-latex-document/?answer=45130#post-id-45130So then wy not directly exchange jupyter notebooks ? It is possible to add some text (markdown) cells, add images, etc. If they do not want/have to learn latex, they could just share and print jupyter worksheets.Mon, 21 Jan 2019 08:58:53 -0600http://ask.sagemath.org/question/45119/sagemath-export-to-latex-document/?answer=45130#post-id-45130