def png(x, filename, density=150, debug=False, do_in_background=True, tiny=False): """ Create a png image representation of x and save to the given filename. INPUT: - ``x`` - object to be displayed - ``filename`` - file in which to save the image - ``density`` - integer (default: 150) - ``debug`` - bool (default: False): print verbose output - ``do_in_background`` - bool (default: True): create the file in the background - ``tiny`` - bool (default: False): use 'tiny' font """ import sage.plot.all if sage.plot.all.is_Graphics(x): x.save(filename) return s = _latex_file_([x], math_left='$\\displaystyle', math_right='$', title='', debug=debug, tiny=tiny, extra_preamble='\\textheight=2\\textheight') abs_path_to_png = os.path.abspath(filename) tmp = tmp_dir('sage_viewer') open('%s/sage.tex'%tmp,'w').write(s) os.system('ln -sf %s/common/macros.tex %s'%(SAGE_DOC, tmp)) O = open('%s/go'%tmp,'w') go = 'latex \\\\nonstopmode \\\\input{sage.tex}; dvips -l =1 -f < sage.dvi > sage.ps ; convert -density %sx%s -trim sage.ps "%s";'%(density, density, abs_path_to_png) go += ' rm sage.* macros.* go ; cd .. ; rmdir %s'%tmp if debug: print go O.write(go) O.close() if not debug: direct = '1>/dev/null 2>/dev/null' else: direct = '' if do_in_background: background = '&' else: background = '' os.system('cd %s; chmod +x go; ./go %s%s'%(tmp,direct,background)) return s
def view(objects, title='SAGE', debug=False, sep='', tiny=False, **kwds): r"""nodetex Compute a latex representation of each object in objects, compile, and display typeset. If used from the command line, this requires that latex be installed. INPUT: - ``objects`` - list (or object) - ``title`` - string (default: 'Sage'): title for the document - ``debug`` - bool (default: False): print verbose output - ``sep`` - string (default: ''): separator between math objects - ``tiny`` - bool (default: False): use tiny font. OUTPUT: Display typeset objects. This function behaves differently depending on whether in notebook mode or not. If not in notebook mode, this opens up a window displaying a dvi (or pdf) file, displaying the following: the title string is printed, centered, at the top. Beneath that, each object in objects is typeset on its own line, with the string sep inserted between these lines. The value of ``sep`` is inserted between each element of the list ``objects``; you can, for example, add vertical space between objects with ``sep='\\vspace{15mm}'``, while ``sep='\\hrule'`` adds a horizontal line between objects, and ``sep='\\newpage'`` inserts a page break between objects. If in notebook mode, this uses jmath to display the output in the notebook. Only the first argument, objects, is relevant; the others are ignored. If objects is a list, the result is typeset as a Python list, e.g. [12, -3.431] - each object in the list is not printed on its own line. EXAMPLES:: sage: sage.misc.latex.EMBEDDED_MODE=True sage: view(3) <html><span class="math">3</span></html> sage: sage.misc.latex.EMBEDDED_MODE=False """ if EMBEDDED_MODE: print typeset(objects) return if isinstance(objects, LatexExpr): s = str(objects) else: s = _latex_file_(objects, title=title, debug=debug, sep=sep, tiny=tiny) from sage.misc.viewer import dvi_viewer viewer = dvi_viewer() tmp = tmp_dir('sage_viewer') open('%s/sage.tex'%tmp,'w').write(s) os.system('ln -sf %s/common/macros.tex %s'%(SAGE_DOC, tmp)) O = open('%s/go'%tmp,'w') #O.write('export TEXINPUTS=%s/doc/commontex:.\n'%SAGE_ROOT) # O.write('latex \\\\nonstopmode \\\\input{sage.tex}; xdvi -noscan -offsets 0.3 -paper 100000x100000 -s %s sage.dvi ; rm sage.* macros.* go ; cd .. ; rmdir %s'%(zoom,tmp)) # Added sleep 1 to allow viewer to open the file before it gets removed # Yi Qiang 2008-05-09 O.write('latex \\\\nonstopmode \\\\input{sage.tex}; %s sage.dvi ; sleep 1 rm sage.* macros.* go ; cd .. ; rmdir %s' % (viewer, tmp)) O.close() if not debug: direct = '1>/dev/null 2>/dev/null' else: direct = '' os.system('cd %s; chmod +x go; ./go %s&'%(tmp,direct))
def eval(self, x, globals, strip=False, filename=None, debug=None, density=None, locals={}): """ INPUT: globals -- a globals dictionary - ``x`` - string to evaluate. - ``strip`` - ignored - ``filename`` - output filename - ``debug`` - whether to print verbose debugging output - ``density`` - how big output image is. - ``locals`` - extra local variables used when evaluating Sage.. code in x. .. warning:: You must have the dvipng (or dvips and convert) installed on your operating system, or this command won't work. """ from sage.misc.latex_macros import sage_latex_macros MACROS="\n" + "\n".join(sage_latex_macros) + "\n" if density is None: density = self.__density if filename is None: filename = 'sage%s'%random.randint(1,100) # to defeat browser caches else: filename = os.path.splitext(filename)[0] # get rid of extension base = tmp_dir() orig_base, filename = os.path.split(os.path.abspath(filename)) if len(filename.split()) > 1: raise ValueError, "filename must contain no spaces" if debug is None: debug = self.__debug x = self._latex_preparse(x, locals) O = open('%s/%s.tex'%(base,filename),'w') if self.__slide: O.write(SLIDE_HEADER) O.write(MACROS) O.write('\\begin{document}\n\n') else: O.write(LATEX_HEADER) O.write(MACROS) O.write('\\begin{document}\n') O.write(x) if self.__slide: O.write('\n\n\\end{document}') else: O.write('\n\n\\end{document}\n') O.close() if not debug: redirect=' 2>/dev/null 1>/dev/null ' else: redirect='' lt = 'cd "%s"&& sage-native-execute latex \\\\nonstopmode \\\\input{%s.tex} %s'%(base, filename, redirect) if have_dvipng(): dvipng = 'sage-native-execute dvipng -q -T bbox -D %s %s.dvi -o %s.png'%(density, filename, filename) cmd = ' && '.join([lt, dvipng]) else: dvips = 'sage-native-execute dvips %s.dvi %s'%(filename, redirect) convert = 'sage-native-execute convert -density %sx%s -trim %s.ps %s.png %s '%\ (density,density, filename, filename, redirect) cmd = ' && '.join([lt, dvips, convert]) if debug: print cmd e = os.system(cmd + ' ' + redirect) if e: print "An error occured." try: print open(filename + '.log').read() except IOError: pass return 'Error latexing slide.' shutil.copy(base + '/' + filename + '.png', orig_base + '/'+filename + '.png') shutil.rmtree(base) return ''