Anonymous avatar Anonymous committed 6ab22f4

Small fixes; no functionality changes. Added examples.
Obfuscated email, fixed up sys.exit, discovered that the \thepage business
is even trickier than I thought.

Comments (0)

Files changed (2)

 \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}
 
 % 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 pain---or 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}

sagetexpackage.dtx

 % \iffalse meta-comment
 %
-% Copyright (C) 2009 by Dan Drake <ddrake@member.ams.org>
+% Copyright (C) 2009 by Dan Drake <ddrake (at) member (dot) ams (dot) org>
 % -------------------------------------------------------
 %
 % See the "Copying and licenses" section at the end of this file for the
 %<latex>\NeedsTeXFormat{LaTeX2e}
 %<latex>\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]
 %</latex>
 %<*driver>
 \documentclass{ltxdoc}
 %
 % \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
 %
 % |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 Sage-computed 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 Sage-computed 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
  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}
 %  confident the pagebreak problems won't occur.
 % \clearpage
 % \fi
-
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.