Ask Your Question

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

asked 2020-06-12 16:40:34 +0200

Moondoggy gravatar image

updated 2020-06-12 18:26:27 +0200

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


edit retag flag offensive close merge delete


And your question is ?

Emmanuel Charpentier gravatar imageEmmanuel Charpentier ( 2020-06-12 18:20:36 +0200 )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.

Moondoggy gravatar imageMoondoggy ( 2020-06-12 18:25:42 +0200 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2020-06-12 19:16:42 +0200

Emmanuel Charpentier gravatar image

updated 2020-06-12 23:15:01 +0200

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  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.


edit flag offensive delete link 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.".

Moondoggy gravatar imageMoondoggy ( 2020-06-12 19:42:32 +0200 )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.

Emmanuel Charpentier gravatar imageEmmanuel Charpentier ( 2020-06-12 23:26:33 +0200 )edit

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

Moondoggy gravatar imageMoondoggy ( 2020-06-12 23:32:07 +0200 )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 !

Emmanuel Charpentier gravatar imageEmmanuel Charpentier ( 2020-06-12 23:46:26 +0200 )edit

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

Moondoggy gravatar imageMoondoggy ( 2020-06-13 00:19:53 +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


Asked: 2020-06-12 16:40:34 +0200

Seen: 414 times

Last updated: Jun 12 '20