How do I get the output of this text in latex with sagetex?

Using Sage\TeX, one can use Sage to compute things and put them into your \LaTeX{} document. For example, there are $\sage{number_of_partitions(1269)}$ integer partitions of $1269$. You don't need to compute the number yourself, or even cut and paste it from somewhere.

Here's some Sage code:

\begin{sageblock} f(x) = exp(x) * sin(2*x) \end{sageblock}

The second derivative of $f$ is

[ \frac{\mathrm{d}^{2}}{\mathrm{d}x^{2}} \sage{f(x)} = \sage{diff(f, x, 2)(x)}. ]

Here's a plot of $f$ from $-1$ to $1$:

\sageplot{plot(f, -1, 1)}

\end{document}

edit retag close merge delete

( 2020-06-12 11:20:36 -0600 )edit

I don't know how to run a code in latex with sagetex. I have got some calculations in sage and I want latex to print them out.

( 2020-06-12 11:25:42 -0600 )edit

Sort by » oldest newest most voted

EDIT: a good starting point is this documentation.

Very roughly :

• create your text in LaTeX, interspeding Sage fragments as needed : this will be, say, yourtext.tex.

• pdflatex yourtext.tex : this will create yourtext.sagetex.sage, and give warninges about undefined references.

• sage yourtext.sagetex.sage. This will, among other effects, update yourtext.tex.

• pdflatex yourtext.tex (usually, you have to do it twice).

yourtext.pdf is now the sought document...

EDIT : The documenntation compiled in PDF form an an example tec=xt are available in $SAGE_ROOT/local/share/doc/sagetex/. To recompile it (and get the indices) : in your installation, you have: (sage-sh) charpent@zen-book-flip:~$ ls \$SAGE_ROOT/local/share/texmf/tex/latex/sagetex/
CONTRIBUTORS        py-and-sty.dtx       sagetex.dtx      scripts.dtx
example.tex     remote-sagetex.dtx       sagetex.ins
extractsagecode.py  remote-sagetex.py        sagetexparse.py
makestatic.py       run-sagetex-if-necessary.py  sagetex.sty


Compiling sagetex.dtx with pdflatex (in a scratch directory) will give you instructions on how to compile both Sagetex and its documentation. More details here.

This cannot be done automatically in Sage's installation, because it requires a (not inconsequential) functional installation of LaTeX, which is not a Sage dependency.

The fie example.tex is also quite instructive... Fiddle with it a bit.

HTH,

more

Thanks for your help. I do not really understand the third point. Now latex is telling me the following:"Run Sage on sage.sagetex.sage, and then run LaTeX on sage.tex again.".

( 2020-06-12 12:42:32 -0600 )edit

I suppose you named your LaTeX file sage.tex, right ? So just do what you're instructed to do :

• Step 1 : pdflatex sage.tex gives you :

• asage.pdf file with [??] marks in place of your Sage results, which are supposed to exist in files yet nonexistant.

• a sage.sagetex.sage file (as well as other files), which contain your Sage code (wrapped in execution instructions).

• Step 2 : sage sage.sagetex.sage will run this file through Sage, generating a bunch of .tex files containing your results.

• Step 3 pdflatex sage.tex will reconpile your tex file, where the references can now be obtained from the files created at step 2.

The new material added in step 2 may well change page, figure and table numbering, thus entailing further recompilation.

( 2020-06-12 16:26:33 -0600 )edit

Now I have the file .sagetex.sout: % This file was autogenerated from 2020-06-12-230428.sagetex.sage with % sagetex.py version 2019/01/09 v3.3 %b138f8a4fc7b23f763cd933974ea7272% md5sum of corresponding .sage file (minus "goboom", "current_tex_line", and pause/unpause lines)

( 2020-06-12 16:32:07 -0600 )edit

That has been generated by Step 2, and contains (references to) your Sage results. They are referenced by the sagetex.tex file (via the \sagexxx macroes), and ill be read during Step 3. Go ahead !

( 2020-06-12 16:46:26 -0600 )edit

Thank you for your patience. I do not really know how to do step 3. If I recompile the latex file nothing changes.

( 2020-06-12 17:19:53 -0600 )edit