convertor Sage -> TeX -> PDF

521 views
Skip to first unread message

ma...@mendelu.cz

unread,
Oct 29, 2009, 7:44:23 AM10/29/09
to sage-support
Hello all, the conversion into PDF has been discussed several times
here.

One option is to print into a PDF file. This is another possibility:

I wrote for myself a simple converter from Sage worksheets to PDF via
PDF LaTeX

You can see the outputs in the bulletted list at http://user.mendelu.cz/marik/sage/
, for example http://user.mendelu.cz/marik/sage/dr.pdf

The initial version of the script is at http://user.mendelu.cz/marik/sage/sage2tex
and is neither cleanly written nor clever too much, but still "better
than get a wire into your eye" - as we say in Czech :)

For whom is this topic worth

Feel free to use it or modify as you need.

Robert Marik

Wilfried_Huss

unread,
Nov 1, 2009, 7:49:44 AM11/1/09
to sage-support


On 29 Okt., 12:44, "ma...@mendelu.cz" <ma...@mendelu.cz> wrote:
> Hello all, the conversion into PDF has been discussed several times
> here.
>
> One option is to print into a PDF file. This is another possibility:
>
> I wrote for myself a simple converter from Sage worksheets to PDF via
> PDF LaTeX

Great, this is exactly what I need.

> You can see the outputs in the bulletted list athttp://user.mendelu.cz/marik/sage/
> , for examplehttp://user.mendelu.cz/marik/sage/dr.pdf
>
> The initial version of the script is athttp://user.mendelu.cz/marik/sage/sage2tex
> and is neither cleanly written nor clever too much, but still "better
> than get a wire into your eye" - as we say in Czech :)
>
> For whom is this topic worth
>
> Feel free to use it or modify as you need.

I have modified the skript a little bit. It now first builds a list of
text-, input- and output
cells, and then converts each sell to latex. This makes things much
easier. I also
added syntax highlighting for python, html and latex input cells, and
if the output is not
a tex formula it is put into a verbatim environment.

The HTML-LaTeX conversion is still done by a bunch of regular
expressions, so this
will need some improvements. But it already works very well.

You can find the new version of the script at:
http://www.math.tugraz.at/~huss/sage/sws2tex.py

And here are example outputs:
http://www.math.tugraz.at/~huss/sage/sage_tutorium.pdf
http://www.math.tugraz.at/~huss/sage/sws2pdf_test.pdf

Would you agree to release your initial script under the GPL or an
other Sage compatible
licence? I hope this can be improved to a point where it could be
included into Sage, for
this it needs a proper licence.

Cheers,
Wilfried Huss

ma...@mendelu.cz

unread,
Nov 1, 2009, 9:08:25 AM11/1/09
to sage-support


On 1 lis, 13:49, Wilfried_Huss <h...@finanz.math.tugraz.at> wrote:
> On 29 Okt., 12:44, "ma...@mendelu.cz" <ma...@mendelu.cz> wrote:
>
> > Hello all, the conversion into PDF has been discussed several times
> > here.
>
> > One option is to print into a PDF file. This is another possibility:
>
> > I wrote for myself a simple converter from Sage worksheets to PDF via
> > PDF LaTeX
>
> Great, this is exactly what I need.
>
> > You can see the outputs in the bulletted list athttp://user.mendelu.cz/marik/sage/
> > , for examplehttp://user.mendelu.cz/marik/sage/dr.pdf
>
> > The initial version of the script is athttp://user.mendelu.cz/marik/sage/sage2tex
> > and is neither cleanly written nor clever too much, but still "better
> > than get a wire into your eye" - as we say in Czech :)
>
> > For whom is this topic worth
>
> > Feel free to use it or modify as you need.
>
> I have modified the skript a little bit.


Great, I have seen that you improved it a lot! Thanks.

I have two another improvements (and changed the script at
http://user.mendelu.cz/marik/sage/sage2tex )

First:

The following lines can be used to process tables created by
html.table command, see the file http://user.mendelu.cz/marik/sage/integral.pdf

obsah=string.replace(obsah,"<table class=\"table_form\">","{\
\offinterlineskip\\halign{\\vrule \\vrule width 0 pt height 14pt depth
5pt # &&\\vrule # \\cr\\noalign{\\hrule}")

obsah=string.replace(obsah,"<tbody>","%")
obsah=string.replace(obsah,"</tbody>","%")
obsah=string.replace(obsah,"</table>","\\noalign{\\hrule}}}")
obsah=string.replace(obsah,"<tr>","\\global\\def\\optcr{\\vskip 2
pt\\hrule}")
obsah=string.replace(obsah,"<tr class =\"row-a\">","\\global\\let\
\optcr\\relax{}")
obsah=string.replace(obsah,"<tr class =\"row-b\">","\\global\\let\
\optcr\\relax{}")
obsah=string.replace(obsah,"</tr>","\\cr\\noalign{\\hrule\
\optcr}")
obsah=string.replace(obsah,"<th>","\\quad\\textbf{")
obsah=string.replace(obsah,"</th>","} & ")
obsah=string.replace(obsah,"<td>","\\quad ")
obsah=string.replace(obsah,"</td>"," & ")


Second: TeX does not understand \textcolor[HTML]{ff0000}{stuff}. So
these lines convert into \textcolor[HTML]{FF0000}{stuff} (replacement
for previous version of this regexp substitution)

regular=re.compile(r"<span style=\"color: #(.*?);\">")
obsah=regular.sub(lambda x: "\\textcolor[HTML]{"+x.group(1).upper()
+"}{",obsah)



> You can find the new version of the script at:
>    http://www.math.tugraz.at/~huss/sage/sws2tex.py
>
> And here are example outputs:
>    http://www.math.tugraz.at/~huss/sage/sage_tutorium.pdf
>    http://www.math.tugraz.at/~huss/sage/sws2pdf_test.pdf
>
> Would you agree to release your initial script under the GPL or an
> other Sage compatible
> licence? I hope this can be improved to a point where it could be
> included into Sage, for
> this it needs a proper licence.

I am O.K. with any free licence, GNU GPL, Creative Commons or any
other.

In fact, the script was so simple that I was not thinking about any
licence.

I have no skills in programing in Python or any other object oriented
langugae and you cannot expect, that I can help to improve it into a
form which could be used for sufficiently big set of sage worksheets,
including complicated html, 3D graphics, included external pictures
and some other things.

Would you like to work a bit deeper on conversion between Sage and
TeX? If yes, then everything is up to you and from my point of view it
is sufficient to state somewhere, that the initial version was based
on the work of Robert Marik.

Thank you very much
Robert

ma...@mendelu.cz

unread,
Nov 1, 2009, 3:51:16 PM11/1/09
to sage-support


On 1 lis, 15:08, "ma...@mendelu.cz" <ma...@mendelu.cz> wrote:
>
> I have two another improvements (and changed the script at http://user.mendelu.cz/marik/sage/sage2tex)
>

btw: another improvement in current http://user.mendelu.cz/marik/sage/sage2tex
(from October 30) are lines

lines 46-48
worksheet=open("sage_worksheet/worksheet.txt",'r')
title = worksheet.readline()
worksheet.close()

lines 184-190 from TeX preamble:

\usepackage{eso-pic}
\definecolor{mygreen}{RGB}{10,80,40}

\AddToShipoutPicture{\hbox to \paperwidth{\color{mygreen}\vrule
width 0.5em height\paperheight
\raise 2pt\hbox to \paperwidth{\tiny\hss\color{mygreen}\textrm {Robert
Ma\v{r}\'{\i}k}: \titlename \quad \hss Sage worksheet converted by
\href{http://user.mendelu.cz/marik/sage/sage2tex}{\texttt{sage2tex}}
\qquad}}}%

and line 206
+"\\def\\titlename{"+do_substitutions(title)+


this is used to write name of the author and name of the worksheet in
bottom line.

The name of the author is hardwired in my version, however it could be
perhaps passed from command line or config file.

I am thinking also on possibility how to translate the stuff D
[0,0]\left(y\right) into y''. I think that the simplest solution is to
allow users custom substitutions specified from command line or from
config file. And I think that there is a trac which will fix this
notation.


Questions to more experienced Sage users and developers:
----------------------------------------------------------------------------------

when exporting sws file and unpacking, I do not understand the
following

1. what is the difference between sage_workseet/worksheet.html and
sage_workseet/worksheet.txt ? (neglecting the ffirst two lines, the
rest is identical in my notebooks)

2. My worksheet.html looks like this

{{{id=1|
x,y=var('x y')
///
}}}

{{{id=2|
plot(sin(x),(x,0,3))
///
<html><font color='black'><img src='cell://sage0.png'></font></html>
}}}

{{{id=3|
plot(sin(x),(x,0,6),filename="sage0.pdf")
///
<html><font color='black'><img src='cell://sage0.pdf'></font></html>
}}}

{{{id=4|
plot3d(x^2-y^2,(x,-1,1),(y,-1,1),viewer='tachyon')
///
}}}

{{{id=5|

///
}}}

Why is line like <html><font color='black'><img src='cell://
sage0.png'></font></html> missing in the output for cell id=4. The
file with the picture is in the directory and the picture is in the
notebook - exactly like for output of 2d plot in cell id=2. Is there
any reason to skip it, or is it a bug?

Thank you.
Robert



Stan Schymanski

unread,
Nov 2, 2009, 3:08:18 AM11/2/09
to sage-s...@googlegroups.com
Dear Wilfried,

The tutorial looks great! How did you do the numbered head lines and the
index? Is the sage worksheet itself published somewhere?

Cheers
Stan

Wilfried_Huss

unread,
Nov 2, 2009, 3:56:31 AM11/2/09
to sage-support


On 2 Nov., 09:08, Stan Schymanski <schym...@gmail.com> wrote:
> Dear Wilfried,
>
> The tutorial looks great! How did you do the numbered head lines and the
> index?

Just use <h1> ... </h1> tags in the notebook. They are converted to
\sections{}
in the LaTeX file. The the worksheet the sections are not numbered.

> Is the sage worksheet itself published somewhere?

The worksheet is attached to the PDF. If you click on the graphic
on the top left of the first page, you can download the sws file.

If you don't have a PDF viewer that supports this, I can put up a
direct link to the worksheet.

Cheers,
Wilfried

Stan Schymanski

unread,
Nov 6, 2009, 8:03:50 AM11/6/09
to sage-s...@googlegroups.com
Dear Wilfried,

That's amazing, I will have to try out the script on my own documents.

Unfortunately, Adobe Acrobat 8 does not find a link behind the pin
graphic, and I was also not able to find the attached worksheet. I think
that it would be great to put up the link in the sage library under
http://wiki.sagemath.org/DocumentationProject.

Thanks again for sharing this with us.

Stan Schymanski

unread,
Nov 6, 2009, 9:07:40 AM11/6/09
to sage-s...@googlegroups.com
Dear Marik,

I tried out the sage2tex script from your web page, but I keep getting
the following error:

sangria:~/notebook sschym$ ./sage2tex Benard.sws
Traceback (most recent call last):
File "./sage2tex", line 46, in ?
worksheet=open("sage_worksheet/worksheet.txt",'r')
IOError: [Errno 2] No such file or directory: 'sage_worksheet/worksheet.txt'
sangria:~/notebook sschym$

Do you have an idea how I could avoid this?

Thanks for your help!

Cheers
Stan
--
________________________________________

Stan Schymanski
Scientist
Max Planck Institute for Biogeochemistry
Postfach 10 01 64
D-07701 Jena

Phone: +49.3641.576264
Fax: +49.3641.577274
WWW: http://www.bgc-jena.mpg.de/~sschym

Biospheric Theory and Modelling Group
http://www.bgc-jena.mpg.de/bgc-theory/
_________________________________________

ma...@mendelu.cz

unread,
Nov 6, 2009, 9:21:47 AM11/6/09
to sage-support
I have no problem with acrobart reader 8. There is no lik below the
pin, it is an attachemenet.

Robert

On 6 lis, 14:03, Stan Schymanski <schym...@gmail.com> wrote:
> Dear Wilfried,
>
> That's amazing, I will have to try out the script on my own documents.
>
> Unfortunately, Adobe Acrobat 8 does not find a link behind the pin
> graphic, and I was also not able to find the attached worksheet. I think
> that it would be great to put up the link in the sage library underhttp://wiki.sagemath.org/DocumentationProject.

Jason Grout

unread,
Nov 6, 2009, 9:21:35 AM11/6/09
to sage-s...@googlegroups.com
Wilfried_Huss wrote:
>
>
> On 2 Nov., 09:08, Stan Schymanski <schym...@gmail.com> wrote:
>> Dear Wilfried,
>>
>> The tutorial looks great! How did you do the numbered head lines and the
>> index?
>
> Just use <h1> ... </h1> tags in the notebook. They are converted to
> \sections{}
> in the LaTeX file. The the worksheet the sections are not numbered.
>
>> Is the sage worksheet itself published somewhere?
>
> The worksheet is attached to the PDF. If you click on the graphic
> on the top left of the first page, you can download the sws file.
>
> If you don't have a PDF viewer that supports this, I can put up a
> direct link to the worksheet.

At one point, we were talking about then allowing such a pdf file being
uploaded to an online notebook and the online notebook automatically
extracting the .sws file. This would allow a person to, for example,
include an sws file with an arxiv pdf preprint. A reader could just
download the pdf from arxiv, then upload the pdf to sagenb.org, and
automatically have the associated .sws file. There's a trac ticket
listing some code to do this sort of thing. It would be an interesting
project for someone who had the time to work on this to integrate this
into the sage notebook.


See http://trac.sagemath.org/sage_trac/ticket/4825

Thanks,

Jason

--
Jason Grout

ma...@mendelu.cz

unread,
Nov 6, 2009, 9:22:40 AM11/6/09
to sage-support
Which version of Sage do you use? 4.2?

Robert

ma...@mendelu.cz

unread,
Nov 6, 2009, 9:35:07 AM11/6/09
to sage-support
> At one point, we were talking about then allowing such a pdf file being
> uploaded to an online notebook and the online notebook automatically
> extracting the .sws file. This would allow a person to, for example,
> include an sws file with an arxiv pdf preprint. A reader could just
> download the pdf from arxiv, then upload the pdf to sagenb.org, and
> automatically have the associated .sws file. There's a trac ticket
> listing some code to do this sort of thing. It would be an interesting
> project for someone who had the time to work on this to integrate this
> into the sage notebook.
>
> Seehttp://trac.sagemath.org/sage_trac/ticket/4825

Thanks for comments

Is it possible to use command like " pdftk sage_worksheet.pdf
unpack_files output . " to get attached files into current directory
and then upload all sws files into Sage?


Robert


Stan Schymanski

unread,
Nov 6, 2009, 10:05:10 AM11/6/09
to sage-s...@googlegroups.com
Hi Marik,

I was trying to convert notebooks I saved prior to 4.2. I just freshly
exported a worksheet from sage 4.2 and ran sage2tex, but it got stuck at
a later stage:

...
No file sage_worksheet.aux.
(/usr/local/texlive/2008/texmf-dist/tex/context/base/supp-pdf.tex
[Loading MPS to PDF converter (version 2006.09.02).]
) (/usr/local/texlive/2008/texmf-dist/tex/latex/hyperref/nameref.sty
(/usr/local/texlive/2008/texmf-dist/tex/latex/oberdiek/refcount.sty))
*geometry auto-detecting driver*
*geometry detected driver: pdftex*
ABD: EveryShipout initializing macros<<VOM_plots_4.2.sws>>
(/usr/local/texlive/2008/texmf-dist/tex/latex/cslatex/il2cmtt.fd)
! Missing $ inserted.
<inserted text>
$
l.74 }}}

?

I suspect that the work sheet I used for this is a bit too messy, so I
will try with a cleaner one. Thanks for your feedback, anyway!

By the way, how do you get the attachment out of the pdf? I tried
clicking on the graphic, but couldn't find any fuseful options in Acrobat 8.

Cheers
Stan

Jason Grout

unread,
Nov 6, 2009, 10:11:18 AM11/6/09
to sage-s...@googlegroups.com


Sure, I suppose, if you want to do it by hand. We were trying to make
it so that you just uploaded the pdf to the sage notebook.

Jason

--
Jason Grout

ma...@mendelu.cz

unread,
Nov 6, 2009, 10:15:47 AM11/6/09
to sage-support


On 6 lis, 16:05, Stan Schymanski <schym...@gmail.com> wrote:
> By the way, how do you get the attachment out of the pdf? I tried
> clicking on the graphic, but couldn't find any fuseful options in Acrobat 8.
>

Right click - save embeded file to disk
you can also use pdftk, se my previous messege in this thread.

Robert

Dan Drake

unread,
Nov 6, 2009, 7:32:10 PM11/6/09
to sage-s...@googlegroups.com
On Fri, 06 Nov 2009 at 09:11AM -0600, Jason Grout wrote:
> > Is it possible to use command like " pdftk sage_worksheet.pdf
> > unpack_files output . " to get attached files into current directory
> > and then upload all sws files into Sage?
>
> Sure, I suppose, if you want to do it by hand. We were trying to make
> it so that you just uploaded the pdf to the sage notebook.

I think our plan (if you can call it that) was to use something like
pdftk to unpack the worksheet. So pdftk should definitely work, although
as Jason said, the idea was that the notebook server would say, "hrm,
this user gave me a PDF...so, let me see if I can extract a worksheet"
and then use some Python PDF library.

There's a sample of this idea here:
http://sage.math.washington.edu/home/drake/
Look for "embedded_worksheet" files.

Dan

--
--- Dan Drake
----- http://mathsci.kaist.ac.kr/~drake
-------

signature.asc

ma...@mendelu.cz

unread,
Nov 7, 2009, 2:13:28 AM11/7/09
to sage-support


On 6 lis, 16:11, Jason Grout <jason-s...@creativetrax.com> wrote:
>
> Sure, I suppose, if you want to do it by hand.  We were trying to make
> it so that you just uploaded the pdf to the sage notebook.
>
> Jason

Ah, so the problem is that you want not to install pdftk and all its
dependencies on each Sage server. Right? Since otherwise I guess that
unpacking sws could be done automatically.

Robert

Stochastix

unread,
Nov 7, 2009, 7:40:23 AM11/7/09
to sage-support
I have a similar but slightly different request.
When I have a plot in sage, say of a function x-> sin(x), I would like
to be able to do something like what is explained in

http://www.texample.net/tikz/examples/gnuplot-basics/

for gnuplot and pgf. This means, extracting the relevant numerical
values and leaving pgf deal with axis, ticks, grid, captions and
additional stuff in order to keep the same fonts as in LaTeX for both
the text and the pictures.

Any idea ?

William Stein

unread,
Nov 7, 2009, 12:40:05 PM11/7/09
to sage-s...@googlegroups.com

You can easily extract the relevant numerical values for all elements
of plots. E.g.,

sage: L = list(plot(sin)[0])

Another example:

sage: a = plot(sin) + plot(cos)
sage: a[0]
Line defined by 200 points
sage: a[1]
Line defined by 200 points
sage: list(a[1])[:10]

[(-1.0, 0.54030230586813977),
(-0.98921646193235457, 0.54934475026523821),
(-0.97881737105892419, 0.55800432723415849),
(-0.96672470607055161, 0.567998237362355),
(-0.95509177118254784, 0.57753384135311991),
(-0.94833678316216885, 0.58303517065191224),
(-0.94399062303568926, 0.58656067742426055),
(-0.93148561011253583, 0.59664242831390957),
(-0.92443618000428662, 0.60228477505404343),
(-0.90878687891761545, 0.61470306127180685)]

Jason Grout

unread,
Nov 7, 2009, 12:55:35 PM11/7/09
to sage-s...@googlegroups.com


In addition to what William said, I think we ought to eventually support
pgf (either through matplotlib or on our own). This would be very handy
in sagetex, for example.

Note that we already use pgf to draw graphs (vertex/line things) in latex.

There has been interest in a pgf backend to matplotlib (what we use to
actually draw 2d graphics most of the time):
http://www.mailinglistarchive.com/html/matplotl...@lists.sourceforge.net/2009-08/msg00358.html

I played with making a pgf backend to matplotlib once, but ran out of
time to experiment with it.

Jason

--
Jason Grout

Stan Schymanski

unread,
Nov 9, 2009, 4:32:07 AM11/9/09
to sage-s...@googlegroups.com
Hi Marik,

Thanks for that. For some reason, it does not work on the pdf I
downloaded, but if I create a pdf myself, it works. The comment in the
pdf could mention "right-click" to make it clearer. Great work! I hope
this script will make it into sage some time!! It still produced a lot
of error messages and formatting glitches with the worksheet I tried it
on, but it is a good start.

Cheers
Stan
Reply all
Reply to author
Forward
0 new messages