Ticket #4871: trac_4871.patch

File trac_4871.patch, 4.7 KB (added by was, 12 years ago)
  • sage/misc/latex.py

    # HG changeset patch
    # User William Stein <wstein@gmail.com>
    # Date 1230142123 28800
    # Node ID e14c48e9fe069b51c6cbd62540fe76943289d60c
    # Parent  0ba79f63caafd053dfc26c3a6caf20d86ddd4a02
    trac #4871 -- latex typesetting using dvipng is broken on OS X unless unless one uses the dvipng spkg; also the optional filename argument is broken if the file isn't in the current directory; finally, there area  lot of dangerous tmp files created in the current directory
    
    diff -r 0ba79f63caaf -r e14c48e9fe06 sage/misc/latex.py
    a b  
    3434SLIDE_HEADER='\\documentclass[a0,8pt]{beamer}\\usepackage{fullpage}\\usepackage{amsmath}\n\\usepackage{amssymb}\n\\usepackage{amsfonts}\\usepackage{graphicx}\usepackage{pstricks}\pagestyle{empty}\n\\textwidth=1.1\\textwidth\\textheight=2\\textheight'
    3535
    3636
    37 import os
     37import os, shutil
    3838
    3939import os.path
    4040
     
    164164
    165165    Use \code{latex(...)} to typeset a SAGE object.
    166166
    167     Use \code{\%slide} instead to typeset slides.
     167    Use \code{\%slide} instead to typeset slides.
     168
     169    WARNING: You must have the dvipng (or dvips and convert) installed
     170    on your operating system, or this command won't work.
    168171    """
    169172    def __init__(self, debug=False, slide=False, density=150):
    170173        self.__debug = debug
     
    200203        INPUT:
    201204            x -- string to evaluate.
    202205            strip -- ignored
    203             filename -- output filename
     206            filename -- output filename (string with no spaces)
    204207            debug -- whether to print verbose debugging output
    205208            density -- how big output image is.
    206209            locals -- extra local variables used when evaluating \sage{..}
    207210                      code in x.
     211
     212        WARNING: You must have the dvipng (or dvips and convert)
     213        installed on your operating system, or this command won't
     214        work.
     215
     216        EXAMPLES:
     217            sage: tmp = tmp_filename()
     218            sage: sage.misc.latex.Latex().eval('1', filename=tmp)   # optional -- dvipng
     219            ''
     220            sage: os.path.exists(tmp+'.png')                        # optional -- dvipng
     221            True
    208222        """
    209223        if density is None:
    210224            density = self.__density
    211225        if filename is None:
    212226            filename = 'sage%s'%random.randint(1,100) # to defeat browser caches
     227        else:
     228            filename = os.path.splitext(filename)[0]  # get rid of extension
     229        base = tmp_dir()
     230        orig_base, filename = os.path.split(os.path.abspath(filename))
     231        if len(filename.split()) > 1:
     232            raise ValueError, "filename must contain no spaces"
    213233        if debug is None:
    214234            debug = self.__debug
    215235        x = self._latex_preparse(x, locals)
    216         O = open('%s.tex'%filename,'w')
     236        O = open('%s/%s.tex'%(base,filename),'w')
    217237        if self.__slide:
    218238            O.write(SLIDE_HEADER)
    219239            O.write('\\begin{document}\n\n')
     
    232252            redirect=' 2>/dev/null 1>/dev/null '
    233253        else:
    234254            redirect=''
    235         lt = 'latex \\\\nonstopmode \\\\input{%s.tex} %s'%(filename, redirect)
     255        lt = 'cd "%s"&& sage-native-execute latex \\\\nonstopmode \\\\input{%s.tex} %s'%(base, filename, redirect)
    236256        if have_dvipng():
    237             dvipng = 'dvipng -q -T bbox -D %s %s.dvi'%(density, filename)
    238             cmd = ' ; '.join([lt, dvipng])
     257            dvipng = 'sage-native-execute dvipng -q -T bbox -D %s %s.dvi -o %s.png'%(density, filename, filename)
     258            cmd = ' && '.join([lt, dvipng])
    239259           
    240260        else:
    241             dvips = 'dvips %s.dvi %s'%(filename, redirect)
    242             convert = 'convert -density %sx%s -trim %s.ps %s.png %s '%\
     261            dvips = 'sage-native-execute dvips %s.dvi %s'%(filename, redirect)
     262            convert = 'sage-native-execute convert -density %sx%s -trim %s.ps %s.png %s '%\
    243263                      (density,density, filename, filename, redirect)
    244             cmd = ' ; '.join([lt, dvips, convert])
    245         e = os.system(cmd + ' 1>/dev/null 2>/dev/null')
     264            cmd = ' && '.join([lt, dvips, convert])
     265        if debug:
     266            print cmd
     267        e = os.system(cmd + ' ' + redirect)
    246268        if e:
    247269            print "An error occured."
    248270            try:
     
    250272            except IOError:
    251273                pass
    252274            return 'Error latexing slide.'
    253 
    254         if not debug:
    255             F = os.listdir('.')
    256             n = len(filename) + 1
    257             d = ' '.join([x for x in F if x[:n] == filename+'.' and x[-4:] != '.png'])
    258             cmd = 'rm ' + d
    259             e = os.system(cmd)
     275        shutil.copy(base + '/' + filename + '.png', orig_base + '/'+filename + '.png')
     276        shutil.rmtree(base)
    260277        return ''
    261278
    262279