# HG changeset patch
# User Dan Drake
# Date 1240284960 32400
# Node ID 6ab22f46623e66b057bb1fda2b9894ffbbb09013
# Parent 55002e02468ebf0fd3de8d67616d356742b944a5
Small fixes; no functionality changes. Added examples.
Obfuscated email, fixed up sys.exit, discovered that the \thepage business
is even trickier than I thought.
diff git a/example.tex b/example.tex
 a/example.tex
+++ b/example.tex
@@ 6,12 +6,12 @@
\documentclass{article}
\title{Examples of embedding Sage in \LaTeX{} with \textsf{Sage\TeX}}
\author{Dan Drake and others}

+\usepackage{amsmath}
\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:
+% 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}
@@ 185,4 +185,49 @@
% passing an option to includegraphics
\sageplot[][png]{G.plot3d(engine='tachyon')}
+\subsection{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 painor rather, it \emph{was} a pain. Here's how to
+make Pascal's triangle. It requires the \texttt{amsmath} package because
+of what Sage does when producing a \LaTeX{} representation of a string.
+(It puts it inside a \verb\text macro.)
+
+\begin{sageblock}
+def pascals_triangle(n):
+ # start of the table
+ s = r"\begin{tabular}{cc" + "r" * (n+1) + "}"
+ s += r" & & $k$: & \\"
+ # second row, with k values:
+ s += r" & "
+ for k in [0..n]:
+ s += "& %d " % k
+ s += r"\\"
+ # the n = 0 row:
+ s += r"\hline" + "\n" + r"$n$: & 0 & 1 & \\"
+ # now the rest of the rows
+ for r in [1..n]:
+ s += " & %d " % r
+ for k in [0..r]:
+ s += "& %d " % binomial(r, k)
+ s += r"\\"
+ # add the last line and return
+ s += r"\end{tabular}"
+ return 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}
+ \sage{pascals_triangle(n)}
+\end{center}
+
\end{document}
diff git a/sagetexpackage.dtx b/sagetexpackage.dtx
 a/sagetexpackage.dtx
+++ b/sagetexpackage.dtx
@@ 1,6 +1,6 @@
% \iffalse metacomment
%
% Copyright (C) 2009 by Dan Drake
+% Copyright (C) 2009 by Dan Drake
% 
%
% See the "Copying and licenses" section at the end of this file for the
@@ 27,7 +27,7 @@
%\NeedsTeXFormat{LaTeX2e}
%\ProvidesPackage{sagetex}
%<*latex>
 [2009/01/27 v2.0.1 embedding Sage into LaTeX documents]
+ [2009/04/21 v2.0.2 embedding Sage into LaTeX documents]
%
%<*driver>
\documentclass{ltxdoc}
@@ 93,7 +93,7 @@
%
% \title{The \ST{} package\thanks{This document
% corresponds to \ST~\fileversion, dated \filedate.}}
% \author{Dan Drake (\texttt{ddrake@member.ams.org}) and others}
+% \author{Dan Drake (\texttt{ddrake (at) member (dot) ams (dot) org}) and others}
%
% \maketitle
%
@@ 365,22 +365,25 @@
% The prime factorization of the current page number plus foo\\
% is $\sage{factor(foo + \thepage)}$.
% \end{quote}
+%
% Here, I'll do just that right now: the prime factorization of the
% current page number plus $12$ is $\sage{factor(\thepage + 12)}$.
% (Wrong answer? See footnote.\footnote{Is the above factorization
% wrong? If the current page number plus $12$ is one larger than the
% claimed factorization, you need to do another Sage/\LTX cycle on
% this source file. Why? The first time you run \LTX on this file, the
% sine graph isn't available, so the text where I've talked about the
% prime factorization is back one page. Then you run Sage, and it
% creates the sine graph and does the factorization. When you run \LTX
% again, the sine graph pushes the text onto the next page, but it
% uses the Sagecomputed value from the previous page. Meanwhile, the
% .sage file has been rewritten with the correct page number, so if
% you do another Sage/\LTX cycle, you'll get the correct value
% above.}) The \sage command doesn't automatically use math mode for
% its output, so be sure to use dollar signs or a displayed math
% environment
+% current page number plus $12$ is $\sage{factor(\thepage + 12)}$ and
+% \thepage. (Wrong answer? See footnote.\footnote{Is the above
+% factorization wrong? If the current page number plus $12$ is one
+% larger than the claimed factorization, another Sage/\LTX cycle on
+% this source file should fix it. Why? The first time you run \LTX on
+% this file, the sine graph isn't available, so the text where I've
+% talked about the prime factorization is back one page. Then you run
+% Sage, and it creates the sine graph and does the factorization. When
+% you run \LTX again, the sine graph pushes the text onto the next
+% page, but it uses the Sagecomputed value from the previous page.
+% Meanwhile, the .sage file has been rewritten with the correct page
+% number, so if you do another Sage/\LTX cycle, you should get the
+% correct value above. However, in some cases, even \emph{that}
+% doesn't work because of some kind of \TeX{} weirdness in ending the
+% one page a bit short and starting another.}) The \sage command
+% doesn't automatically use math mode for its output, so be sure to use
+% dollar signs or a displayed math environment
% as appropriate.\\
%
% \DescribeMacro{\percent} If you are doing modular arithmetic or string
@@ 1505,8 +1508,13 @@
again.' % ((self.filename,) * 2))
self.souttmp.close()
os.remove(self.filename + '.sout.tmp')
 sys.exit(1)
+ sys.exit(int(1))
% \end{macrocode}
+% We use int(1) above to make sure sys.exit sees a Python integer;
+% see
+% \href{http://trac.sagemath.org/sage_trac/ticket/2861#comment:5}{ticket
+% \#2861}.
+% \changes{v2.0.2}{2008/04/21}{Make sure sys.exit sees a Python integer.}
% \end{macro}
%
% \begin{macro}{endofdoc}
@@ 2014,4 +2022,3 @@
% confident the pagebreak problems won't occur.
% \clearpage
% \fi
