I'd like to include some Sage code in a TeX file. Originally I thought the right way would be to use $\texttt{}$ whenever I had to include the source code, but then I ran into some difficulties:
- When my code contained something like
$2^3$and I entered it as\texttt{2^3}I got error messages. - Whenever I needed straight single quotes I got curved ones.
I managed to solve the first issue by typing \texttt{2\textasciicircum 3}, but didn't find a fix for the second one, so I started searching the web. This way I came to know listing environment that I'd never used before. Since Sage is not listed as a language for listing, I entered Python instead, however the spacing is not right and the output does not look like that of \texttt. So I wonder:
what is the right way of entering Sage code in LaTeX? I apologize if this question has already been answered , but I couldn't locate it myself.
\documentclass{article,amsmath, amssymb}
\usepackage{listings}
\begin{document}
\lstset{language=Python}
\begin{lstlisting}
sqrt(9)
\end{lstlisting}
\end{document}


\verb|x^2|for inline fragments or\begin{verbatim}...\end{verbatim}for a code block. – David Carlisle Dec 14 '15 at 23:53listingspackage for longer code blocks; see this post for some ideas about how to highlight it nicely. As for straight quotes inlistings, see this TeX.SX post. – Arun Debray Dec 15 '15 at 00:35sagetexdoes provide a command explicitly for typesetting sage code in a latex document. look at thesagetexdocumentation. – barbara beeton Dec 15 '15 at 15:11