SageTeX / example.tex

Full commit
% General example LaTeX file for including Sage calculations and plots
% Build with: (pdf)latex example.tex; sage example.sage; pdflatex example.tex 
% Please read README.txt and the documentation of the SageTeX package
% for more information!


\title{Examples of embedding Sage in \LaTeX}


\section{Inline Sage, code blocks}

This is an example $2+2=\sage{2+2}$. If you raise the current year
($\the\year$) to the power of the current day ($\the\day$), you get

Code block which uses a variable \texttt{s} to store the solutions:
 eqn = [a+b*c==1, b-a*c==0, a+b==5]
 s = solve(eqn, a,b,c)

Solutions of $\mbox{eqn}=\sage{eqn}$:

Now we evaluate the following block:
E = EllipticCurve("37a")
You can't do assignment inside \verb|\sage| macros, since Sage doesn't
know how to typeset the output of such a thing. So you have to use a
code block. The elliptic curve $E$ given by $\sage{E}$ 
has discriminant $\sage{E.discriminant()}$. 

You can do anything in a code block that you can do in Sage and/or
Python. Here we save an elliptic curve into a file.
    E = load('E2')
except IOError:
    E = EllipticCurve([1,2,3,4,5])

The 9999th Fourier coefficient of $\sage{E}$ is 

The following code block doesn't appear in the typeset file\dots
  e = 2
  e = 3*e + 1
but we can refer to whatever we did in that code block: $e=\sage{e}$.

  f = log(sin(x)/x)
The Taylor Series of $f$ is: $\sage{ f.taylor(x, 0, 10) }$.


Here's a plot of the elliptic curve $E$.



You can use variables to hold plot objects and do stuff with them.
  p = plot(f, x, -5, 5)

Here's a small plot of $f$ from $-5$ to $5$, which I've centered:


On second thought, use the default size of $3/4$ the \verb|\textwidth|
and don't use axes:

\sageplot{p, axes=False}

Remember, you're using Sage, and can therefore call upon any of the
software packages Sage is built out of.
f = maxima('sin(x)^2*exp(x)')
g = f.integrate('x')
Plot $g(x)$, but don't typeset it.
  # g is a Maxima thingy, it needs to get converted into a Sage object
  plot1 = plot(g.sage(),x,-1,2*pi)

You can specify a file format and options for \verb|includegraphics|.
Observe that if you are producing a DVI file from this, it will
\emph{always} complain about a missing file, since regular \verb|latex|
doesn't do PNGs. The default is for EPS and PDF files, which are the
best choice in almost all situations.

\sageplot[angle=45]{plot1, format='png'}

When using \verb|\sageplot|, you can pass in just about anything that
Sage can call \verb|.save()| on to produce a graphics file:

\sageplot{plot1 + plot(f.sage(),x,-1,2*pi,rgbcolor=hue(0.4)), figsize=[1,2]}


G4 = DiGraph({1:[2,2,3,5], 2:[3,4], 3:[4], 4:[5,7], 5:[6]}, multiedges=True)
G4plot = G4.plot(layout='circular')

\sageplot{G4plot, axes=False}

Indentation and so on works fine.
 s     = 7
 s2    = 2^s
 P.<x> = GF(2)[]
 M     = matrix(parent(x),s2)
 for i in range(s2):
    p  = (1+x)^i
    pc = p.coeffs()
    a  = pc.count(1)
    for j in range(a):
        idx        = pc.index(1)
        M[i,idx+j] = pc.pop(idx)

 matrixprogram = matrix_plot(M,cmap='Greys')
And here the picture:


\subsection{3D plotting}

3D plotting right now is problematic because there's no way to produce
vector graphics. We can make PNGs, though. If you don't specify
anything, right now you get Targa files, which are huge.

Here's the famous Sage cube graph:

  G = graphs.CubeGraph(5)

\sageplot{G.plot3d(), format='png'}