Ticket #6591: trac_6591_view_viewer_tightpage-nt.patch

File trac_6591_view_viewer_tightpage-nt.patch, 5.6 KB (added by Nicolas M. Thiéry, 13 years ago)

Apply only this one (it includes the doc patch)

  • sage/misc/latex.py

    # HG changeset patch
    # User Nicolas M. Thiery <nthiery@users.sf.net>
    # Date 1248384456 -7200
    # Node ID 1854d6c7e956416dffbbb2a616d8fa590a4a68ba
    # Parent  cc6a63da77324a1b009217c47676918e7039ea77
    #6591: Implement view(object, viewer='pdf') and view(object, tightpage = True)
    
    diff --git a/sage/misc/latex.py b/sage/misc/latex.py
    a b def jsmath(x, mode='display'): 
    14091409        x = str(x)
    14101410    return html(delimiter + x + delimiter)
    14111411
    1412 def view(objects, title='SAGE', debug=False, sep='', tiny=False, pdflatex=None, **kwds):
     1412def view(objects, title='SAGE', debug=False, sep='', tiny=False, pdflatex=None, viewer = None, tightpage = None, **kwds):
    14131413    r"""nodetex
    14141414    Compute a latex representation of each object in objects, compile,
    14151415    and display typeset. If used from the command line, this requires
    def view(objects, title='SAGE', debug=Fa 
    14321432   
    14331433    -  ``pdflatex`` - bool (default: False): use pdflatex.
    14341434   
     1435    -  ``viewer`` -- string or None (default: None): specify a viewer to
     1436       use; currently the only options are None and 'pdf'.
     1437   
     1438    -  ``tightpage`` - bool (default: False): use the LaTeX package
     1439       'preview' with the 'tightpage' option.
     1440   
    14351441    OUTPUT: Display typeset objects.
    14361442   
    14371443    This function behaves differently depending on whether in notebook
    14381444    mode or not.
    1439    
    1440     If not in notebook mode, this opens up a window displaying a dvi
    1441     (or pdf) file, displaying the following: the title string is
    1442     printed, centered, at the top. Beneath that, each object in ``objects``
    1443     is typeset on its own line, with the string ``sep`` inserted between
    1444     these lines.
     1445
     1446    If note in notebook mode, the output is displayed in a separate
     1447    viewer displaying a dvi (or pdf) file, with the following: the
     1448    title string is printed, centered, at the top. Beneath that, each
     1449    object in ``objects`` is typeset on its own line, with the string
     1450    ``sep`` inserted between these lines.
    14451451
    14461452    The value of ``sep`` is inserted between each element of the list
    14471453    ``objects``; you can, for example, add vertical space between
    def view(objects, title='SAGE', debug=Fa 
    14491455    adds a horizontal line between objects, and ``sep='\\newpage'``
    14501456    inserts a page break between objects.
    14511457
    1452     If ``pdflatex`` is True, then this produces a pdf file.
     1458    If ``pdflatex`` is ``True``, then this produces a pdf file.
    14531459    Otherwise, it produces a dvi file, and if the program dvipng is
    14541460    installed, it checks the dvi file by trying to convert it to a png
    14551461    file.  If this conversion fails, the dvi file probably contains
    14561462    some postscript special commands or it has other issues which
    14571463    might make displaying it a problem; in this case, the file is
    14581464    converted to a pdf file, which is then displayed.
    1459    
    1460     If in notebook mode, this usually uses jsMath -- see the next
    1461     paragraph for the exception -- to display the output in the
    1462     notebook. Only the first argument, ``objects``, is relevant; the
    1463     others are ignored. If ``objects`` is a list, each object is
    1464     printed on its own line.
     1465
     1466    Setting ``viewer`` to ``'pdf'`` forces the use of a separate
     1467    viewer, even in notebook mode. This also sets ``pdflatex`` to
     1468    ``True``.
     1469
     1470    Setting the option ``tightpage`` to ``True`` tells LaTeX to use
     1471    the package 'preview' with the 'tightpage' option. Then, each
     1472    object is typeset in its own page, and that page is cropped to
     1473    exactly the size of the object. This is typically useful for very
     1474    large pictures (like graphs) generated with tikz. This only works
     1475    when using a separate viewer. Note that the object are currently
     1476    typeset in plain math mode rather than displaymath, because the
     1477    later imposes a limit on the width of the picture. Technically,
     1478    ``tightpage`` adds
     1479
     1480      \\usepackage[tightpage,active]{preview}
     1481      \\PreviewEnvironment{page}
     1482
     1483    to the `\LaTeX` preamble, and replaces the ``\\[`` and ``\\]`` around
     1484    each object by ``\\begin{page}$`` and ``$\\end{page}``.
     1485
     1486
     1487    If in notebook mode with ``viewer`` equal to ``None``, this
     1488    usually uses jsMath -- see the next paragraph for the exception --
     1489    to display the output in the notebook. Only the first argument,
     1490    ``objects``, is relevant; the others are ignored. If ``objects``
     1491    is a list, each object is printed on its own line.
    14651492
    14661493    In the notebook, this *does* *not* use jsMath if the LaTeX code
    14671494    for ``objects`` contains a string in
    def view(objects, title='SAGE', debug=Fa 
    14781505    if isinstance(objects, LatexExpr):
    14791506        s = str(objects)
    14801507    else:
    1481         s = _latex_file_(objects, title=title, sep=sep, tiny=tiny, debug=debug)
     1508        if tightpage == True:
     1509            latex_options = {'extra_preamble':'\\usepackage[tightpage,active]{preview}\\PreviewEnvironment{page}',
     1510                             'math_left':'\\begin{page}$', 'math_right':'$\\end{page}'}
     1511        else:
     1512            latex_options = {}
     1513        s = _latex_file_(objects, title=title, sep=sep, tiny=tiny, debug=debug, **latex_options)
    14821514    # notebook
    1483     if EMBEDDED_MODE:
     1515    if EMBEDDED_MODE and viewer is None:
    14841516        jsMath_okay = True
    14851517        for t in latex.jsmath_avoid_list():
    14861518            if s.find(t) != -1:
    def view(objects, title='SAGE', debug=Fa 
    14981530            print '<html><img src="%s"></html>'%png_link  # put comma at end of line?
    14991531        return
    15001532    # command line
     1533    if viewer == "pdf":
     1534        pdflatex = True
    15011535    if pdflatex is None:
    15021536        pdflatex = _Latex_prefs._option["pdflatex"]
    15031537    tmp = tmp_dir('sage_viewer')