The sageexample
environment
The sageexample
environment does exactly that (except the sage:
prompt needs to be added by hand).
Create a latex file gcd.tex
containing
\documentclass{article}
\usepackage{sagetex}
\begin{document}
\begin{sageexample}
sage: gcd(x^2 + 2*x + 1, x + 1)
\end{sageexample}
\end{document}
then in a terminal run
pdflatex gcd && sage gcd.sagetex.sage && pdflatex gcd
and a pdf will be produced containing
sage : gcd(x^2 + 2*x + 1, x + 1)
x + 1
(where x + 1 is typeset).
The sagecommandline
environment
Note the related sagecommandline
environment
which leaves the output in non-typeset form,
and is good for running doctests.
Create a latex file gcd.tex
containing
\documentclass{article}
\usepackage{sagetex}
\begin{document}
\begin{sagecommandline}
sage: gcd(x^2 + 2*x + 1, x + 1)
\end{sagecommandline}
\end{document}
then in a terminal run
pdflatex gcd && sage gcd.sagetex.sage && pdflatex gcd
and a pdf will be produced containing
sage : gcd(x^2 + 2*x + 1, x + 1)
x + 1
with additional line numbering. To remove the line numbering,
add the line
\lstdefinestyle{SageInput}{style=DefaultSageInput,numbers=none}
somewhere before \begin{sagecommandline}
.