Ticket #14256: trac_14256-verb-text-nt.patch

File trac_14256-verb-text-nt.patch, 4.3 KB (added by nthiery, 9 years ago)
  • sage/misc/latex.py

    # HG changeset patch
    # User Nicolas M. Thiery <nthiery@users.sf.net>
    # Date 1363030396 14400
    # Node ID 05e7b56e3f791f52f19f0dc8512464879cfcd77b
    # Parent  af9947274520010a6f6993fc450e15852f4ec7a7
    imported patch blah
    
    diff --git a/sage/misc/latex.py b/sage/misc/latex.py
    a b def str_function(x): 
    311311        '-34.5'
    312312        sage: str_function('+34.5')
    313313        '+34.5'
    314         sage: str_function('hello_world')
    315         '\\verb|hello_world|'
    316         sage: str_function('-1.00000?') # trac 12178
    317         '-1.00000?'
     314        sage: print str_function('hello world!')
     315        \text{hello world!}
     316        sage: print str_function('hello_world')
     317        \verb|hello_world|
     318        sage: print str_function('hello{world')
     319        \verb|hello{world|
     320        sage: print str_function('1 0 0\n  1 0\n    1')
     321        \begin{array}{l}
     322        \verb|1|\phantom{\verb!x!}\verb|0|\phantom{\verb!x!}\verb|0|\\
     323        \phantom{\verb!xx!}\verb|1|\phantom{\verb!x!}\verb|0|\\
     324        \phantom{\verb!xxxx!}\verb|1|
     325        \end{array}
     326        sage: print str_function('-1.00000?') # trac 12178
     327        -1.00000?
    318328    """
    319329    # Check if x is just a number with a possible sign, and/or decimal
    320330    # point, and/or ends with "?"
    321331    if re.match(r'(\+|-)?[0-9]*\.?[0-9]*\??$', x):
    322332        return x
     333    # for strings without {}, newlines, or special caracters, just use
     334    # the standard `\text` function
     335    if not any(c in x for c in "\n{}_^"):
     336        return r"\text{%s}"%x
    323337    # Try to pick a delimiter.
    324338    for delimiter in """|"'`#%&,.:;?!@_~^+-/\=<>()[]{}0123456789E""":
    325339        if delimiter == "E":
    class Latex(LatexCall): 
    973987        sage: latex(FiniteField(25,'a'))
    974988        \Bold{F}_{5^{2}}
    975989        sage: latex("hello")
    976         \verb|hello|
     990        \text{hello}
    977991        sage: LatexExpr(r"\frac{x^2 - 1}{x + 1} = x - 1")
    978992        \frac{x^2 - 1}{x + 1} = x - 1
    979993
    class MathJax: 
    19391953            sage: MathJax().eval(3, mode='inline')
    19401954            <html><script type="math/tex">\newcommand{\Bold}[1]{\mathbf{#1}}3</script></html>
    19411955            sage: MathJax().eval(type(3), mode='inline')
    1942             <html><script type="math/tex">\newcommand{\Bold}[1]{\mathbf{#1}}\verb|<type|\phantom{\verb!x!}\verb|'sage.rings.integer.Integer'>|</script></html>
     1956            <html><script type="math/tex">\newcommand{\Bold}[1]{\mathbf{#1}}\text{<type 'sage.rings.integer.Integer'>}</script></html>
    19431957        """
    19441958        # Get a regular LaTeX representation of x
    19451959        x = latex(x, combine_all=combine_all)
    def repr_lincomb(symbols, coeffs): 
    22622276        sage: t = PolynomialRing(QQ, 't').0
    22632277        sage: from sage.misc.latex import repr_lincomb
    22642278        sage: repr_lincomb(['a', 's', ''], [-t, t - 2, t^12 + 2])
    2265         '-t\\verb|a| + \\left(t - 2\\right)\\verb|s| + \\left(t^{12} + 2\\right)'
     2279        '-t\\text{a} + \\left(t - 2\\right)\\text{s} + \\left(t^{12} + 2\\right)'
    22662280        sage: repr_lincomb(['a', 'b'], [1,1])
    2267         '\\verb|a| + \\verb|b|'
     2281        '\\text{a} + \\text{b}'
    22682282
    22692283    Verify that a certain corner case works (see :trac:`5707` and
    22702284    :trac:`5766`)::
    def pretty_print (*args): 
    23602374        sage: pretty_print(ZZ)  # indirect doctest
    23612375        <html><script type="math/tex">\newcommand{\Bold}[1]{\mathbf{#1}}\Bold{Z}</script></html>
    23622376        sage: pretty_print("Integers = ", ZZ) # trac 11775
    2363         <html><script type="math/tex">\newcommand{\Bold}[1]{\mathbf{#1}}\verb|Integers|\phantom{\verb!x!}\verb|=| \Bold{Z}</script></html>
     2377        <html><script type="math/tex">\newcommand{\Bold}[1]{\mathbf{#1}}\text{Integers = } \Bold{Z}</script></html>
    23642378
    23652379    To typeset LaTeX code as-is, use :class:`LatexExpr`::
    23662380
    def pretty_print_default(enable=True): 
    24112425
    24122426        sage: pretty_print_default(True)
    24132427        sage: sys.displayhook
    2414         <html><script type="math/tex">\newcommand{\Bold}[1]{\mathbf{#1}}\verb|<function|\phantom{\verb!x!}\verb|pretty_print|\phantom{\verb!x!}\verb|at|\phantom{\verb!x!}\verb|...|</script></html>
     2428        <html><script type="math/tex">\newcommand{\Bold}[1]{\mathbf{#1}}\verb|<function|\phantom{\verb!x!}\verb|pretty_print|\phantom{\verb!x!}\verb|at|\phantom{\verb!x!}\verb|...>|</script></html>
    24152429        sage: pretty_print_default(False)
    24162430        sage: sys.displayhook == sys.__displayhook__
    24172431        True