I am trying to use pythontex for SAGE computations inside a .tex document. I am aware that there exists a sagetex engine which allows for exactly my use case, but pythontex's concept of sessions is specially useful for me (some computations are very long, but don't change often, so splitting into sessions makes sense for me).

So far, it is easy to convince pythontex to use SAGE's python by running pythontex in this way:

pythontex --interpreter "python:sage -python" mainfile.tex

Question: SAGE offers a preparse(string) function to allow for extended syntax, e.g. the following is not legit python, but it is working SAGE:


How could I tell pythontex that I want to preparse inputs?


% Run with: pythontex --interpreter "python:sage  -python" main
# This works, I am using SAGE's python.
from sage.all import *
# I want to apply preparse() to every pycode:
s=preparse('R.<x,y> = QQ[]\na=1')
print "\\begin{verbatim}\n%s\\end{verbatim}" % s
  • 2
    There isn't a good way to add the SAGE preparsing without changing the pythontex internals. However, adding SAGE support to pythontex should be simple; probably only a few lines of code are needed. I've actually been waiting for someone to request SAGE support. I will see about adding SAGE support in the next day or so.
    – G. Poore
    Mar 13 '15 at 15:57
  • @G.Poore that'd be great, thanks. While you're at it, can you say if it is possible to have sage in console mode?
    – Koji
    Mar 14 '15 at 14:14
  • It should be possible to have a SAGE console mode, but it may take a while. I can add a code mode for SAGE very quickly, because it's all based on templates. Adding console support will take longer, because I need to rewrite the current console handling to remove a number of limitations and make it easier to extend to other languages. The new console handling is already scheduled for the next release, but I don't have an estimate of when that will actually be.
    – G. Poore
    Mar 14 '15 at 16:24
  • @G.Poore thanks for the details. I considered writing a sage template myself, but I didn't find an easy way to add templates without modifying the distributed python files. Perhaps you could consider an "extensions" mechanism in the future? For what's worth, I'd be happy to help...
    – Koji
    Mar 15 '15 at 9:45
  • 1
    I'm interested in adding an extension mechanism, but have been waiting until I've added enough additional languages to have a good idea of what that mechanism should look like. I will probably try to start putting together an extension mechanism over the summer or in the fall.
    – G. Poore
    Mar 20 '15 at 17:46

I've added SAGE support to the development version of pythontex at GitHub. Everything is preparsed. If you download the latest version, and load pythontex with usefamily=sage, you will have access to sagecode, sageblock, and sageverbatim environments, with corresponding commands, plus a \sage command. By default, the \sage command sends its argument through the latex() function, so that you get formatted output. This can be changed by modifying the utilities class formatting function, pytex.formatter(). In the future, I may allow the utilities class to be accessed as either sagetex or pytex, but I'm sticking with pytex for now to avoid confusion with the sagetex package. If you encounter any bugs or missing features, please open an issue at GitHub.

I will look into adding SAGE console emulation once I get around to refactoring the console system so that it may be extended for other languages.

Here's a simple example document.




f = 1 - sin(x)^2

\[ \sage{f.simplify_trig()} \]

\[ \sage{f(x=pi/2)} \]

\[ \sage{integrate(f, x).simplify_trig()} \]

  • 1
    Thanks a lot for the work you put in; I just looked briefly at your github commits, and it seems like some substantial work went into this.
    – Koji
    Mar 20 '15 at 19:24
  • I get an error though, and it is too long to include in the comments. How may I report it?
    – Koji
    Mar 20 '15 at 19:25
  • 1
    @Koji I've opened an issue for refining the SAGE support at github.com/gpoore/pythontex/issues/63. You can post your code and error messages there.
    – G. Poore
    Mar 20 '15 at 19:59
  • For the record: The following information was provided by @G.Poore at the above link: you need sage 6.5 or else add \setpythontexoutputdir{pythontex_files_new} in your tex file. This is due to a bug in sage
    – Koji
    Mar 20 '15 at 21:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.