Ask Your Question

Code highlighting and execution in sagetex

asked 2017-09-05 14:44:22 +0200

Is there a version of \begin{sageblock} my multiline code \end{sageblock} that has code highlighting and line numbering like the sagecommandline environment and code execution?

Of course typesetting with lstlistings and execution with sagesilent could be a workaround, but it would seem to require me to have the same code in my document twice. And that I find unacceptable.

edit retag flag offensive close merge delete



Recap of the various SageTeX commands and environments:

  • \sage{code}

    • code is executed
    • code is not displayed
    • output is inserted to be displayed by LaTeX
  • sageblock

    • code is executed
    • code is displayed verbatim, no syntax highlighting, no line numbers
    • output is not displayed
  • sagesilent

    • code is executed
    • code is not displayed
    • output is not displayed
  • sagecommandline

    • code lines with sage prompt are executed
    • code is displayed verbatim with syntax highlighting
    • output is inserted in the displayed code block
    • lines with no sage prompt are ignored when typesetting
    • lines with no sage prompt are useful for doctesting
    • line numbering is on by default, can be disabled
    • inserting of the output is on by default, can be disabled
slelievre gravatar imageslelievre ( 2018-03-21 15:51:58 +0200 )edit

With the straighforward lstlistings, you would get:

  • code is displayed verbatim, optionally with syntax highlighting and line numbering
  • code is not executed
slelievre gravatar imageslelievre ( 2018-03-21 15:53:03 +0200 )edit

1 Answer

Sort by » oldest newest most voted

answered 2017-09-06 03:54:41 +0200

slelievre gravatar image

updated 2018-03-21 13:12:16 +0200

Is sagecommandline what you are looking for?

I found it by searching the sagetex github repo for "highlighting":

Try this (from the documentation).

  sage: 1+1
  sage: factor(x^2 + 2*x + 1)
  (x + 1)^2

For function definitions or loops, use the continuation prompt ....: as follows:

  sage: def square(x):
  ....:     return x * x
  sage: square(12)
  sage: first_few_squares = []
  sage: for n in range(4):
  ....:     first_few_squares.append(square(n))
  sage: first_few_squares
  [0, 1, 4, 9]

To get rid of the output, change the value of sagecommandlinetextoutput:


The documentation of SageTeX and its example file have a lot of information.

One comment in the example file is:

The Sage input and output is typeset using the listings package with the styles SageInput and SageOutput, respectively. If you don't like the defaults you can change them.

and there is a recommendation on how to change the defaults if you want to.

Another wonderful feature (which justifies including the sage: prompt) is

You can include output, but it will be ignored. This is useful for doctesting, as all the sagecommandline environment things get put into the _doctest.sage file.

The SageTeX documentation and example file are worth reading and studying!

If you have Sage installed and if /path/to/sagedir is the directory where it is installed, open a terminal and run the following:

cd $SAGE_ROOT/local/share/texmf/tex/latex/sagetex
export TEXINPUTS="$SAGE_ROOT/local/share/texmf//:"
pdflatex example.tex
sage example.sagetex.sage
pdflatex example.tex
open example.pdf
edit flag offensive delete link more


It is a partial workaround, but does not solve the original question, really. The sage: is a bit in the way, and this doesn't seem to support block code like function definitions or loops. Sorry for not noticing your answer earlier, I haven't logged in in a while.

Björn gravatar imageBjörn ( 2018-03-21 08:39:50 +0200 )edit

I edited my answer to address your comment.

slelievre gravatar imageslelievre ( 2018-03-21 13:11:50 +0200 )edit

I had a similar problem, wore which I proposed a workaround, thar you my find useful.

Emmanuel Charpentier gravatar imageEmmanuel Charpentier ( 2018-03-21 21:01: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



Asked: 2017-09-05 14:44:22 +0200

Seen: 649 times

Last updated: Mar 21 '18