Commits

Nicolas M. Thiéry  committed 1323b7d

Doctests and vertical spacing

This patch:

- Adjusts the vertical space between sageexample inputs and typeset outputs
- Produce a new file ...._test.sage on which to run sage -t

  • Participants
  • Parent commits 911609d

Comments (0)

Files changed (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.
 % fiddle with spacing to demonstrate the kind of thing mentioned in the
 % next paragraph:
 \vspace{10mm}
 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
 \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
+  ...       '''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
   toothpaste
 \end{sageexample}
 \renewcommand{\sageexampleincludetextoutput}{False}
-\ldots but it should be possible to run the usual Sage doctest mechanism
-on the generated \texttt{.sage} file---or perhaps the \texttt{.py} file.
-Running doctests on files outside the main Sage library does not always
-work, so contact sage-support if you run into troubles.
+
+However, typesetting your document produces a file named
+\texttt{example\_test.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
+\end{verbatim}
+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.
 
 Some more examples. This environment is implemented a little bit
 differently than the other environments, so it's good to make sure that
 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}
 

File py-and-sty.dtx

 \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
 % 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 Thi\'ery.
+% Python function are due to Nicolas M. Thi\'ery.
 %    \begin{macrocode}
 \newcommand{\sageexampleincludetextoutput}{False}
-\newenvironment{sageexample}{\ST@wsf{%
+\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
    \def\verbatim@processline{%
      \ST@wsf{\the\verbatim@line}%
+     \immediate\write\ST@sftest{\the\verbatim@line}%
    }%
    \verbatim@start%
 }
     \begin{NoHyper}\ref{@sageinline\theST@inline}\end{NoHyper}%
     \@ifundefined{r@@sageinline\theST@inline}{\gdef\ST@rerun{x}}{}%
   \fi%
+  \immediate\write\ST@sftest{}
   \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) + '}{{' + \
+    self.souttmp.write('\\newlabel{@sageinline' + str(counter) + '}{{%\n' + \
                  s.rstrip() + '}{}{}{}{}}\n')
 %    \end{macrocode}
 % We are using five fields, just like |hyperref| does, because that
           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_string += "\\begin{displaymath}\n  %s\n\\end{displaymath}\n"%latex(result)
+              # 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:
               # If this fails, we assume that the code was a statement, and just execute it
               exec current_statement in globals, locals