Anonymous avatar Anonymous committed 5036e7f

improvements on Nicolas' sageexample / doctest patch; version 2.2.5

Comments (0)

Files changed (5)

 
 clean:
 	latexcleanup clean .
-	rm -fr sage-plots-for-* E2.sobj *.pyc sagetex.tar.gz sagetex.py sagetex.pyc sagetex.sty makestatic.py sagetexparse.py extractsagecode.py dist MANIFEST remote-sagetex.py auto
+	rm -fr sage-plots-for-* E2.sobj *.pyc sagetex.tar.gz sagetex.py sagetex.pyc sagetex.sty makestatic.py sagetexparse.py extractsagecode.py dist MANIFEST remote-sagetex.py auto example_doctest.sage
 
-# the following bit requires SHELL=bash
 auxclean:
 	/bin/bash -c "rm -f {$(pkg),example}.{glo,gls,aux,sout,out,toc,dvi,pdf,ps,log,ilg,ind,idx,sage,fdb_latexmk}"
+	rm -f example_doctest.sage
 
 # make a tarball suitable for CTAN uploads, or for someone who knows how
 # to handle .dtx files
 \end{verbatim}
 \renewcommand{\sageexampleincludetextoutput}{True}
 This can be useful to check that the two outputs are consistent.
-% fiddle with spacing to demonstrate the kind of thing mentioned in the
-% next paragraph:
-\vspace{10mm}
 
 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
 \renewcommand{\sageexampleincludetextoutput}{False}
 
 However, typesetting your document produces a file named
-\texttt{example\_test.sage} containing all the doctest-like examples,
+\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_test.sage
-sage -t  "example_test.sage"
-**********************************************************************
-File "/home/nthiery/Sage-Combinat/sagetex/example_test.sage", line 30:
-    sage: is_prime(57)
-Expected:
-    toothpaste
-Got:
-    False
-**********************************************************************
-1 items had failures:
-   1 of  11 in __main__.example_0
-***Test Failed*** 1 failures.
-For whitespace errors, see the file /.../.sage//tmp/.doctest_example_test.py
-         [3.7 s]
-----------------------------------------------------------------------
-The following tests failed:
-        sage -t  "example_test.sage"
-Total time for all tests: 3.7 seconds
+  $ 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 (even just \texttt{example-test.sage}).
-Also, running doctests on files outside the main Sage library does not
-always work, so contact \texttt{sage-support} if you run into
-troubles.
+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
 \AtBeginDocument{\@ifundefined{ST@final}{%
 \newwrite\ST@sf%
 \immediate\openout\ST@sf=\jobname.sage%
-
-\newwrite\ST@sftest%
-\immediate\openout\ST@sftest=\jobname_test.sage
-\immediate\write\ST@sftest{r"""}
-\AtEndDocument{\immediate\write\ST@sftest{"""}}
-
 %    \end{macrocode}
 % \begin{macro}{\ST@wsf}
 %   We will write a lot of stuff to that file, so make a convenient
 {\newcommand{\ST@wsf}[1]{\relax}}}
 %    \end{macrocode}
 % \end{macro}
+%
+% \begin{macro}{\ST@dodfsetup}
+%   The |sageexample| environment writes stuff out to a different file
+%   formatted so that one can run doctests on it. We define a macro that
+%   only sets this up if necessary.
+%    \begin{macrocode}
+\newcommand{\ST@dodfsetup}{%
+\@ifundefined{ST@diddfsetup}{%
+\newwrite\ST@df%
+\immediate\openout\ST@df=\jobname_doctest.sage%
+\immediate\write\ST@df{r"""^^J%
+This file was *autogenerated* from \jobname.tex with sagetex.sty^^J%
+version \ST@ver. It contains the contents of all the^^J%
+sageexample environments from \jobname.tex. You should be able to^^J%
+doctest this file with "sage -t \jobname_doctest.sage".^^J%
+^^J%
+It is always safe to delete this file; it is not used in typesetting your^^J%
+document.^^J}%
+\AtEndDocument{\immediate\write\ST@df{"""}}%
+\gdef\ST@diddfsetup{x}}%
+{\relax}}
+%    \end{macrocode}
+% \changes{v2.2.5}{2010/03/25}{Write sageexample environment contents to
+% a separate file, formatted for doctesting}
+% \end{macro}
+%
+% \begin{macro}{\ST@wdf}
+%   This is the compansion to |\ST@wsf|; it writes to the doctest file,
+%   assuming that is has been set up. We ignore the |final| option here
+%   since nothing in this file is relevant to typesetting the document.
+%    \begin{macrocode}
+\newcommand{\ST@wdf}[1]{\immediate\write\ST@df{#1}}
+%    \end{macrocode}
+% \end{macro}
+%
 % Now we declare our options, which mostly just set flags that we check
 % at the beginning of the document, and when running the |.sage| file.
 % \changes{v2.0}{2008/04/04}{Add \texttt{epstopdf} option}
 % |comment| environment does that.\\
 %
 % \begin{environment}{sageexample}
-% Finally, we have an environment which is mostly-but-not-entirely
-% verbatim; this is the example environment, which takes input like
-% Sage doctests, and prints out the commands verbatim but nicely
-% typesets the output of those commands. This and the corresponding
-% Python function are due to Nicolas M. Thi\'ery.
+%   Finally, we have an environment which is mostly-but-not-entirely
+%   verbatim; this is the example environment, which takes input like
+%   Sage doctests, and prints out the commands verbatim but nicely
+%   typesets the output of those commands. This and the corresponding
+%   Python function are due to Nicolas M. Thi\'ery.
 %    \begin{macrocode}
 \newcommand{\sageexampleincludetextoutput}{False}
 \newenvironment{sageexample}{%
    \ST@wsf{%
 try:^^J
- _st_.doctest(\theST@inline, r"""}
-   % It would be good to include the name of the file as well!!!
-   % But I don't know how to do it, and it might be non trivial:
-   % look for inputlineno in 7.7 of:
-   % http://mirror.ctan.org/macros/latex/contrib/examplep/eurotex_2005_examplep.pdf
-   \immediate\write\ST@sftest{Sage example, line \the\inputlineno::}
-   \immediate\write\ST@sftest{}
-   \begingroup
-   \@bsphack
-   \let\do\@makeother\dospecials
-   \catcode`\^^M\active
+ _st_.doctest(\theST@inline, r"""}%
+   \ST@dodfsetup%
+   \ST@wdf{Sage example, line \the\inputlineno::^^J}%
+   \begingroup%
+   \@bsphack%
+   \let\do\@makeother\dospecials%
+   \catcode`\^^M\active%
    \def\verbatim@processline{%
      \ST@wsf{\the\verbatim@line}%
-     \immediate\write\ST@sftest{\the\verbatim@line}%
+     \ST@wdf{\the\verbatim@line}%
    }%
    \verbatim@start%
 }
   \ST@wsf{%
     """, globals(), locals(), \sageexampleincludetextoutput)^^Jexcept:^^J
     _st_.goboom(\the\inputlineno)}%
-  \ifST@paused
+  \ifST@paused%
     \mbox{(Sage\TeX{} is paused)}%
   \else%
     \begin{NoHyper}\ref{@sageinline\theST@inline}\end{NoHyper}%
     \@ifundefined{r@@sageinline\theST@inline}{\gdef\ST@rerun{x}}{}%
   \fi%
-  \immediate\write\ST@sftest{}
+  \ST@wdf{}%
   \stepcounter{ST@inline}}
 %    \end{macrocode}
 % \changes{v2.2.4}{2010/03/14}{Add first support for
 %    \begin{macrocode}
   def inline(self, counter, s):
     self.progress('Inline formula {0}'.format(counter))
-    self.souttmp.write('\\newlabel{@sageinline' + str(counter) + '}{{%\n' + \
+    self.souttmp.write('\\newlabel{@sageinline' + str(counter) + '}{{%\n' +
                  s.rstrip() + '}{}{}{}{}}\n')
 %    \end{macrocode}
 % We are using five fields, just like |hyperref| does, because that
 % just like that for doctests in the Sage library.
 %    \begin{macrocode}
   def doctest(self, counter, str, globals, locals, include_text_output):
+      print 'in doctest'
       current_statement = None
       current_lines = None
       latex_string = ""
           try: # How to test whether the code is an Python expression or a statement?
               # In the first case, we compute the result and include it in the latex
               result = eval(current_statement, globals, locals)
-              # LaTeX includes a lot of vertical space between two
-              # successive verbatim and displaymath environment.
-              # We try to compensate this with a vspace, but this
-              # might not be very robust; anything better welcome.
-              latex_string += "\\vspace{-3ex}\n\\begin{displaymath}\n  %s\n\\end{displaymath}\n"%latex(result)
-          except:
+%    \end{macrocode}
+% The verbatim stuff seems to end with a bit of vertical space, so don't
+% start the displaymath environment with unnecessary vertical
+% space---the displayskip stuff is from \S 11.5 of Herbert Vo\ss's
+% ``\href{http://www.ctan.org/tex-archive/info/math/voss/mathmode/}{Math
+%   Mode}''. Be careful when using \TeX{} commands and Python 3 (or
+% 2.6+) curly brace string formatting; either double braces or separate
+% strings, as below.
+%    \begin{macrocode}
+              latex_string += r"""\abovedisplayskip=0pt plus 3pt
+\abovedisplayshortskip=0pt plus 3pt
+\begin{displaymath}""" + "\n {0}\n".format(latex(result)) + r"\end{displaymath}" + "\n"
+          except SyntaxError:
               # If this fails, we assume that the code was a statement, and just execute it
               exec current_statement in globals, locals
           current_lines = current_statement = None
           sage_block += 1
       self.inline(counter, latex_string)
 %    \end{macrocode}
+% \changes{v2.2.5}{2010/03/25}{Fix up spacing in sageexample displaymath envs}
 % \end{macro}
 %
 % \begin{macro}{plot}

sagetexpackage.dtx

 %<latex>\ProvidesPackage{sagetex}
 %<python>__version__ = """
 %<*latex|python>
-  [2010/03/14 v2.2.4 embedding Sage into LaTeX documents]
+  [2010/03/25 v2.2.5 embedding Sage into LaTeX documents]
 %</latex|python>
 %<python>""".strip()
-%<latex>\newcommand{\ST@ver}{2010/03/14 v2.2.4}
+%<latex>\newcommand{\ST@ver}{2010/03/25 v2.2.5}
 %<*driver>
 \documentclass{ltxdoc}
 \usepackage{sagetex}
 %</driver>
 % \fi
 %
-% \CheckSum{416}
+% \CheckSum{452}
 %
 % \CharacterTable
 %  {Upper-case    \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
 % \end{quote}
 % in your document will be typeset with the Sage inputs in the usual
 % fixed-width font, and the outputs will be typeset as if given to a
-% |\sage| macro. When typesetting the document, there is no test of
-% the validity of the outputs (that is, typesetting with a typical
-% \LTX-Sage-\LTX cycle does not do doctesting), but you should be able
-% to doctest your document with ``|sage -t my_file.sage|''. (This does
-% not always work; if this fails for you, please contact the
-% sage-support group.)
+% |\sage| macro. When typesetting the document, there is no test of the
+% validity of the outputs (that is, typesetting with a typical
+% \LTX-Sage-\LTX cycle does not do doctesting), but when using the
+% |sageexample| environment, an extra file named
+% ``|myfile_doctest.sage|'' is created with the contents of all those
+% environments; it is formatted so that Sage can doctest that file. You
+% should be able to doctest your document with ``|sage -t myfile_doctest.sage|''.
+% (This does not always work; if this fails for you, please contact
+% the sage-support group.)
 %
 % If you would like to see both the original text input and the
 % typeset output, you can issue
 % used in a |sageexample| environment; instead, you can use
 % triple-quoted strings delimited by |'''|.
 %
-% The initial implementation of this environment is due to Nicolas
+% The initial implementation of this environment is due to Nicolas M.
 % Thi\'ery.\\
 %
 % \DescribeMacro{\sagetexindent} There is one final bit to our
       long_description="""The SageTeX package allows you to embed code,
   results of computations, and plots from the Sage mathematics
   software suite (http://sagemath.org) into LaTeX documents.""",
-      version='2.2.4',
+      version='2.2.5',
       author='Dan Drake',
       author_email='ddrake@member.ams.org',
-      url='http://mathsci.kaist.ac.kr/~drake',
+      url='http://bitbucket.org/ddrake/sagetex/',
       license='GPLv2+',
       py_modules=['sagetex'],
       data_files = [('share/texmf/tex/generic/sagetex',
          'sagetexpackage.pdf',
          'sagetexparse.py',
          'sagetex.sty'])])
-
-
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.