# How to display the output of a loop in SageTex

I am preparing a class on Sage, and i want the following code on my document.

\documentclass{article}
\usepackage{sagetex}

\begin{document}

\begin{sagecommandline}
sage: for i in range(0,4):
....:     i^2
\end{sagecommandline}

\end{document}


However, i want it to display the output as it does in a Sage worksheet.

Working with your code, I'd try something like this:

\documentclass{article}
\usepackage{sagetex}
\lstdefinestyle{SageInput}{style=DefaultSageInput, numbers=none}
\begin{document}
\begin{sagecommandline}
sage: for i in range(0,4):
....:     i^2
\end{sagecommandline}
\begin{sagesilent}
output = r""
for i in range(0,4):
output += r"\texttt{%s}\\"%(i^2)
\end{sagesilent}
\sagestr{output}
\end{document}


The output running in Cocalc is:

The code numbers=none turns off the line numbers, if that's something you wanted.

You might also be interested to know that my answer to the question Questions about including source code in a TeX file mentions that one of the developers of SAGE has posted a LaTeX file that he uses in typesetting SAGE.