# Questions about including source code in a TeX file

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:

1. When my code contained something like $2^3$ and I entered it as \texttt{2^3} I got error messages.
2. 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}

• the question is not really very clear about what output you expect. If you are not looking for syntax highlighting then \verb|x^2| for inline fragments or \begin{verbatim}... \end{verbatim} for a code block. Dec 14 '15 at 23:53
• listings allows (simple) customisation of keywords so you could set up some sage highlighting probably but I know nothing about sage, verbatim is built in to latex and just does straight typewritter emulation. The other main syntax highlighter package is minted (which uses the external python pygments library, so if there is a pygments setup for sage you could use that) Dec 15 '15 at 0:07
• I'd definitely recommend the listings package for longer code blocks; see this post for some ideas about how to highlight it nicely. As for straight quotes in listings, see this TeX.SX post. Dec 15 '15 at 0:35
• perhaps this is overkill, but the package sagetex does provide a command explicitly for typesetting sage code in a latex document. look at the sagetex documentation. Dec 15 '15 at 15:11
• glad to be of help. there was a tugboat article a while back that you might also find interesting. Dec 15 '15 at 15:46

If you want to use Sage code in a LaTeX document then you should use the sagetex package. According to this CTAN document, sagetex has several verbatim like environments. It sounds like you want the sageverbatim environment. In using this environment, "... whatever you type will be typeset, but not written into the .sage file. This allows you to typeset psue-docode, code that will fail, or take too much time to execute, or whatever". Using the built in environment gives you access to \sagetexindent, which lets you control the indentation of the code.