Ask Your Question

Tom Dorris's profile - activity

2017-06-17 03:28:46 +0200 received badge  Famous Question (source)
2015-02-17 22:37:17 +0200 received badge  Notable Question (source)
2014-02-15 03:25:35 +0200 received badge  Popular Question (source)
2012-12-16 09:57:28 +0200 received badge  Good Question (source)
2012-04-25 22:06:00 +0200 received badge  Nice Answer (source)
2012-04-25 19:41:02 +0200 received badge  Teacher (source)
2012-04-25 19:31:42 +0200 answered a question How to import a module at startup?

I read somewhere that the .sage/init.sage file gets executed at startup, but I haven't tried it myself.

2012-04-25 19:15:32 +0200 received badge  Nice Question (source)
2012-04-25 18:56:47 +0200 received badge  Editor (source)
2012-04-25 18:21:28 +0200 received badge  Student (source)
2012-04-25 16:20:52 +0200 asked a question Sagetex from the notebook - feedback on how I automate things

This isn't really a question, so please accept my apologies if non-questions are unwelcome here.

I am using sage with latex, but I am the kind of person who doesn't like leaving the notebook, so I tried to automate the process. You can see my code below. Any comments and suggestions are appreciated.

Also, if you think you will find this or a similar setup useful, please say so, because people who like staying within the notebook are probably underrepresented among the developers.

def load_cache(f): # so that we don't recompute for every latex change
    try:f.cache=load(DATA+"/cache/"+f.f.func_name)
    except IOError:pass

def export_code (list_of_functions, folder): # copy code to our latex folder
    for func in list_of_functions:
        try:
            save(func.cache,DATA+"/cache/"+func.f.func_name)
            func=func.f
        except AttributeError:pass # in case the function isn't cached
        sh.eval("cp " + func.func_code.co_filename + " " + folder + "/" + func.func_name + ".py")

def runsagelatex(latexfile):
    def evalindir(cmd,path): # otherwise the output appears in the current directory
        sh.eval("( cd "+os.path.dirname(path)+" ; "+cmd+" )")
    evalindir("pdflatex --file-line-error "+latexfile,latexfile)
    evalindir("sage "+os.path.splitext(latexfile)[0]+".sagetex.sage",latexfile)
    evalindir("pdflatex --file-line-error "+latexfile,latexfile)

def sagelatex(name="project",export=[],cmd=""):
    html(r'<div class="notruncate">') # for the output of pdflatex
    try:os.mkdir(DATA+"/cache/")
    except OSError:pass
    try:os.mkdir(DATA+"/sagelatex/")
    except OSError:pass
    try:os.mkdir(DATA+"/sagelatex/sagenb-export/")
    except OSError:pass
    export_code(export,DATA+"/sagelatex/sagenb-export/")
    f=open(DATA+"/sagelatex/"+name+".tex","wb")
    f.write(cmd)
    f.close()
    runsagelatex(DATA+"/sagelatex/"+name+".tex")
    html(r'</div>')

Example usage:

def f1(a,b):
    return a+b

@cached_function
def f2(a):
    return a^2
load_cache(f2)

And in a new cell (and change DATA below):

sagelatex(name="myproject",export=[f1,f2,load_cache],cmd=\
r"""
\documentclass{article}
\usepackage{sagetex}
\begin{document}
\begin{sagesilent}
DATA="/.../.sage/sage_notebook.sagenb/home/user/123/data/" # make it match the worksheet's DATA constant
execfile("sagenb-export/load_cache.py")
execfile("sagenb-export/f1.py")
execfile("sagenb-export/f2.py")
\end{sagesilent}
$\sage{f1(1,2)+f2(10)}$
\end{document}
""")

This should produce a pdf file in your DATA/sagelatex folder. That folder is accessible through your web browser.