1

I'd like to put sage code in my LaTeX document that looks mostly like how an interactive session would appear, except that I want LaTeX formatted output.

So, I'd like to write something like this in my LaTeX file:

\begin{sageblock}
gcd(x^2 + 2*x + 1, x + 1)
\end{sageblock}

and the result is first a Courier-typeset box containing

sage: gcd(x^2 + 2*x + 1, x + 1)

and then a box after that containing the LaTeX formatted result.

$$ x+1 $$

Any idea how to do this, without assigning the result to an auxiliary variable and then printing the variable?

1

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}.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.