When using sagetex the background of sagecommandline environments can be easily changed via lstlistings setup.

How can the background of a sageblock environment be changed?



stringstyle ={\ttfamily\color{dgraycolor}\bfseries},
tabsize = 4,
basicstyle={\small \ttfamily},
backgroundcolor= \color{orange!20},

The following function computes the square of a number:
def f(x):
    return x**2

Testing in the code gives for $x=2$ the value \sage{f(2)}.

Commandline Version:
sage: x = 2
sage: f(2)


enter image description here

  • 1
    (1) welcome, (2) not everyone knows what sagetex or a sageblock is, so please provide a full minimal example, otherwise it is rather hard to help
    – daleif
    Commented Mar 22, 2018 at 12:52
  • If you are unable to get help with your question, consider posting here.
    – DJP
    Commented Mar 26, 2018 at 1:44

1 Answer 1


You can use the tcolorbox package to achieve this. Change colback to the background colour you want. tcolorbox provide much more options (title, frame colour etc.), see the documentation for those details.

rendered pdf



% colback: background colour

\begin{tcolorbox}[colback=orange!20, enhanced, arc=0pt, frame hidden]
    f(x) = sin(x) - 1
    g(x) = log(x)
    h(x) = diff(f(x) * g(x), x)

% verify that sageblock works

$h : \sage{h}$
  • 1
    This answer is a good recommendation, but maybe you want to set enhanced, arc=0pt, frame hidden (requires library skins) to make it look like the other environment in the OP.
    – TeXnician
    Commented Aug 2, 2018 at 8:46
  • @TeXnician Yes, you're right, just added. I also tried to do code-highlighting with minted/listings (for python/sage) but it's far more complicated than I thought since sage needs to evaluate expressions in sageblock environment. Commented Aug 2, 2018 at 10:39

You must log in to answer this question.

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