# Revision history [back]

## 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)