# HG changeset patch # User Nicolas M. Thiery # Date 1241113197 -7200 # Node ID 0d317e789b92dcc4897309b3bdfae6fdbe9cbdb7 # Parent 1fe54e10eb4e6edc1427a59b71f24437e0ee24ee #5920: Implements view(object, viewer='pdf') This patch implements: {{{ sage: view(object, viewer = "pdf") }}} Typical use cases: - you prefer your pdf browser to your dvi browser - you want to view latex snippets which are not displayed well in dvi viewers (e.g. tikzpicture) Potential extensions: {{{view(object, viewer='png')}}}, {{{view(object, viewer='html')}}} diff --git a/sage/misc/latex.py b/sage/misc/latex.py --- a/sage/misc/latex.py +++ b/sage/misc/latex.py @@ -763,7 +763,7 @@ def jsmath(x, mode='display'): def typeset(x): return JSMath().eval(x, mode='inline') -def view(objects, title='SAGE', debug=False, sep='', tiny=False, **kwds): +def view(objects, title='SAGE', debug=False, sep='', tiny=False, viewer = "dvi", **kwds): r"""nodetex Compute a latex representation of each object in objects, compile, and display typeset. If used from the command line, this requires @@ -784,7 +784,8 @@ def view(objects, title='SAGE', debug=Fa math objects - ``tiny`` - bool (default: False): use tiny font. - + + - ``viewer`` - string (default: 'dvi'): 'dvi' or 'pdf' OUTPUT: Display typeset objects. @@ -795,7 +796,7 @@ def view(objects, title='SAGE', debug=Fa (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. + these lines. The viewer argument is only relevant in that case. The value of ``sep`` is inserted between each element of the list ``objects``; you can, for example, add vertical space between @@ -815,8 +816,23 @@ def view(objects, title='SAGE', debug=Fa sage: view(3) \newcommand{\Bold}[1]{\mathbf{#1}}3 sage: sage.misc.latex.EMBEDDED_MODE = False + + + In non notebook mode, this opens a dvi file by default:: + + sage: view(3) # todo: not implemented + + Another viewer can be specified instead:: + + sage: view(3, viewer = "dvi") # todo: not implemented + sage: view(3, viewer = "pdf") # todo: not implemented + + Note: those are actually implemented but we do not want to run + the tests above. + """ if EMBEDDED_MODE: + assert viewer == "dvi" print typeset(objects) return @@ -824,8 +840,16 @@ def view(objects, title='SAGE', debug=Fa 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() + from sage.misc.viewer import dvi_viewer, pdf_viewer #, png_viewer, browser + extension = viewer + if viewer == "dvi": + viewer = dvi_viewer() + latex = 'latex' + elif viewer == "pdf": + viewer = pdf_viewer() + latex = 'pdflatex' + else: + raise ValueError, "format must be either 'dvi' or 'pdf'" 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)) @@ -835,7 +859,7 @@ def view(objects, title='SAGE', debug=Fa # 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.write('%s \\\\nonstopmode \\\\input{sage.tex}; %s sage.%s ; sleep 1 rm sage.* macros.* go ; cd .. ; rmdir %s' % (latex, viewer, extension, tmp)) O.close() if not debug: direct = '1>/dev/null 2>/dev/null'