Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attempt to fix latex printing error #9202 and added to doc modules #9360

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
147 changes: 147 additions & 0 deletions doc/src/modules/functions/elementary.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ Examples::
acos
----

Returns the inverse cosine value of the argument in radians.

Examples::

>>> from sympy.functions import acos
>>> acos(0)
pi/2
>>> acos(1)
0

.. autoclass:: sympy.functions.elementary.trigonometric.acos
:members:

Expand All @@ -34,6 +44,16 @@ acosh
acot
----

Returns the inverse cotangent value of the argument in radians.

Examples::

>>> from sympy.functions import acot
>>> acot(0)
pi/2
>>> acot(1)
pi/4

.. autoclass:: sympy.functions.elementary.trigonometric.acot
:members:

Expand Down Expand Up @@ -66,6 +86,16 @@ Examples::
asin
----

Returns the inverse sine value of the argument in radians.

Examples::

>>> from sympy.functions import asin
>>> asin(0)
0
>>> asin(1)
pi/2

.. autoclass:: sympy.functions.elementary.trigonometric.asin
:members:

Expand All @@ -78,6 +108,16 @@ asinh
atan
----

Returns the inverse tangent value of the argument in radians.

Examples::

>>> from sympy.functions import atan
>>> atan(0)
0
>>> atan(1)
pi/4

.. autoclass:: sympy.functions.elementary.trigonometric.atan
:members:

Expand Down Expand Up @@ -130,6 +170,18 @@ Examples::
cos
---

Returns the trigonmetric cosine value of the argument. Given that the argument is an angle value in radians, the cosine of the argument is the ratio of the length of the adjacent side to the length of the hypotenuse of a triangle.

Examples::

>>> from sympy.functions import cos
>>> cos(pi)
-1
>>> cos(pi/3)
1/2
>>> cos(-5*pi/6)
-sqrt(3)/2

.. autoclass:: sympy.functions.elementary.trigonometric.cos
:members:

Expand All @@ -141,6 +193,18 @@ cosh
cot
---

Returns the trigonmetric cotangent value of the argument. Given that the argument is an angle value in radians, the cotangent of the argument is the ratio of the length of the adjacent side to the length of the opposite side of a triangle.

Examples::

>>> from sympy.functions import cot
>>> cot(pi)
zoo
>>> cot(pi/3)
sqrt(3)/3
>>> cot(-5*pi/6)
sqrt(3)

.. autoclass:: sympy.functions.elementary.trigonometric.cot
:members:

Expand All @@ -153,6 +217,8 @@ coth
exp
---

Returns the

.. autoclass:: sympy.functions.elementary.exponential.exp
:members:

Expand Down Expand Up @@ -210,6 +276,18 @@ LambertW
log
---

Returns the logarithm of the argument with default base argument, e. A different base can be specified.

Examples::

>>> from sympy.functions import log
>>> log(E)
1
>>> log(4, 2)
2
>>> log(100000, 10)
5

.. autoclass:: sympy.functions.elementary.exponential.log
:members:

Expand Down Expand Up @@ -240,6 +318,14 @@ Max

Returns the maximum of two (comparable) expressions

Examples::

>>> from sympy.functions import Max
>>> Max(1, 2)
2
>>> Max(5, 5)
5

It is named ``Max`` and not ``max`` to avoid conflicts with the built-in function ``max``.

.. autoclass:: sympy.functions.elementary.miscellaneous.Max
Expand Down Expand Up @@ -274,16 +360,57 @@ Examples::
root
----

Returns the i-th root of an argument.

Examples::

>>> from sympy.functions import root
>>> from sympy.abc import x, i
>>> root(x, 2)
sqrt(x)
>>> root(x, 5)
x**(1/5)
>>> root(x, i)
x**(1/i)

.. autofunction:: sympy.functions.elementary.miscellaneous.root

RoundFunction
-------------

Returns the rounded value of an argument with n decimal places. If the number of decimal places is not specified, it returns the nearest integer value.

round(x, n)

Examples::

>>> from sympy.functions import round
>>> round(10.5)
10
>>> round(10.6)
11
>>> round(10.1234567, 1)
10.1
>>> round(10.1234567, 3)
10.123

.. autoclass:: sympy.functions.elementary.integers.RoundFunction

sin
---

Returns the trigonmetric sine value of the argument. Given that the argument is an angle value in radians, the sine of the argument is the ratio of the length of the opposite side to the length of the hypotenuse of a triangle.

Examples::

>>> from sympy.functions import sin
>>> sin(pi)
0
>>> sin(pi/3)
sqrt(3)/2
>>> sin(-5*pi/6)
-1/2

.. autoclass:: sympy.functions.elementary.trigonometric.sin
:members:

Expand All @@ -303,6 +430,14 @@ Returns the square root of an expression. It is equivalent to raise to ``Rationa
>>> sqrt(2) == 2**Rational(1,2)
True

Examples::

>>> from sympy.functions import sqrt
>>> sqrt(4)
2
>>> sqrt(10)
sqrt(10)

.. autofunction:: sympy.functions.elementary.miscellaneous.sqrt

sign
Expand All @@ -314,6 +449,18 @@ sign
tan
---

Returns the trigonmetric tangent value of the argument. Given that the argument is an angle value in radians, the tangent of the argument is the ratio of the length of the opposite side to the length of the adjacent side of a triangle.

Examples::

>>> from sympy.functions import tan
>>> tan(pi)
0
>>> tan(pi/3)
sqrt(3)
>>> tan(-5*pi/6)
sqrt(3)/3

.. autoclass:: sympy.functions.elementary.trigonometric.tan
:members:

Expand Down
43 changes: 3 additions & 40 deletions sympy/printing/latex.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,8 @@ def _print_Mul(self, expr):

def convert(expr):
if not expr.is_Mul:
if denom == 1:
return "(" + str(self._print(expr)) + ")"
return str(self._print(expr))
else:
_tex = last_term_tex = ""
Expand Down Expand Up @@ -634,7 +636,6 @@ def _print_Function(self, expr, exp=None):
symbols. For multi-letter function names that LaTeX does not know
about, (e.g., Li, sech) use \operatorname{} so that the function name
is rendered in Roman font and LaTeX handles spacing properly.

expr is the expression involving the function
exp is an exponent
'''
Expand Down Expand Up @@ -1815,12 +1816,9 @@ def translate(s):
r'''
Check for a modifier ending the string. If present, convert the
modifier to latex and translate the rest recursively.

Given a description of a Greek letter or other special character,
return the appropriate latex.

Let everything else pass as given.

>>> from sympy.printing.latex import translate
>>> translate('alphahatdotprime')
"{\\dot{\\hat{\\alpha}}}'"
Expand All @@ -1841,19 +1839,15 @@ def translate(s):
def latex(expr, **settings):
r"""
Convert the given expression to LaTeX representation.

>>> from sympy import latex, pi, sin, asin, Integral, Matrix, Rational
>>> from sympy.abc import x, y, mu, r, tau

>>> print(latex((2*tau)**Rational(7,2)))
8 \sqrt{2} \tau^{\frac{7}{2}}

order: Any of the supported monomial orderings (currently "lex", "grlex", or
"grevlex"), "old", and "none". This parameter does nothing for Mul objects.
Setting order to "old" uses the compatibility ordering for Add defined in
Printer. For very large expressions, set the 'order' keyword to 'none' if
speed is a concern.

mode: Specifies how the generated code will be delimited. 'mode' can be one
of 'plain', 'inline', 'equation' or 'equation*'. If 'mode' is set to
'plain', then the resulting code will not be delimited at all (this is the
Expand All @@ -1862,104 +1856,73 @@ def latex(expr, **settings):
enclosed in the 'equation' or 'equation*' environment (remember to import
'amsmath' for 'equation*'), unless the 'itex' option is set. In the latter
case, the ``$$ $$`` syntax is used.

>>> print(latex((2*mu)**Rational(7,2), mode='plain'))
8 \sqrt{2} \mu^{\frac{7}{2}}

>>> print(latex((2*tau)**Rational(7,2), mode='inline'))
$8 \sqrt{2} \tau^{\frac{7}{2}}$

>>> print(latex((2*mu)**Rational(7,2), mode='equation*'))
\begin{equation*}8 \sqrt{2} \mu^{\frac{7}{2}}\end{equation*}

>>> print(latex((2*mu)**Rational(7,2), mode='equation'))
\begin{equation}8 \sqrt{2} \mu^{\frac{7}{2}}\end{equation}

itex: Specifies if itex-specific syntax is used, including emitting ``$$ $$``.

>>> print(latex((2*mu)**Rational(7,2), mode='equation', itex=True))
$$8 \sqrt{2} \mu^{\frac{7}{2}}$$

fold_frac_powers: Emit "^{p/q}" instead of "^{\frac{p}{q}}" for fractional
powers.

>>> print(latex((2*tau)**Rational(7,2), fold_frac_powers=True))
8 \sqrt{2} \tau^{7/2}

fold_func_brackets: Fold function brackets where applicable.

>>> print(latex((2*tau)**sin(Rational(7,2))))
\left(2 \tau\right)^{\sin{\left (\frac{7}{2} \right )}}
>>> print(latex((2*tau)**sin(Rational(7,2)), fold_func_brackets = True))
\left(2 \tau\right)^{\sin {\frac{7}{2}}}

fold_short_frac: Emit "p / q" instead of "\frac{p}{q}" when the
denominator is simple enough (at most two terms and no powers).
The default value is `True` for inline mode, False otherwise.

>>> print(latex(3*x**2/y))
\frac{3 x^{2}}{y}
>>> print(latex(3*x**2/y, fold_short_frac=True))
3 x^{2} / y

long_frac_ratio: The allowed ratio of the width of the numerator to the
width of the denominator before we start breaking off long fractions.
The default value is 2.

>>> print(latex(Integral(r, r)/2/pi, long_frac_ratio=2))
\frac{\int r\, dr}{2 \pi}
>>> print(latex(Integral(r, r)/2/pi, long_frac_ratio=0))
\frac{1}{2 \pi} \int r\, dr

mul_symbol: The symbol to use for multiplication. Can be one of None,
"ldot", "dot", or "times".

>>> print(latex((2*tau)**sin(Rational(7,2)), mul_symbol="times"))
\left(2 \times \tau\right)^{\sin{\left (\frac{7}{2} \right )}}

inv_trig_style: How inverse trig functions should be displayed. Can be one
of "abbreviated", "full", or "power". Defaults to "abbreviated".

>>> print(latex(asin(Rational(7,2))))
\operatorname{asin}{\left (\frac{7}{2} \right )}
>>> print(latex(asin(Rational(7,2)), inv_trig_style="full"))
\arcsin{\left (\frac{7}{2} \right )}
>>> print(latex(asin(Rational(7,2)), inv_trig_style="power"))
\sin^{-1}{\left (\frac{7}{2} \right )}

mat_str: Which matrix environment string to emit. "smallmatrix", "matrix",
"array", etc. Defaults to "smallmatrix" for inline mode, "matrix" for
matrices of no more than 10 columns, and "array" otherwise.

>>> print(latex(Matrix(2, 1, [x, y])))
\left[\begin{matrix}x\\y\end{matrix}\right]

>>> print(latex(Matrix(2, 1, [x, y]), mat_str = "array"))
\left[\begin{array}{c}x\\y\end{array}\right]

mat_delim: The delimiter to wrap around matrices. Can be one of "[", "(",
or the empty string. Defaults to "[".

>>> print(latex(Matrix(2, 1, [x, y]), mat_delim="("))
\left(\begin{matrix}x\\y\end{matrix}\right)

symbol_names: Dictionary of symbols and the custom strings they should be
emitted as.

>>> print(latex(x**2, symbol_names={x:'x_i'}))
x_i^{2}

``latex`` also supports the builtin container types list, tuple, and
dictionary.

>>> print(latex([2/x, y], mode='inline'))
$\left [ 2 / x, \quad y\right ]$

"""

return LatexPrinter(settings).doprint(expr)


def print_latex(expr, **settings):
"""Prints LaTeX representation of the given expression."""
print(latex(expr, **settings))
print(latex(expr, **settings))