### Installing latexify-py via pip Source: https://github.com/google/latexify_py/blob/main/docs/getting_started.md Installs the latexify-py library using the pip package manager. This is the standard and recommended way to install the library from PyPI. ```Shell pip install latexify-py ``` -------------------------------- ### Installing latexify-py Package (Python) Source: https://github.com/google/latexify_py/blob/main/examples/latexify_examples.ipynb Installs the required `latexify-py` package using pip within a notebook environment. Requires restarting the runtime after installation. ```python # Restart the runtime before running the examples below. %pip install latexify-py ``` -------------------------------- ### Importing Libraries and Checking Version (Python) Source: https://github.com/google/latexify_py/blob/main/examples/latexify_examples.ipynb Imports the `math`, `numpy`, and `latexify` libraries. Optionally imports `math` and `numpy` for later examples. Prints the installed version of `latexify`. ```python import math # Optional import numpy as np # Optional import latexify latexify.__version__ ``` -------------------------------- ### Getting LaTeX string directly with latexify.get_latex Source: https://github.com/google/latexify_py/blob/main/docs/getting_started.md Illustrates how to use the `latexify.get_latex` function to obtain the LaTeX string for a given function directly, without needing to apply a decorator. ```Python def solve(a, b, c): return (-b + math.sqrt(b**2 - 4 * a * c)) / (2 * a) latexify.get_latex(solve) ``` -------------------------------- ### Getting LaTeX source from decorated function Source: https://github.com/google/latexify_py/blob/main/docs/getting_started.md Shows how to retrieve the raw LaTeX string representation generated by `latexify` for a decorated function by printing the function object. ```Python print(solve) ``` -------------------------------- ### Invoking a latexify decorated function Source: https://github.com/google/latexify_py/blob/main/docs/getting_started.md Demonstrates that a function decorated with `latexify.function` remains callable and executes its original Python logic, returning the computed result. ```Python solve(1, 2, 1) ``` -------------------------------- ### Decorating a function with @latexify.function Source: https://github.com/google/latexify_py/blob/main/docs/getting_started.md Applies the `latexify.function` decorator to a Python function. This decorator enables the function to be automatically displayed as a LaTeX formula in environments that support rich output, such as Jupyter notebooks. ```Python @latexify.function def solve(a, b, c): return (-b + math.sqrt(b**2 - 4 * a * c)) / (2 * a) solve ``` -------------------------------- ### Decorating a function with @latexify.expression Source: https://github.com/google/latexify_py/blob/main/docs/getting_started.md Uses the `latexify.expression` decorator, which converts the function body into a LaTeX expression, similar to `latexify.function`, but typically omits the function signature in the output. ```Python @latexify.expression def solve(a, b, c): return (-b + math.sqrt(b**2 - 4 * a * c)) / (2 * a) solve ``` -------------------------------- ### Handling Conditional Logic with latexify.function (Python) Source: https://github.com/google/latexify_py/blob/main/examples/latexify_examples.ipynb Defines a function `sinc` with an `if/else` statement and applies the `@latexify.function` decorator. Shows how `latexify` translates conditional logic into LaTeX. ```python @latexify.function def sinc(x): if x == 0: return 1 else: return math.sin(x) / x sinc ``` -------------------------------- ### Obtaining LaTeX Source with latexify.get_latex (Python) Source: https://github.com/google/latexify_py/blob/main/examples/latexify_examples.ipynb Shows how to use the `latexify.get_latex` function to directly obtain the LaTeX source string for a given Python function without using decorators. ```python # latexify.get_latex obtains the underlying LaTeX expression directly. def solve(a, b, c): return (-b + math.sqrt(b**2 - 4*a*c)) / (2*a) latexify.get_latex(solve) ``` -------------------------------- ### Generating LaTeX Equation with latexify.function (Python) Source: https://github.com/google/latexify_py/blob/main/examples/latexify_examples.ipynb Defines a Python function `solve` using the `@latexify.function` decorator to generate its LaTeX representation. Shows how to call the function, print its LaTeX source, display it, and write the source to a file. ```python @latexify.function def solve(a, b, c): return (-b + math.sqrt(b**2 - 4*a*c)) / (2*a) print(solve(1, 4, 3)) # Invoking the function works as expected. print(solve) # Printing the function shows the underlying LaTeX source. solve # Displays the expression. # Writes the underlying LaTeX source into a file. with open("compiled.tex", "w") as fp: print(solve, file=fp) ``` -------------------------------- ### Handling Elif/Nested Conditionals with latexify.function (Python) Source: https://github.com/google/latexify_py/blob/main/examples/latexify_examples.ipynb Defines a recursive Fibonacci function `fib` using `if/elif/else` and applies the `@latexify.function` decorator. Demonstrates how `latexify` handles and unrolls `elif` or nested `else-if` structures. ```python # Elif or nested else-if are unrolled. @latexify.function def fib(x): if x == 0: return 0 elif x == 1: return 1 else: return fib(x-1) + fib(x-2) fib ``` -------------------------------- ### Handling Matrix Operations with latexify.function (Python) Source: https://github.com/google/latexify_py/blob/main/examples/latexify_examples.ipynb Defines a function `transform` that performs matrix operations using NumPy arrays and the `@` operator. Applies the `@latexify.function` decorator with `reduce_assignments=True` and `use_math_symbols=True` to generate LaTeX for the matrix transformation. ```python # Matrix support. @latexify.function(reduce_assignments=True, use_math_symbols=True) def transform(x, y, a, b, theta, s, t): cos_t = math.cos(theta) sin_t = math.sin(theta) scale = np.array([[a, 0, 0], [0, b, 0], [0, 0, 1]]) rotate = np.array([[cos_t, -sin_t, 0], [sin_t, cos_t, 0], [0, 0, 1]]) move = np.array([[1, 0, s], [0, 1, t], [0, 0, 1]]) return move @ rotate @ scale @ np.array([[x], [y], [1]]) transform ``` -------------------------------- ### Generating LaTeX Expression with latexify.expression (Python) Source: https://github.com/google/latexify_py/blob/main/examples/latexify_examples.ipynb Uses the `@latexify.expression` decorator to generate a LaTeX expression from a Python function. Unlike `@latexify.function`, this decorator omits the function signature in the generated LaTeX. ```python # latexify.expression works similarly, but does not output the signature. @latexify.expression def solve(a, b, c): return (-b + math.sqrt(b**2 - 4*a*c)) / (2*a) solve ``` -------------------------------- ### Generating Algorithmic Environment with latexify.algorithmic (Python) Source: https://github.com/google/latexify_py/blob/main/examples/latexify_examples.ipynb Defines a recursive Fibonacci function `fib` and applies the `@latexify.algorithmic` decorator. Shows how this decorator generates a LaTeX algorithmic environment representation of the function's control flow instead of a mathematical equation. ```python # latexify.algorithmic generates an algorithmic environment instead of an equation. @latexify.algorithmic def fib(x): if x == 0: return 0 elif x == 1: return 1 else: return fib(x-1) + fib(x-2) fib ``` -------------------------------- ### Handling Loops and Conditionals with latexify.algorithmic (Python) Source: https://github.com/google/latexify_py/blob/main/examples/latexify_examples.ipynb Defines a function `collatz` implementing the Collatz conjecture using a `while` loop and `if/else` statements. Applies the `@latexify.algorithmic` decorator to show how it translates loops and control flow into a LaTeX algorithmic environment. ```python # Another example: latexify.algorithmic supports usual control flows. @latexify.algorithmic def collatz(x): n = 0 while x > 1: n = n + 1 if x % 2 == 0: x = x // 2 else: x = 3 * x + 1 return n collatz ``` -------------------------------- ### Converting Math Symbols with latexify.function (Python) Source: https://github.com/google/latexify_py/blob/main/examples/latexify_examples.ipynb Defines a function `greek` using the `@latexify.function` decorator with `use_math_symbols=True`. Shows how this option automatically converts certain Python math symbols and variable names (like Greek letters) into their LaTeX equivalents. ```python # Some math symbols are converted automatically. @latexify.function(use_math_symbols=True) def greek(alpha, beta, gamma, Omega): return alpha * beta + math.gamma(gamma) + Omega greek ``` -------------------------------- ### Reducing Assignments with latexify.function (Python) Source: https://github.com/google/latexify_py/blob/main/examples/latexify_examples.ipynb Defines a function `f` with intermediate variable assignments and applies the `@latexify.function` decorator with `reduce_assignments=True`. Shows how this option simplifies the generated LaTeX by substituting variables to produce a single expression. ```python # Assignments can be reduced into one expression. @latexify.function(reduce_assignments=True) def f(a, b, c): discriminant = b**2 - 4 * a * c numerator = -b + math.sqrt(discriminant) denominator = 2 * a return numerator / denominator f ``` -------------------------------- ### Replacing Identifiers with latexify.function (Python) Source: https://github.com/google/latexify_py/blob/main/examples/latexify_examples.ipynb Defines a dictionary `identifiers` to map Python names to desired LaTeX names. Uses the `@latexify.function` decorator with the `identifiers` argument to replace function names, arguments, and variables in the generated LaTeX output. ```python # Function names, arguments, variables can be replaced. identifiers = { "my_function": "f", "my_inner_function": "g", "my_argument": "x", } @latexify.function(identifiers=identifiers) def my_function(my_argument): return my_inner_function(my_argument) my_function ``` -------------------------------- ### Applying Identifiers with latexify.function (Python) Source: https://github.com/google/latexify_py/blob/main/docs/parameters.md Demonstrates how to use the `identifiers` parameter in `latexify.function` to replace variable and function names in the generated LaTeX output with custom strings defined in a dictionary. ```Python identifiers = { "my_function": "f", "my_inner_function": "g", "my_argument": "x", } @latexify.function(identifiers=identifiers) def my_function(my_argument): return my_inner_function(my_argument) my_function ``` -------------------------------- ### Using Set Symbols with latexify.function (Python) Source: https://github.com/google/latexify_py/blob/main/docs/parameters.md Demonstrates the `use_set_symbols=True` parameter, which converts standard Python bitwise operators (`&`, `|`, `-`, `^`, `<`, `<=`, `>`, `>=`) into their corresponding LaTeX set notation symbols. ```Python @latexify.function(use_set_symbols=True) def f(x, y): return x & y, x | y, x - y, x ^ y, x < y, x <= y, x > y, x >= y f ``` -------------------------------- ### Controlling Signature Output with latexify.function (Python) Source: https://github.com/google/latexify_py/blob/main/docs/parameters.md Shows how `use_signature=False` prevents `latexify.function` from including the function signature (`f(a, b, c) = ...`) in the generated LaTeX output, resulting in just the expression. Requires the `math` module for `sqrt`. ```Python @latexify.function(use_signature=False) def f(a, b, c): return (-b + math.sqrt(b**2 - 4 * a * c)) / (2 * a) f ``` -------------------------------- ### Reducing Assignments with latexify.function (Python) Source: https://github.com/google/latexify_py/blob/main/docs/parameters.md Shows how the `reduce_assignments=True` parameter simplifies the LaTeX output by composing intermediate variables defined before the return statement into a single expression. Requires the `math` module for `sqrt`. ```Python @latexify.function(reduce_assignments=True) def f(a, b, c): discriminant = b**2 - 4 * a * c numerator = -b + math.sqrt(discriminant) denominator = 2 * a return numerator / denominator f ``` -------------------------------- ### Using Math Symbols with latexify.function (Python) Source: https://github.com/google/latexify_py/blob/main/docs/parameters.md Illustrates the effect of `use_math_symbols=True`, which automatically converts variable names corresponding to mathematical symbols (like alpha, beta, gamma, Omega) into their LaTeX symbol representations. Requires the `math` module for `gamma`. ```Python @latexify.function(use_math_symbols=True) def greek(alpha, beta, gamma, Omega): return alpha * beta + math.gamma(gamma) + Omega greek ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.