Ticket #6012: pdflatex.patch

File pdflatex.patch, 7.2 KB (added by John Palmieri, 14 years ago)
  • sage/all_notebook.py

    # HG changeset patch
    # User J. H. Palmieri <palmieri@math.washington.edu>
    # Date 1241886557 25200
    # Node ID a51ba08cd446de86aa6e19fb7e6581a30aaf0aee
    # Parent  fec04e16cb8ff3b7824b7dbb55a30120a4c66457
    implement pdflatex
    
    diff -r fec04e16cb8f -r a51ba08cd446 sage/all_notebook.py
    a b  
    1616latex_debug = Latex(debug=True, density=130)
    1717slide = Latex(slide=True, density=256)
    1818slide_debug = Latex(slide=True, debug=True, density=256)
     19pdflatex = Latex(density=130, pdflatex=True)
     20pdflatex_debug = Latex(density=130, pdflatex=True, debug=True)
    1921# we need a global instance of this in order to get %jsmath to work...
    2022jsmath = JSMath()
    2123
  • sage/misc/latex.py

    diff -r fec04e16cb8f -r a51ba08cd446 sage/misc/latex.py
    a b  
    209209        self._option["vector_delimiters"] = list(delimiters)
    210210        self._option["macros"] = ""
    211211        self._option["preamble"] = ""
     212        self._option["pdflatex"] = False
    212213
    213214_Latex_prefs = _Latex_prefs_object()
    214215
     
    262263       on your operating system, or this command won't work.
    263264
    264265    """
    265     def __init__(self, debug=False, slide=False, density=150):
     266    def __init__(self, debug=False, slide=False, density=150, pdflatex=None):
    266267        self.__debug = debug
    267268        self.__slide = slide
     269        self.__pdflatex = pdflatex
    268270        self.__density = density
    269271
    270272    def __call__(self, x):
     
    310312            s = s[:i] + k + t[j+1:]
    311313       
    312314    def eval(self, x, globals, strip=False, filename=None, debug=None,
    313              density=None, locals={}):
     315             density=None, pdflatex=None, locals={}):
    314316        """
    315317        INPUT:
    316318            globals -- a globals dictionary
     
    327329       
    328330        -  ``density`` - how big output image is.
    329331       
     332        -  ``pdflatex`` - whether to use pdflatex.
     333       
    330334        -  ``locals`` - extra local variables used when
    331335           evaluating Sage.. code in x.
    332336
    333337        .. warning::
    334        
    335            You must have dvipng (or dvips and convert) installed on
    336            your operating system, or this command won't work.
    337338
     339           When using latex (the default), you must have 'dvipng' (or
     340           'dvips' and 'convert') installed on your operating system,
     341           or this command won't work.  When using pdflatex, you must
     342           have 'convert' installed.
    338343        """
    339344        MACROS = latex_extra_preamble()
    340345
     
    372377            redirect=' 2>/dev/null 1>/dev/null '
    373378        else:
    374379            redirect=''
    375         lt = 'cd "%s"&& sage-native-execute latex \\\\nonstopmode \\\\input{%s.tex} %s'%(base, filename, redirect)
    376         if have_dvipng():
     380        if pdflatex is None:
     381            if self.__pdflatex is None:
     382                pdflatex = _Latex_prefs._option["pdflatex"]
     383            else:
     384                pdflatex = bool(self.__pdflatex)
     385        if pdflatex:
     386            command = "pdflatex"
     387        else:
     388            command = "latex"
     389        lt = 'cd "%s"&& sage-native-execute %s \\\\nonstopmode \\\\input{%s.tex} %s'%(base, command, filename, redirect)
     390        if have_dvipng() and not pdflatex:
    377391            dvipng = 'sage-native-execute dvipng -q -T bbox -D %s %s.dvi -o %s.png'%(density, filename, filename)
    378392            cmd = ' && '.join([lt, dvipng])
    379            
    380393        else:
     394            # seem to need a larger size when using convert compared to when using dvipng
     395            density = int(1.4 * density / 1.3) 
    381396            dvips = 'sage-native-execute dvips %s.dvi %s'%(filename, redirect)
    382             convert = 'sage-native-execute convert -density %sx%s -trim %s.ps %s.png %s '%\
    383                       (density,density, filename, filename, redirect)
    384             cmd = ' && '.join([lt, dvips, convert])
     397            if pdflatex:
     398                suffix = "pdf"
     399            else:
     400                suffix = "ps"
     401            convert = 'sage-native-execute convert -density %sx%s -trim %s.%s %s.png %s '%\
     402                      (density,density, filename, suffix, filename, redirect)
     403            if pdflatex:
     404                cmd = ' && '.join([lt, convert])
     405            else:               
     406                cmd = ' && '.join([lt, dvips, convert])
    385407        if debug:
    386408            print cmd
    387409        e = os.system(cmd + ' ' + redirect)
     
    670692        """
    671693        _Latex_prefs._option['preamble'] += s
    672694
     695    def pdflatex(self, t = None):
     696        """
     697        Controls whether Sage uses PDFLaTeX or LaTeX when typesetting
     698        with ``view``, in ``%latex`` cells, etc.
     699       
     700        INPUT:
     701       
     702        - ``t`` -- boolean or None
     703
     704        OUTPUT: if t is None, return the current setting (True or False).
     705
     706        If t == True, use PDFLaTeX; otherwise use LaTeX.
     707
     708        EXAMPLES::
     709
     710            sage: latex.pdflatex()
     711            False
     712            sage: latex.pdflatex(True)
     713            sage: latex.pdflatex()
     714            True
     715            sage: latex.pdflatex(False)
     716        """
     717        if t is None:
     718            return _Latex_prefs._option["pdflatex"]
     719        _Latex_prefs._option["pdflatex"] = bool(t)
     720
    673721# Note: latex used to be a separate function, which by default was
    674722# only loaded in command-line mode: in the notebook, all_notebook.py
    675723# defined (and still defines) latex by 'latex = Latex(density=130)'.
     
    900948def typeset(x):
    901949    return JSMath().eval(x, mode='inline')
    902950
    903 def view(objects, title='SAGE', debug=False, sep='', tiny=False, **kwds):
     951def view(objects, title='SAGE', debug=False, sep='', tiny=False, pdflatex=None, **kwds):
    904952    r"""nodetex
    905953    Compute a latex representation of each object in objects, compile,
    906954    and display typeset. If used from the command line, this requires
     
    922970   
    923971    -  ``tiny`` - bool (default: False): use tiny font.
    924972   
     973    -  ``pdflatex`` - bool (default: False): use pdflatex.
    925974   
    926975    OUTPUT: Display typeset objects.
    927976   
     
    9611010        s = str(objects)
    9621011    else:
    9631012        s = _latex_file_(objects, title=title, debug=debug, sep=sep, tiny=tiny)
    964     from sage.misc.viewer import dvi_viewer
    965     viewer = dvi_viewer()
     1013    if pdflatex is None:
     1014        pdflatex = _Latex_prefs._option["pdflatex"]
     1015    if pdflatex:
     1016        from sage.misc.viewer import pdf_viewer
     1017        viewer = pdf_viewer()
     1018        command = "pdflatex"
     1019        suffix = "pdf"
     1020    else:
     1021        from sage.misc.viewer import dvi_viewer
     1022        viewer = dvi_viewer()
     1023        command = "latex"
     1024        suffix = "dvi"
    9661025    tmp = tmp_dir('sage_viewer')
    9671026    open('%s/sage.tex'%tmp,'w').write(s)
    9681027    os.system('ln -sf %s/common/macros.tex %s'%(SAGE_DOC, tmp))
     
    9721031   
    9731032    # Added sleep 1 to allow viewer to open the file before it gets removed
    9741033    # Yi Qiang 2008-05-09
    975     O.write('latex \\\\nonstopmode \\\\input{sage.tex}; %s sage.dvi ; sleep 1 rm sage.* macros.* go ; cd .. ; rmdir %s' % (viewer, tmp))
     1034    O.write('%s \\\\nonstopmode \\\\input{sage.tex}; %s sage.%s ; sleep 1 rm sage.* macros.* go ; cd .. ; rmdir %s' % (command, viewer, suffix, tmp))
    9761035    O.close()
    9771036    if not debug:
    9781037        direct = '1>/dev/null 2>/dev/null'