1

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,
commentstyle={\ttfamily\color{green}},
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}

enter image description here

2
  • 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

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

\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}
2
  • 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 .