# HG changeset patch
# User Jeroen Demeyer <jdemeyer@cage.ugent.be>
# Date 1323942049 -3600
# Node ID fd045541a56c3a6a9b0ca5193a59dd15af6169bb
# Parent b72663c760492cc8c360d328a6d370c8f88d2069
Pretty print LatexExpr directly, import LatexExpr globally
diff --git a/sage/misc/all.py b/sage/misc/all.py
a
|
b
|
|
144 | 144 | parent) |
145 | 145 | |
146 | 146 | |
147 | | from latex import latex, view, pretty_print, pretty_print_default |
| 147 | from latex import LatexExpr, latex, view, pretty_print, pretty_print_default |
148 | 148 | |
149 | | # disabled -- nobody uses mathml |
150 | | #from mathml ml |
151 | | |
152 | 149 | from trace import trace |
153 | 150 | |
154 | 151 | from constant_function import ConstantFunction |
diff --git a/sage/misc/latex.py b/sage/misc/latex.py
a
|
b
|
|
7 | 7 | """ |
8 | 8 | |
9 | 9 | #***************************************************************************** |
10 | | # |
11 | | # Sage |
12 | | # |
13 | 10 | # Copyright (C) 2005 William Stein <wstein@gmail.com> |
14 | 11 | # |
15 | 12 | # Distributed under the terms of the GNU General Public License (GPL) |
16 | | # |
17 | | # This code is distributed in the hope that it will be useful, |
18 | | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
19 | | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
20 | | # General Public License for more details. |
21 | | # |
22 | | # The full text of the GPL is available at: |
23 | | # |
| 13 | # as published by the Free Software Foundation; either version 2 of |
| 14 | # the License, or (at your option) any later version. |
24 | 15 | # http://www.gnu.org/licenses/ |
25 | 16 | #***************************************************************************** |
26 | 17 | |
| 18 | |
27 | 19 | EMBEDDED_MODE = False |
28 | 20 | |
29 | 21 | COMMON_HEADER = \ |
… |
… |
|
380 | 372 | |
381 | 373 | class LatexExpr(str): |
382 | 374 | """ |
383 | | LaTeX expression. This is a string. |
| 375 | A class for LaTeX expressions, i.e. the result of a :func:`latex` |
| 376 | call. A ``LatexExpr`` can also be generated directly from a string. |
| 377 | This string is then pretty-printed (see :func:`pretty_print`) as-is. |
384 | 378 | |
385 | 379 | EXAMPLES:: |
386 | 380 | |
387 | | sage: from sage.misc.latex import LatexExpr |
388 | | sage: LatexExpr(3) |
389 | | 3 |
390 | | sage: LatexExpr(False) |
391 | | False |
392 | | sage: LatexExpr(pi) |
393 | | pi |
394 | | sage: LatexExpr(3.14) |
395 | | 3.14000000000000 |
396 | | sage: LatexExpr("abc") |
397 | | abc |
| 381 | sage: latex(x^20 + 1) |
| 382 | x^{20} + 1 |
| 383 | sage: LatexExpr(r"\frac{x^2 + 1}{x - 2}") |
| 384 | \frac{x^2 + 1}{x - 2} |
| 385 | |
| 386 | ``LatexExpr`` simply converts to string without doing anything |
| 387 | extra, it does *not* call :func:`latex`:: |
| 388 | |
| 389 | sage: latex(ZZ) |
| 390 | \Bold{Z} |
| 391 | sage: LatexExpr(ZZ) |
| 392 | Integer Ring |
| 393 | |
| 394 | The result of :func:`latex` is of type ``LatexExpr``:: |
| 395 | |
| 396 | sage: L = latex(x^20 + 1) |
| 397 | sage: L |
| 398 | x^{20} + 1 |
| 399 | sage: type(L) |
| 400 | <class 'sage.misc.latex.LatexExpr'> |
398 | 401 | """ |
399 | 402 | def __repr__(self): |
400 | 403 | """ |
401 | 404 | EXAMPLES:: |
402 | 405 | |
403 | | sage: from sage.misc.latex import LatexExpr |
404 | 406 | sage: LatexExpr("abc").__repr__() |
405 | 407 | 'abc' |
406 | 408 | """ |
… |
… |
|
410 | 412 | """ |
411 | 413 | EXAMPLES:: |
412 | 414 | |
413 | | sage: from sage.misc.latex import LatexExpr |
414 | | sage: LatexExpr("abc")._latex_() |
415 | | 'abc' |
| 415 | sage: latex(LatexExpr("abc")) |
| 416 | abc |
416 | 417 | """ |
417 | 418 | return str(self) |
418 | 419 | |
… |
… |
|
1679 | 1680 | sage: JSMath().eval(type(3), mode='inline') |
1680 | 1681 | <html>...\verb|<type|\phantom{x}\verb|'sage.rings.integer.Integer'>|</span></html> |
1681 | 1682 | """ |
1682 | | # If x is already a LaTeX expression, i.e. the output of latex(blah), |
1683 | | # we will treat it as a string, so that we can see the code itself. |
1684 | | if isinstance(x, LatexExpr): |
1685 | | x = str(x) |
1686 | 1683 | # Now get a regular LaTeX representation of x... |
1687 | 1684 | x = latex(x) |
1688 | 1685 | # ... and make it suitable for jsMath, which has issues with < and >. |
… |
… |
|
1875 | 1872 | 'pdf' |
1876 | 1873 | |
1877 | 1874 | """ |
1878 | | if isinstance(objects, LatexExpr): |
1879 | | s = str(objects) |
| 1875 | if tightpage == True: |
| 1876 | latex_options = {'extra_preamble':'\\usepackage[tightpage,active]{preview}\\PreviewEnvironment{page}', |
| 1877 | 'math_left':'\\begin{page}$', 'math_right':'$\\end{page}'} |
1880 | 1878 | else: |
1881 | | if tightpage == True: |
1882 | | latex_options = {'extra_preamble':'\\usepackage[tightpage,active]{preview}\\PreviewEnvironment{page}', |
1883 | | 'math_left':'\\begin{page}$', 'math_right':'$\\end{page}'} |
1884 | | else: |
1885 | | latex_options = {} |
1886 | | s = _latex_file_(objects, title=title, sep=sep, tiny=tiny, debug=debug, **latex_options) |
| 1879 | latex_options = {} |
| 1880 | s = _latex_file_(objects, title=title, sep=sep, tiny=tiny, debug=debug, **latex_options) |
1887 | 1881 | # notebook |
1888 | 1882 | if EMBEDDED_MODE and viewer is None: |
1889 | 1883 | jsMath_okay = True |
… |
… |
|
2130 | 2124 | |
2131 | 2125 | INPUT: |
2132 | 2126 | |
2133 | | - ``object`` - a Sage object |
2134 | | |
2135 | | This function is used in the notebook when the ``Typeset`` button is |
| 2127 | - ``object`` -- a Sage object |
| 2128 | |
| 2129 | This function is used in the notebook when the "Typeset" button is |
2136 | 2130 | checked. |
2137 | 2131 | |
2138 | 2132 | EXAMPLES:: |
2139 | 2133 | |
2140 | | sage: from sage.misc.latex import pretty_print |
2141 | 2134 | sage: pretty_print(ZZ) # indirect doctest |
2142 | 2135 | <html><span class="math">\newcommand{\Bold}[1]{\mathbf{#1}}\Bold{Z}</span></html> |
| 2136 | |
| 2137 | To typeset LaTeX code as-is, use :class:`LatexExpr`:: |
| 2138 | |
| 2139 | sage: pretty_print(LatexExpr(r"\frac{x^2 + 1}{x - 2}")) |
| 2140 | <html><span class="math">\newcommand{\Bold}[1]{\mathbf{#1}}\frac{x^2 + 1}{x - 2}</span></html> |
2143 | 2141 | """ |
2144 | 2142 | if object is None: |
2145 | 2143 | return |