Ticket #5920: view_as_pdf-5920-final.patch

File view_as_pdf-5920-final.patch, 3.8 KB (added by nthiery, 13 years ago)
  • sage/misc/latex.py

    # HG changeset patch
    # User Nicolas M. Thiery <nthiery@users.sf.net>
    # 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 b def jsmath(x, mode='display'): 
    763763def typeset(x):
    764764    return JSMath().eval(x, mode='inline')
    765765
    766 def view(objects, title='SAGE', debug=False, sep='', tiny=False,  **kwds):
     766def view(objects, title='SAGE', debug=False, sep='', tiny=False,  viewer = "dvi", **kwds):
    767767    r"""nodetex
    768768    Compute a latex representation of each object in objects, compile,
    769769    and display typeset. If used from the command line, this requires
    def view(objects, title='SAGE', debug=Fa 
    784784       math objects
    785785   
    786786    -  ``tiny`` - bool (default: False): use tiny font.
    787    
     787
     788    -  ``viewer`` - string (default: 'dvi'): 'dvi' or 'pdf'
    788789   
    789790    OUTPUT: Display typeset objects.
    790791   
    def view(objects, title='SAGE', debug=Fa 
    795796    (or pdf) file, displaying the following: the title string is
    796797    printed, centered, at the top. Beneath that, each object in objects
    797798    is typeset on its own line, with the string sep inserted between
    798     these lines.
     799    these lines. The viewer argument is only relevant in that case.
    799800
    800801    The value of ``sep`` is inserted between each element of the list
    801802    ``objects``; you can, for example, add vertical space between
    def view(objects, title='SAGE', debug=Fa 
    815816        sage: view(3)
    816817        <html><span class="math">\newcommand{\Bold}[1]{\mathbf{#1}}3</span></html>
    817818        sage: sage.misc.latex.EMBEDDED_MODE = False
     819
     820
     821    In non notebook mode, this opens a dvi file by default::
     822
     823        sage: view(3)                   # todo: not implemented
     824
     825    Another viewer can be specified instead::
     826
     827        sage: view(3, viewer = "dvi")   # todo: not implemented
     828        sage: view(3, viewer = "pdf")   # todo: not implemented
     829
     830    Note: those are actually implemented but we do not want to run
     831    the tests above.
     832
    818833    """
    819834    if EMBEDDED_MODE:
     835        assert viewer == "dvi"
    820836        print typeset(objects)
    821837        return
    822838
    def view(objects, title='SAGE', debug=Fa 
    824840        s = str(objects)
    825841    else:
    826842        s = _latex_file_(objects, title=title, debug=debug, sep=sep, tiny=tiny)
    827     from sage.misc.viewer import dvi_viewer
    828     viewer = dvi_viewer()
     843    from sage.misc.viewer import dvi_viewer, pdf_viewer #, png_viewer, browser
     844    extension = viewer
     845    if viewer == "dvi":
     846        viewer = dvi_viewer()
     847        latex = 'latex'
     848    elif viewer == "pdf":
     849        viewer = pdf_viewer()
     850        latex = 'pdflatex'
     851    else:
     852        raise ValueError, "format must be either 'dvi' or 'pdf'"
    829853    tmp = tmp_dir('sage_viewer')
    830854    open('%s/sage.tex'%tmp,'w').write(s)
    831855    os.system('ln -sf %s/common/macros.tex %s'%(SAGE_DOC, tmp))
    def view(objects, title='SAGE', debug=Fa 
    835859   
    836860    # Added sleep 1 to allow viewer to open the file before it gets removed
    837861    # Yi Qiang 2008-05-09
    838     O.write('latex \\\\nonstopmode \\\\input{sage.tex}; %s sage.dvi ; sleep 1 rm sage.* macros.* go ; cd .. ; rmdir %s' % (viewer, tmp))
     862    O.write('%s \\\\nonstopmode \\\\input{sage.tex}; %s sage.%s ; sleep 1 rm sage.* macros.* go ; cd .. ; rmdir %s' % (latex, viewer, extension, tmp))
    839863    O.close()
    840864    if not debug:
    841865        direct = '1>/dev/null 2>/dev/null'