Skip to content

Commit 60f1c01

Browse files
authored
Merge pull request #36 from dbatten5/docs
Docs
2 parents 63ae1d3 + 9916eae commit 60f1c01

File tree

7 files changed

+162
-11
lines changed

7 files changed

+162
-11
lines changed

.github/workflows/tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,4 +149,4 @@ jobs:
149149
nox --session=coverage -- xml
150150
151151
- name: Upload coverage report
152-
uses: codecov/codecov-action@3.1.2
152+
uses: codecov/codecov-action@v3

docs/index.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,16 @@ end-before: <!-- github-only -->
66

77
[license]: license
88
[contributor guide]: contributing
9-
[command-line reference]: usage
9+
10+
<!-- [command-line reference]: usage -->
1011

1112
```{toctree}
1213
---
1314
hidden:
1415
maxdepth: 1
1516
---
1617
17-
<!-- usage -->
18+
usage
1819
reference
1920
contributing
2021
Code of Conduct <codeofconduct>

docs/usage.md

Lines changed: 140 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,143 @@
11
# Usage
22

3-
```{eval-rst}
4-
.. click:: arraytex.__main__:main
5-
:prog: arraytex
6-
:nested: full
3+
<!-- ```{eval-rst} -->
4+
<!-- .. click:: arraytex.__main__:main -->
5+
<!-- :prog: arraytex -->
6+
<!-- :nested: full -->
7+
<!-- ``` -->
8+
9+
Suppose you want to convert a `numpy.NDArray` object to a LaTeX representation:
10+
11+
```python
12+
>>> import numpy as np
13+
>>> A = np.arange(6).reshape(2, 3)
14+
>>> A
15+
array([[0, 1, 2],
16+
[3, 4, 5]])
17+
```
18+
19+
## To matrix
20+
21+
First import the `to_matrix` function:
22+
23+
```python
24+
>>> from arraytex import to_matrix
25+
```
26+
27+
Then run `to_matrix` with a `numpy.NDArray` object as the first argument:
28+
29+
```python
30+
>>> print(to_matrix(A))
31+
\begin{bmatrix}
32+
0 & 1 & 2 \\
33+
3 & 4 & 5 \\
34+
\end{bmatrix}
35+
```
36+
37+
Different matrix style environment delimiters can be used:
38+
39+
```python
40+
>>> print(to_matrix(A, style="p"))
41+
\begin{pmatrix}
42+
0 & 1 & 2 \\
43+
3 & 4 & 5 \\
44+
\end{pmatrix}
45+
```
46+
47+
So can builtin number formatters:
48+
49+
```python
50+
>>> print(to_matrix(A, num_format=".2e"))
51+
\begin{bmatrix}
52+
0.00\mathrm{e}{+00} & 1.00\mathrm{e}{+00} & 2.00\mathrm{e}{+00} \\
53+
3.00\mathrm{e}{+00} & 4.00\mathrm{e}{+00} & 5.00\mathrm{e}{+00} \\
54+
\end{bmatrix}
55+
```
56+
57+
Prefer scientific notation to e-notation? No problem:
58+
59+
```python
60+
>>> print(to_matrix(A, num_format=".2e", scientific_notation=True))
61+
\begin{bmatrix}
62+
0.00 \times 10^{+00} & 1.00 \times 10^{+00} & 2.00 \times 10^{+00} \\
63+
3.00 \times 10^{+00} & 4.00 \times 10^{+00} & 5.00 \times 10^{+00} \\
64+
\end{bmatrix}
65+
```
66+
67+
## To tabular
68+
69+
First import the `to_tabular` function:
70+
71+
```python
72+
>>> from arraytex import to_tabular
73+
```
74+
75+
Then run `to_tabular` with a `numpy.NDArray` as the first argument:
76+
77+
```python
78+
>>> print(to_tabular(A))
79+
\begin{tabular}{c c c}
80+
\toprule
81+
Col 1 & Col 2 & Col 3 \\
82+
\midrule
83+
0 & 1 & 2 \\
84+
3 & 4 & 5 \\
85+
\bottomrule
86+
\end{tabular}
87+
```
88+
89+
The `num_format` and `scientific_notation` arguments are available to use:
90+
91+
```python
92+
>>> print(to_tabular(A, num_format=".2f"))
93+
\begin{tabular}{c c c}
94+
\toprule
95+
Col 1 & Col 2 & Col 3 \\
96+
\midrule
97+
0.00 & 1.00 & 2.00 \\
98+
3.00 & 4.00 & 5.00 \\
99+
\bottomrule
100+
\end{tabular}
101+
```
102+
103+
You can pass custom column names and column align identifiers:
104+
105+
```python
106+
>>> print(to_tabular(A, col_align=["l", "c", "r"], col_names=["Data", "More Data", "Even More Data"]))
107+
\begin{tabular}{l c r}
108+
\toprule
109+
Data & More Data & Even More Data \\
110+
\midrule
111+
0 & 1 & 2 \\
112+
3 & 4 & 5 \\
113+
\bottomrule
114+
\end{tabular}
115+
```
116+
117+
Pass a list of column indices:
118+
119+
```python
120+
>>> print(to_tabular(A, col_index=["Sample 1", "Sample 2"]))
121+
\begin{tabular}{l c c c}
122+
\toprule
123+
Index & Col 1 & Col 2 & Col 3 \\
124+
\midrule
125+
Sample 1 & 0 & 1 & 2 \\
126+
Sample 2 & 3 & 4 & 5 \\
127+
\bottomrule
128+
\end{tabular}
129+
```
130+
131+
Specify the name of the name of index column through `col_names`:
132+
133+
```python
134+
>>> print(to_tabular(A, col_index=["Sample 1", "Sample 2"], col_names=["Which Sample", "A", "B", "C"]))
135+
\begin{tabular}{l c c c}
136+
\toprule
137+
Which Sample & A & B & C \\
138+
\midrule
139+
Sample 1 & 0 & 1 & 2 \\
140+
Sample 2 & 3 & 4 & 5 \\
141+
\bottomrule
142+
\end{tabular}
7143
```

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "arraytex"
3-
version = "0.0.7"
3+
version = "0.0.8"
44
description = "ArrayTeX"
55
authors = ["Dom Batten <[email protected]>"]
66
license = "MIT"

src/arraytex/api.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ def to_tabular(
8282
8383
Raises:
8484
TooManyDimensionsError: when the supplied array has more than 2 dimensions
85-
DimensionMismatchError: when there are mismatched column identifiers and
86-
dimensions
85+
DimensionMismatchError: when there is a mismatch between column items and number
86+
of columns, or column index items and number of rows
8787
"""
8888
n_dims = len(arr.shape)
8989

src/arraytex/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def num_formatter(x: Union[np.int64, np.float64, np.float32]) -> str:
6161
)
6262

6363
if num_format and "e" in num_format:
64-
pattern = r"e(-?\d+)"
64+
pattern = r"e([\+-]?\d+)"
6565
replace = r"\\mathrm{e}{\g<1>}"
6666
if scientific_notation:
6767
replace = r" \\times 10^{\g<1>}"

tests/test_api.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,20 @@ def test_e_notation(self) -> None:
8787
== r"""\begin{bmatrix}
8888
1.00\mathrm{e}{-03} & 2.00\mathrm{e}{-03} & 3.00\mathrm{e}{-03} \\
8989
4.00\mathrm{e}{-03} & 5.00\mathrm{e}{-03} & 6.00\mathrm{e}{-03} \\
90+
\end{bmatrix}"""
91+
)
92+
93+
def test_e_notation_positive(self) -> None:
94+
"""Scientific E notation can be selected with positive powers."""
95+
mat = np.arange(6).reshape(2, 3)
96+
97+
out = to_matrix(mat, num_format=".2e")
98+
99+
assert (
100+
out
101+
== r"""\begin{bmatrix}
102+
0.00\mathrm{e}{+00} & 1.00\mathrm{e}{+00} & 2.00\mathrm{e}{+00} \\
103+
3.00\mathrm{e}{+00} & 4.00\mathrm{e}{+00} & 5.00\mathrm{e}{+00} \\
90104
\end{bmatrix}"""
91105
)
92106

0 commit comments

Comments
 (0)