Skip to content

Commit 1eb9421

Browse files
author
Yusuke Oda
authored
add documentation. (#98)
* add documentation. * fix * fix * Update parameters.md * fix * fix
1 parent 78a6d01 commit 1eb9421

File tree

4 files changed

+250
-14
lines changed

4 files changed

+250
-14
lines changed

README.md

+42-14
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,55 @@
1-
# latexify_py
2-
A Python package that generates LaTeX math description from Python functions.
1+
# `latexify`
2+
3+
`latexify` is a Python package to compile a fragment of Python source code to a
4+
corresponding $\LaTeX$ expression.
5+
6+
`latexify` provides the following functionalities:
7+
8+
* Libraries to compile Python source code or AST to $\LaTeX$.
9+
* IPython classes to pretty-print compiled functions.
10+
311

412
## FAQs
5-
1. Which Python version? ... Using 3.10 AST.
6-
2. GPT-3? ... No, rule-based.
713

8-
## Installation
14+
1. *Which Python versions are supported?*
15+
16+
Syntaxes on **Pythons 3.7 to 3.10** are officially supported, or will be supported.
17+
18+
2. *Which technique is used?*
19+
20+
`latexify` is implemented as a rule-based system on the official `ast` package.
921

10-
```shell
11-
pip install latexify-py
12-
```
22+
3. *Are "AI" techniques adopted?*
1323

14-
## Example
24+
`latexify` is based on traditional parsing techniques.
25+
If the "AI" meant some techniques around machine learning, the answer is no.
1526

16-
Follow provided example on [Google Colaboratory](https://colab.research.google.com/drive/1MuiawKpVIZ12MWwyYuzZHmbKThdM5wNJ?usp=sharing).
1727

18-
## How to Contribute
19-
To contribute to this Project refer [CONTRIBUTING.md](https://github.com/google/latexify_py/blob/develop/CONTRIBUTING.md) file.
28+
## Getting started
29+
30+
We prepared a
31+
[Google Colaboratory notebook](https://colab.research.google.com/drive/1MuiawKpVIZ12MWwyYuzZHmbKThdM5wNJ?usp=sharing)
32+
that provides several examples to use this package.
33+
34+
See also the official [documentation](docs/index.md) for more details.
35+
36+
## How to Contribute
37+
38+
To contribute to this project, please refer
39+
[CONTRIBUTING.md](https://github.com/google/latexify_py/blob/develop/CONTRIBUTING.md).
40+
2041

2142
## Disclaimer
2243

23-
This is not an officially supported Google product.
44+
This software is currently hosted on https://github.com/google, but not officially
45+
supported by Google.
46+
47+
If you have any issues and/or questions about this software, please visit the
48+
[issue tracker](https://github.com/google/latexify_py/issues)
49+
or contact the [main maintainer](https://github.com/odashi).
50+
2451

2552
## License
2653

27-
This Repository follows [Apache License 2.0](https://github.com/google/latexify_py/blob/develop/LICENSE).
54+
This software adopts the
55+
[Apache License 2.0](https://github.com/google/latexify_py/blob/develop/LICENSE).

docs/getting_started.md

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# Getting started
2+
3+
This document describes how to use `latexify` with your Python code.
4+
5+
6+
## Installation
7+
8+
`lateixfy` depends on only Python libraries at this point.
9+
You can simply install `latexify` via `pip`:
10+
11+
```shell
12+
$ pip install latexify-py
13+
```
14+
15+
Note that you have to install `latexify-py` rather than `latexify`.
16+
17+
18+
## Using `latexify` in Jupyter
19+
20+
`latexify.function` decorator function wraps your functions to pretty-print them as
21+
corresponding LaTeX formulas.
22+
Jupyter recoginzes this wrapper and try to print LaTeX instead of the original function.
23+
24+
The following snippet:
25+
26+
```python
27+
@latexify.function
28+
def solve(a, b, c):
29+
return (-b + math.sqrt(b**2 - 4 * a * c)) / (2 * a)
30+
31+
solve
32+
```
33+
34+
will print the following formula to the output:
35+
36+
$$ \mathrm{solve}(a, b, c) = \frac{-b + \sqrt{b^2 - 4ac}}{2a} $$
37+
38+
39+
Invoking wrapped functions work transparently as the original function.
40+
41+
```python
42+
solve(1, 2, 1)
43+
```
44+
45+
```
46+
-1.0
47+
```
48+
49+
Applying `str` to the wrapped function returns the underlying LaTeX source.
50+
51+
```python
52+
print(solve)
53+
```
54+
55+
```
56+
\\mathrm{f}(n) = \\frac{-b + \\sqrt{b^{{2}} - {4}ac}}{{2}a}
57+
```
58+
59+
`latexify.expression` works similarly to `latexify.function`,
60+
but it prints the function without its signature:
61+
```python
62+
@latexify.expression
63+
def solve(a, b, c):
64+
return (-b + math.sqrt(b**2 - 4 * a * c)) / (2 * a)
65+
66+
solve
67+
```
68+
69+
$$ \frac{-b + \sqrt{b^2 - 4ac}}{2a} $$
70+
71+
72+
## Obtaining LaTeX expression directly
73+
74+
You can also use `latexify.get_latex`, which takes a function and directly returns the
75+
LaTeX expression corresponding to the given function.
76+
77+
The same parameters with `latexify.function` can be applied to `latexify.get_latex` as
78+
well.
79+
80+
```python
81+
def solve(a, b, c):
82+
return (-b + math.sqrt(b**2 - 4 * a * c)) / (2 * a)
83+
84+
latexify.get_latex(solve)
85+
```
86+
87+
```
88+
\\mathrm{f}(n) = \\frac{-b + \\sqrt{b^{{2}} - {4}ac}}{{2}a}
89+
```

docs/index.md

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# `latexify` documentation
2+
3+
## Index
4+
5+
* [Getting started](getting_started.md)
6+
* [Parameters](parameters.md)
7+
8+
## External resources
9+
10+
* [Examples on Google Colaboratory](https://colab.research.google.com/drive/1MuiawKpVIZ12MWwyYuzZHmbKThdM5wNJ?usp=sharing)

docs/parameters.md

+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
# `latexify` parameters
2+
3+
This document describes the list of parameters to control the behavior of `latexify`.
4+
5+
6+
## `identifiers: dict[str, str]`
7+
8+
Key-value pair of identifiers to replace.
9+
10+
```python
11+
identifiers = {
12+
"my_function": "f",
13+
"my_inner_function": "g",
14+
"my_argument": "x",
15+
}
16+
17+
@latexify.function(identifiers=identifiers)
18+
def my_function(my_argument):
19+
return my_inner_function(my_argument)
20+
21+
my_function
22+
```
23+
24+
$$\mathrm{f}(x) = \mathrm{g}\left(x\right)$$
25+
26+
27+
## `reduce_assignments: bool`
28+
29+
Whether to compose all variables defined before the `return` statement.
30+
31+
The current version of `latexify` recognizes only the assignment statements.
32+
Analyzing functions with other control flows may raise errors.
33+
34+
```python
35+
@latexify.function(reduce_assignments=True)
36+
def f(a, b, c):
37+
discriminant = b**2 - 4 * a * c
38+
numerator = -b + math.sqrt(discriminant)
39+
denominator = 2 * a
40+
return numerator / denominator
41+
42+
f
43+
```
44+
45+
$$\mathrm{f}(a, b, c) = \frac{-b + \sqrt{b^{{2}} - {4} a c}}{{2} a}$$
46+
47+
48+
## `use_math_symbols: bool`
49+
50+
Whether to automatically convert variables with symbol names into LaTeX symbols or not.
51+
52+
```python
53+
@latexify.function(use_math_symbols=True)
54+
def greek(alpha, beta, gamma, Omega):
55+
return alpha * beta + math.gamma(gamma) + Omega
56+
57+
greek
58+
```
59+
60+
$$\mathrm{greek}({\alpha}, {\beta}, {\gamma}, {\Omega}) = {\alpha} {\beta} + \Gamma\left({{\gamma}}\right) + {\Omega}$$
61+
62+
63+
## `use_raw_function_name: bool`
64+
65+
Whether to use the original string as the function name or not.
66+
67+
```python
68+
@latexify.function(use_raw_function_name=True)
69+
def quadratic_solution(a, b, c):
70+
return (-b + math.sqrt(b**2 - 4 * a * c)) / (2 * a)
71+
72+
f
73+
```
74+
75+
$$\mathrm{quadratic\_solution}(a, b, c) = \frac{-b + \sqrt{b^{{2}} - {4} a c}}{{2} a}$$
76+
77+
(note that GitHub's LaTeX renderrer does not process underscores correctly.)
78+
79+
## `use_set_symbols: bool`
80+
81+
Whether to use binary operators for set operations or not.
82+
83+
```python
84+
@latexify.function(use_set_symbols=True)
85+
def f(x, y):
86+
return x & y, x | y, x - y, x ^ y, x < y, x <= y, x > y, x >= y
87+
88+
f
89+
```
90+
91+
$$\mathrm{f}(x, y) = \left( x \cap y\space,\space x \cup y\space,\space x \setminus y\space,\space x \mathbin{\triangle} y\space,\space {x \subset y}\space,\space {x \subseteq y}\space,\space {x \supset y}\space,\space {x \supseteq y}\right)$$
92+
93+
94+
## `use_signature: bool`
95+
96+
Whether to output the function signature or not.
97+
98+
The default value of this flag depends on the frontend function.
99+
`True` is used in `latexify.function`, while `False` is used in `latexify.expression`.
100+
101+
```python
102+
@latexify.function(use_signature=False)
103+
def f(a, b, c):
104+
return (-b + math.sqrt(b**2 - 4 * a * c)) / (2 * a)
105+
106+
f
107+
```
108+
109+
$$\frac{-b + \sqrt{b^{{2}} - {4} a c}}{{2} a}$$

0 commit comments

Comments
 (0)