3

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}
  • 1
    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. – David Carlisle Dec 14 '15 at 23:53
  • 1
    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) – David Carlisle Dec 15 '15 at 0:07
  • 1
    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. – Arun Debray Dec 15 '15 at 0:35
  • 1
    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. – barbara beeton Dec 15 '15 at 15:11
  • 1
    glad to be of help. there was a tugboat article a while back that you might also find interesting. – barbara beeton Dec 15 '15 at 15:46
4

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.

But if you want to make your code typeset even nicer, then you should consider copying the tex file of William Stein--the driving force behind Sage. Download the paper (www.wstein.org/papers/icms/stein-icms2010.tar.bz2), extract the contents and run it through LaTeX to get the output of his paper shown below: enter image description here

If you look at the right panel you'll see, under the image of the car, the Sage code with code spruced up. Check the preamble of the file and find out how he used \lstdefinelanguage{Sage}[]{Python} enter image description here

You can start with his paper preamble and modify his code to suit your needs. I don't see, however, a way to make the curved single quotes straight.

  • Thanks! Btw I learned a simple way to make single quotes straight that I include here: you need to add \usepackage{upquote} in the preamble and that takes care of the rest! – Simon Dec 16 '15 at 4:09
  • Thanks for the information; I wasn't fond of the curved single quote either. – DJP Dec 16 '15 at 15:25

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.