# How to change background color of a sagetex sageblock-environment?

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?

MWE:

\documentclass[]{article}
\usepackage{sagetex}
\usepackage{xcolor}

\lstset{language=Sage,
keywordstyle={\ttfamily\color{blue}\bfseries},
stringstyle ={\ttfamily\color{dgraycolor}\bfseries},
tabsize = 4,
basicstyle={\small \ttfamily},
backgroundcolor= \color{orange!20},
}

\begin{document}
The following function computes the square of a number:
\begin{sageblock}
def f(x):
return x**2
\end{sageblock}

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

Commandline Version:
\begin{sagecommandline}
sage: x = 2
sage: f(2)
\end{sagecommandline}

\end{document}


• (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 Mar 22 '18 at 12:52
• If you are unable to get help with your question, consider posting here. – DJP Mar 26 '18 at 1:44

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.

\documentclass{article}
\usepackage{sagetex}
\usepackage{tcolorbox}

\begin{document}

% colback: background colour

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

% verify that sageblock works

$h : \sage{h}$
\end{document}

• 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 Aug 2 '18 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. – Firmin Martin Aug 2 '18 at 10:39