SageTeX / example.tex

Dan Drake 23d5b5b 
Dan Drake 6041417 





Dan Drake 23d5b5b 

Dan Drake 3193b70 
Dan Drake 73c02af 
Dan Drake 09cb064 


Dan Drake fe048fe 
Dan Drake 8594da7 
Dan Drake 6ab22f4 


Dan Drake fd22f76 

Dan Drake 23d5b5b 
Dan Drake 258ea8b 

Dan Drake 23d5b5b 




Dan Drake fe048fe 
Dan Drake 22c6700 



Dan Drake 23d5b5b 


Dan Drake 53b3a63 
Dan Drake 23d5b5b 





Dan Drake cf2fef5 
Dan Drake 23d5b5b 
Dan Drake cf2fef5 

Dan Drake 23d5b5b 
Dan Drake cf2fef5 
Dan Drake 23d5b5b 






Dan Drake aaf8ee3 
Dan Drake 8594da7 
Dan Drake 23d5b5b 











Dan Drake 8594da7 

Dan Drake 23d5b5b 









Dan Drake b0d5d03 
Dan Drake 23d5b5b 
Dan Drake b0d5d03 
Dan Drake 23d5b5b 







Dan Drake b0d5d03 

Dan Drake 23d5b5b 
Dan Drake b0d5d03 
Dan Drake 23d5b5b 








Dan Drake 258ea8b 
Dan Drake 23d5b5b 


















Dan Drake 29145fc 

Dan Drake 23d5b5b 
Dan Drake 29145fc 
Dan Drake 23d5b5b 
Dan Drake b6245b8 
Dan Drake 258ea8b 
Dan Drake b6245b8 

Dan Drake 23d5b5b 






Dan Drake 1ba3b09 










Dan Drake 23d5b5b 













Dan Drake 7d7c7f3 
Dan Drake 23d5b5b 
Dan Drake fd22f76 
Dan Drake 23d5b5b 
Dan Drake 7d7c7f3 
Dan Drake 23d5b5b 
Dan Drake 0e28ced 


Dan Drake ebe52a5 
Dan Drake 09cb064 




































Dan Drake 13dab5d 










Dan Drake 09cb064 
Dan Drake 23d5b5b 

Dan Drake 911609d 







Dan Drake 23d5b5b 
Dan Drake 13dab5d 





Dan Drake 911609d 



Dan Drake c7c7fe6 
Dan Drake 13dab5d 

Dan Drake 23d5b5b 




Dan Drake 29145fc 

Dan Drake 13dab5d 
Dan Drake 09cb064 
Dan Drake 0e28ced 





















Dan Drake 6041417 
Dan Drake 6ab22f4 




Dan Drake 13dab5d 


Dan Drake 6ab22f4 


Dan Drake 09cb064 
Dan Drake 13dab5d 

Dan Drake 09cb064 
Dan Drake 13dab5d 
Dan Drake 09cb064 
Dan Drake 13dab5d 

Dan Drake 09cb064 
Dan Drake 13dab5d 
Dan Drake 09cb064 

Dan Drake 13dab5d 
Dan Drake 09cb064 
Dan Drake 13dab5d 

Dan Drake 09cb064 
Dan Drake 13dab5d 

Dan Drake 6ab22f4 









Dan Drake 1ba3b09 
Dan Drake 6ab22f4 

Dan Drake 1ddaf75 








Dan Drake b7d443b 

Nicolas M. Thiér… 1323b7d 


Dan Drake 1ddaf75 
Nicolas M. Thiér… 1323b7d 
Dan Drake b7d443b 




Nicolas M. Thiér… 1323b7d 
Dan Drake 1ddaf75 





Dan Drake b7d443b 
Dan Drake 1ddaf75 



Nicolas M. Thiér… 1323b7d 




Dan Drake 1ddaf75 



Dan Drake b7d443b 



Dan Drake 1ddaf75 
Dan Drake b7d443b 






Nicolas M. Thiér… 1323b7d 

Dan Drake 5036e7f 
Nicolas M. Thiér… 1323b7d 

Dan Drake 5036e7f 
Nicolas M. Thiér… 1323b7d 
Dan Drake 5036e7f 

Nicolas M. Thiér… 1323b7d 


Dan Drake 5036e7f 




Dan Drake 1ddaf75 

Dan Drake b7d443b 



Dan Drake 1ddaf75 






Dan Drake b7d443b 

Dan Drake 1ddaf75 

Nicolas M. Thiér… 1323b7d 
Dan Drake b7d443b 
Dan Drake 1ddaf75 

Dan Drake 23d5b5b 
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
% 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 and the documentation of the SageTeX package for
% more information!

\documentclass{article}
\title{Examples of embedding Sage in \LaTeX{} with \textsf{Sage\TeX}}
\author{Dan Drake and others}
\usepackage{hyperref}
\usepackage{tkz-berge}

\usepackage{sagetex}
%
% If you want SageTeX to use Imagemagick's `convert' utility to make eps
% files from png files when generating a dvi file, add the "imagemagick"
% option above:
%
%    \usepackage[imagemagick]{sagetex}

\setlength{\sagetexindent}{10ex}

\begin{document}
\maketitle

\section{Inline Sage, code blocks}

This is an example $2+2=\sage{2+2}$. If you raise the current year mod
$100$ (which equals $\sage{mod(\the\year, 100)}$) to the power of the
current day ($\the\day$), you get $\sage{Integer(mod(\the\year,
100))^\the\day}$. Also, $\the\year$ modulo $42$ is $\sage{\the\year
\percent 42}$.

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

Solutions of $\mbox{eqn}=\sage{eqn}$:
\[
\sage{s[0]}
\]
\[
\sage{s[1]}
\]

Now we evaluate the following block:
\begin{sageblock}
E = EllipticCurve("37a")
\end{sageblock}
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.
\begin{sageblock}
try:
    E = load('E2')
except IOError:
    E = EllipticCurve([1,2,3,4,5])
    E.anlist(100000)
    E.save('E2')
\end{sageblock}

The 9999th Fourier coefficient of $\sage{E}$ is
$\sage{E.anlist(100000)[9999]}$.

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

\begin{sageblock}
  var('x')
  f(x) = log(sin(x)/x)
\end{sageblock}
The Taylor Series of $f$ begins: $\sage{ f.taylor(x, 0, 10) }$.

\section{Plotting}

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

\sageplot{E.plot(-3,3)}

\begin{sagesilent}
  # the var line is unecessary unless you've defined x to be something
  # other than a symbolic variable
  var('x')
  f(x) = -x^3+3*x^2+7*x-4
\end{sagesilent}

You can use variables to hold plot objects and do stuff with them.
\begin{sageblock}
  p = plot(f, x, -5, 5)
\end{sageblock}

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

\begin{center} \sageplot[scale=.2]{p} \end{center}

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.
\begin{sageblock}
f = maxima('sin(x)^2*exp(x)')
g = f.integrate('x')
\end{sageblock}
Plot $g(x)$, but don't typeset it.
\begin{sagesilent}
  # g is a Maxima thingy, it needs to get converted into a Sage object
  plot1 = plot(g.sage(),x,-1,2*pi)
\end{sagesilent}

You can specify a file format and options for \verb|includegraphics|.
The default is for EPS and PDF files, which are the best choice in
almost all situations. (Although see the section on 3D plotting.)

\sageplot[angle=45, width=.5\textwidth][png]{plot1}

If you use regular \verb|latex| to make a DVI file, you'll see a box,
because DVI files can't include PNG files. If you use \verb|pdflatex|
that will work. See the documentation for details.

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

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

To fiddle with aspect ratio, first save the plot object:

\begin{sageblock}
  p = plot(x, 0, 1) + circle((0,0), 1)
  p.set_aspect_ratio(1)
\end{sageblock}

Now plot it and see the circular circle and nice 45 degree angle:

\sageplot[scale=.33]{p}

Indentation and so on works fine.
\begin{sageblock}
 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')
\end{sageblock}
And here's the picture:

\sageplot{matrixprogram}

Reset \texttt{x} in Sage so that it's not a generator for the polynomial
ring: \sage{var('x')}


\subsection{Plotting (combinatorial) graphs with TikZ}
\label{sec:plotting-graphs-with}

Sage now includes some nice support for plotting graphs using
\href{http://www.texample.net/tikz/}{TikZ}. Here, we mean things with
vertices and edges, not graphs of a function of one or two variables.

First define our graph:

\begin{sageblock}
  g = graphs.PetersenGraph()
  g.set_latex_options(tkz_style='Art')
\end{sageblock}

Now just do \verb|\sage{}| on it to plot it. You'll need to use the
\href{http://altermundus.com/pages/graphtheory.html}{\texttt{tkz-berge}}
package for this to work; that package in turn depends on
\href{http://altermundus.com/pages/graph.html}{\texttt{tkz-graph}} and
TikZ. See
\href{http://sagemath.org/doc/reference/sage/graphs/graph_latex.html}{``\LaTeX{}
  Options for Graphs''} in the Sage reference manual for more details.

\begin{center}
  \sage{g}
\end{center}

The above command just outputs a \texttt{tikzpicture} environment, and
you can control that environment using anything supported by
TikZ---although the output of \verb|\sage{g}| explicitly hard-codes a
lot of things and cannot be flexibly controlled in its current form.

\tikzstyle{every picture}=[rotate=45, scale=1/2]

\begin{center}
  \sage{g}
\end{center}

Here's some more graphs, plotted using the usual plot routines.

\sageplot[scale=.5]{graphs.FlowerSnark().plot()}

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

\sageplot[scale=.5]{G4plot, axes=False}

\subsection{3D plotting}

3D plotting right now (Sage version 4.3.4) is problematic because
there's no convenient way to produce vector graphics. We can make PNGs,
though, so if you pass \verb|sageplot| a graphics object that cannot be
saved to EPS or PDF format, we will automatically save to a PNG file,
which can be used when typesetting a PDF file, but not when creating a
DVI file. However, you can specify the ``\texttt{imagemagick}'' option,
which will use the Imagemagick \texttt{convert} utility to make EPS
files. See the documentation for details.

% FIXME: not sure this works with remote sagetex

\begin{sagesilent}
  x, y = var('x y')
\end{sagesilent}

Here's a 3D plot whose format we do not specify; it will automatically
get saved as a PNG file and won't work when using \texttt{latex} to make
a DVI file.

\sageplot[scale=.5]{plot3d(sin(pi*(x^2+y^2))/2,(x,-1,1),(y,-1,1))}

Here's the (perhaps-not-so-) famous Sage cube graph in 3D.

\begin{sageblock}
  G = graphs.CubeGraph(5)
\end{sageblock}

% need empty [] so sageplot knows you want png format, and aren't
% passing an option to includegraphics
\sageplot[][png]{G.plot3d()}

\section{Pausing Sage\TeX}
\label{sec:pausing-sagetex}

Sometimes you want to ``pause'' for a bit while writing your document if
you have embedded a long calculation or just want to concentrate on the
\LaTeX{} and ignore any Sage stuff. You can use the \verb|\sagetexpause|
and \verb|\sagetexunpause| macros to do that.

\sagetexpause

A calculation: $\sage{factor(2^325 + 1)}$ and a code environment that
simulates a time-consuming calculation. While paused, this will get
skipped over.
\begin{sageblock}
  import time
  time.sleep(15)
\end{sageblock}

Graphics are also skipped: \sageplot{plot(2*sin(x^2) + x^2, (x, 0, 5))}

\sagetexunpause

\section{Make Sage write your \LaTeX{} for you}

With \textsf{Sage\TeX}, you can not only have Sage do your math for you,
it can write parts of your \LaTeX{} document for you! For example, I
hate writing \texttt{tabular} environments; there's too many fiddly
little bits of punctuation and whatnot\ldots and what if you want to add
a column? It's a pain---or rather, it \emph{was} a pain. Just write a
Sage/Python function that outputs a string of \LaTeX{} code, and use
\verb|\sagestr|. Here's how to make Pascal's triangle.

\begin{sageblock}
def pascals_triangle(n):
    # start of the table
    s  = [r"\begin{tabular}{cc|" + "r" * (n+1) + "}"]
    s.append(r"  & & $k$: & \\")
    # second row, with k values:
    s.append(r"  & ")
    for k in [0..n]:
        s.append("& {0} ".format(k))
    s.append(r"\\")
    # the n = 0 row:
    s.append(r"\hline" + "\n" + r"$n$: & 0 & 1 & \\")
    # now the rest of the rows
    for r in [1..n]:
        s.append(" & {0} ".format(r))
        for k in [0..r]:
            s.append("& {0} ".format(binomial(r, k)))
        s.append(r"\\")
    # add the last line and return
    s.append(r"\end{tabular}")
    return ''.join(s)

# how big should the table be?
n = 8
\end{sageblock}

Okay, now here's the table. To change the size, edit \texttt{n} above.
If you have several tables, you can use this to get them all the same
size, while changing only one thing.

\begin{center}
  \sagestr{pascals_triangle(n)}
\end{center}

\section{Include doctest-like examples in your document}

Here are some examples of using the \texttt{sageexample} environment:
\begin{sageexample}
  sage: 1+1
  2
  sage: factor(x^2 + 2*x + 1)
  (x + 1)^2
\end{sageexample}
If you want to see the plain-text output as well as the typeset output,
renew the \texttt{sageexampleincludetextoutput} command to True:
\begin{verbatim}
  \renewcommand{\sageexampleincludetextoutput}{True}
\end{verbatim}
\renewcommand{\sageexampleincludetextoutput}{True}
This can be useful to check that the two outputs are consistent.

When this environment is near the bottom of the page, it may look like
the page number is the output of a command, when in fact the real output
is on the next page. If the output of a command below looks like
\thepage, don't worry, that's just the page number.

\begin{sageexample}
  sage: 1+1
  2
  sage: factor(x^2 + 2*x + 1)
  (x + 1)^2
\end{sageexample}
Multiline statements are supported, as are triple-quoted strings
delimited by single quotes:
\renewcommand{\sageexampleincludetextoutput}{False}
\begin{sageexample}
  sage: def f(a):
  ...       '''This function is really quite nice,
  ...       although perhaps not very useful.'''
  ...       print "f called with a = ", a
  ...       y = integrate(SR(cyclotomic_polynomial(10)) + a, x)
  ...       return y + 1
  sage: f(x)
  f called with a =  x
  1/5*x^5 - 1/4*x^4 + 1/3*x^3 + x + 1
\end{sageexample}
Note that the ``$f$ called with\ldots'' stuff doesn't get typeset, since
when running Sage on \texttt{example.sage}, that gets printed to the
terminal.

When typesetting your document, the validity of the outputs is not
checked. In fact, the provided outputs are completely ignored:
\renewcommand{\sageexampleincludetextoutput}{True}
\begin{sageexample}
  sage: is_prime(57)
  toothpaste
\end{sageexample}
\renewcommand{\sageexampleincludetextoutput}{False}

However, typesetting your document produces a file named
\texttt{example\_doctest.sage} containing all the doctest-like examples,
and you can have Sage check them for you with:
\begin{verbatim}
  $ sage -t  example_doctest.sage
\end{verbatim}
You should get one doctest failure from the ``toothpaste'' line above.

Please look into this file for the original line numbers.

Beware that \texttt{sage -t} does not handle well file names with
special characters in them, particularly dashes, dots, and spaces---this
ultimately comes from the way Python interprets \texttt{import}
statements. Also, running doctests on files outside the main Sage
library does not always work, so contact \texttt{sage-support} if you
run into troubles.

Some more examples. This environment is implemented a little bit
differently than the other environments, so it's good to make sure that
definitions are preserved across multiple uses. This will correctly
define $a$, but not print its output because the statement is made up of
a sequence of expressions.
\begin{sageexample}
  sage: 1; 2; a=4; 3; a
  1
  2
  3
  4
\end{sageexample}
After that, Sage should remember that $a = \sage{a}$ and be able to use
that in future \texttt{sageexample} blocks:
\begin{sageexample}
  sage: f(a)
  f called with a =  4
  1/5*x^5 - 1/4*x^4 + 1/3*x^3 - 1/2*x^2 + 5*x + 1
\end{sageexample}

\end{document}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.