I'm struggling to find a way to get SageTex and any of the commenting (comment or verbatim) packages to play nicely together. I'd like to be able to comment out a section of LaTeX code, containing SageTeX commands, so that nothing prints, but the Sage Code is run (but not printed) internally anyway.

Minimal Example:

I'd like the Sage Code in the following \LaTeX to be run:

So that here I get a 1: $\sage{foo}$.

Note that moving the sagesilent outside the comment block is not an acceptable solution for the following reason:
I'm working on a homework document with a several other colleagues, some without access to a sage server. Problems contain both sagesilent and regular \sage{} environments (which could not be moved outside for typographical reasons). My colleagues are happy to download the sout file after all final Sage changes have been made, but they would like to be able to print a select subset of the questions themselves as we go through problems at different paces. Regular (%%) commenting out of the problem, without rerunning the resulting sage documents, just results in the sage results within the document getting scrambled (since SageTex seems to give variables its own numbers, and removing a variable just causes the output for that variable to shift to the next variable in the document.)

One hint: I'm perhaps naively hopeful that this is possible, since the exam class itself plays quite nicely with SageTex. In particular, whether or not solutions are set to print in an exam file, the same sout file works.

1 Answer 1


This is too long for a comment, so I'm posting it as an answer:

I'm not sure I understand this question. According to the documentation on page 9, the sagesilent command is the command to run Sage in the background without it typesetting the code. The documentation also suggests (page 10-11) that when sharing with people who don't have access to Sage: "The easiest way is to simply include with your document the following files:

  1. sagetex.sty
  2. the generated .sout file
  3. the sage-plots-for-(filename).tex directory and its contents

As long as sagetex.sty is available, your document can be typeset using any reasonable LaTeX system. Since it is very common to include graphics files with a paper submission, this is a solution that should always work."

The documentation proposes another alternative as well that you can look into (makestatitc.py) but Sagemath Cloud seems like an even better option as it allows everyone to use Sage/LaTeX while collaborating on documents. It's free, too.

  • Sorry if I was unclear. Yes, I've used the method you suggest to share code, but thanks. What I would like to be able to do (assuming it's possible) is run latex code with some part of it "commented out" so it doesn't print, but run, so that sage gets any commands it needs from inside a "commented out " section. While sharing the document a certain way (so that sections can be removed without having to update the sout file) is the motivation, my real question is: how can I comment out the above "commented" section and still pass the definition of foo (and 2+2) to Sage?
    – Mathprof
    Aug 30, 2016 at 20:33
  • I don't think that it's possible but I don't know that for sure. Sagemath Cloud seems to be the most natural way to collaborate on a LaTex document involving SageTex.
    – DJP
    Aug 30, 2016 at 20:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .