diff --git a/README.md b/README.md index 3086372..5403762 100644 --- a/README.md +++ b/README.md @@ -32,6 +32,15 @@ Convert a `numpy.NDArray` to various LaTeX forms. 1 & 2 & 3 \\ 4 & 5 & 6 \\ \end{bmatrix} +>>> print(atx.to_tabular(A)) +\begin{tabular}{c c c} +\toprule +Col 1 & Col 2 & Col 3 \\ +\midrule +1 & 2 & 3 \\ +4 & 5 & 6 \\ +\bottomrule +\end{tabular} ``` Inspired by [@josephcslater](https://github.com/josephcslater)'s @@ -39,7 +48,7 @@ Inspired by [@josephcslater](https://github.com/josephcslater)'s ## Features -- Support for different matrix environment delimiters, (`bmatrix`, `pmatrix`, etc.) +- Support for matrix environments with different delimiters (`bmatrix`, `pmatrix`, etc.). - Support for tabular environments. - Support for builtin number formats (`:.2f`, `:.3e`, etc.). - Fully tested and typed. diff --git a/docs/usage.md b/docs/usage.md index 5c0a1c2..7504a79 100644 --- a/docs/usage.md +++ b/docs/usage.md @@ -47,20 +47,20 @@ Different matrix style environment delimiters can be used: So can builtin number formatters: ```python ->>> print(to_matrix(A, num_format=".2e")) +>>> print(to_matrix((A + 1) * 1e3, num_format=".2e")) \begin{bmatrix} -0.00\mathrm{e}{+00} & 1.00\mathrm{e}{+00} & 2.00\mathrm{e}{+00} \\ -3.00\mathrm{e}{+00} & 4.00\mathrm{e}{+00} & 5.00\mathrm{e}{+00} \\ +1.00\mathrm{e}{+03} & 2.00\mathrm{e}{+03} & 3.00\mathrm{e}{+03} \\ +4.00\mathrm{e}{+03} & 5.00\mathrm{e}{+03} & 6.00\mathrm{e}{+03} \\ \end{bmatrix} ``` Prefer scientific notation to e-notation? No problem: ```python ->>> print(to_matrix(A, num_format=".2e", scientific_notation=True)) +>>> print(to_matrix((A + 1) * 1e3, num_format=".2e", scientific_notation=True)) \begin{bmatrix} -0.00 \times 10^{+00} & 1.00 \times 10^{+00} & 2.00 \times 10^{+00} \\ -3.00 \times 10^{+00} & 4.00 \times 10^{+00} & 5.00 \times 10^{+00} \\ +1.00 \times 10^{+03} & 2.00 \times 10^{+03} & 3.00 \times 10^{+03} \\ +4.00 \times 10^{+03} & 5.00 \times 10^{+03} & 6.00 \times 10^{+03} \\ \end{bmatrix} ``` @@ -114,10 +114,10 @@ Data & More Data & Even More Data \\ \end{tabular} ``` -Pass a list of column indices: +Pass a list of row identifiers to be used as a table index: ```python ->>> print(to_tabular(A, col_index=["Sample 1", "Sample 2"])) +>>> print(to_tabular(A, index=["Sample 1", "Sample 2"])) \begin{tabular}{l c c c} \toprule Index & Col 1 & Col 2 & Col 3 \\ @@ -128,10 +128,10 @@ Sample 2 & 3 & 4 & 5 \\ \end{tabular} ``` -Specify the name of the name of index column through `col_names`: +Specify the name of the index column through `col_names`: ```python ->>> print(to_tabular(A, col_index=["Sample 1", "Sample 2"], col_names=["Which Sample", "A", "B", "C"])) +>>> print(to_tabular(A, index=["Sample 1", "Sample 2"], col_names=["Which Sample", "A", "B", "C"])) \begin{tabular}{l c c c} \toprule Which Sample & A & B & C \\ diff --git a/pyproject.toml b/pyproject.toml index a55536d..2ed2c3a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "arraytex" -version = "0.0.8" +version = "0.0.9" description = "ArrayTeX" authors = ["Dom Batten "] license = "MIT" diff --git a/src/arraytex/api.py b/src/arraytex/api.py index 8490f98..9724c96 100644 --- a/src/arraytex/api.py +++ b/src/arraytex/api.py @@ -59,7 +59,7 @@ def to_tabular( scientific_notation: bool = False, col_align: Union[List[str], str] = "c", col_names: Optional[List[str]] = None, - col_index: Optional[List[str]] = None, + index: Optional[List[str]] = None, to_clp: bool = False, ) -> str: """Convert a numpy.NDArray to LaTeX tabular environment. @@ -74,7 +74,7 @@ def to_tabular( is provided then each item will be assigned to each column, list size and number of columns must match col_names: an optional list of column names, otherwise generic names will be assigned - col_index: an optional list of column indices, i.e. row identifiers + index: an optional table index, i.e. row identifiers to_clp: copy the output to the system clipboard Returns: @@ -96,7 +96,7 @@ def to_tabular( else: raise TooManyDimensionsError - if not col_index: + if not index: if isinstance(col_align, list) and len(col_align) != n_cols: raise DimensionMismatchError( f"Number of `col_align` items ({len(col_align)}) " @@ -110,7 +110,7 @@ def to_tabular( ) if ( - col_index + index and col_names and isinstance(col_align, list) and len(col_names) != len(col_align) @@ -128,10 +128,10 @@ def to_tabular( lines = _parse_lines(arr, num_format, scientific_notation) - if col_index: - if len(col_index) != len(lines): + if index: + if len(index) != len(lines): raise DimensionMismatchError( - f"Number of `col_index` items ({len(col_index)}) " + f"Number of `index` items ({len(index)}) " + f"doesn't match number of rows ({len(lines)})" ) @@ -142,7 +142,7 @@ def to_tabular( col_names.insert(0, "Index") for idx, line in enumerate(lines): - lines[idx] = f"{col_index[idx]} & " + line.strip() + lines[idx] = f"{index[idx]} & " + line.strip() rv = [f"\\begin{{tabular}}{{{' '.join(col_align)}}}"] rv += [r"\toprule"] diff --git a/tests/test_api.py b/tests/test_api.py index c06003e..e7a8824 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -292,15 +292,15 @@ def test_0_d(self) -> None: \end{tabular}""" ) - class TestColIndex: - """Tests for the `col_index` support.""" + class TestIndex: + """Tests for the `index` support.""" def test_default(self) -> None: - """`col_index` forms the row names.""" - col_index = ["Row 1", "Row 2"] + """`index` forms the row names.""" + index = ["Row 1", "Row 2"] mat = np.arange(1, 5).reshape(2, 2) - out = to_tabular(mat, col_index=col_index) + out = to_tabular(mat, index=index) assert ( out @@ -315,24 +315,24 @@ def test_default(self) -> None: ) def test_bad_dimensions(self) -> None: - """An error is raised if wrong dimension of `col_index`.""" - col_index = ["Row 1", "Row 2"] + """An error is raised if wrong dimension of `index`.""" + index = ["Row 1", "Row 2"] mat = np.arange(1, 4).reshape(3, 1) with pytest.raises(DimensionMismatchError) as exc: - to_tabular(mat, col_index=col_index) + to_tabular(mat, index=index) assert str(exc.value) == ( - "Number of `col_index` items (2) doesn't match number of rows (3)" + "Number of `index` items (2) doesn't match number of rows (3)" ) def test_given_col_names(self) -> None: """A given index name as part of `col_names` is used.""" - col_index = ["Row 1", "Row 2"] + index = ["Row 1", "Row 2"] col_names = ["My Index", "Col 1", "Col 2"] mat = np.arange(1, 5).reshape(2, 2) - out = to_tabular(mat, col_index=col_index, col_names=col_names) + out = to_tabular(mat, index=index, col_names=col_names) assert ( out @@ -348,10 +348,10 @@ def test_given_col_names(self) -> None: def test_given_col_align(self) -> None: """A given col align char can be used for the col index.""" - col_index = ["Row 1", "Row 2"] + index = ["Row 1", "Row 2"] mat = np.arange(1, 5).reshape(2, 2) - out = to_tabular(mat, col_index=col_index, col_align=["r", "c", "c"]) + out = to_tabular(mat, index=index, col_align=["r", "c", "c"]) assert ( out @@ -367,7 +367,7 @@ def test_given_col_align(self) -> None: def test_given_col_name_and_align(self) -> None: """A given col index name and align can be used for the index.""" - col_index = ["Row 1", "Row 2"] + index = ["Row 1", "Row 2"] col_names = ["My Index", "Col 1", "Col 2"] col_align = ["r", "c", "c"] mat = np.arange(1, 5).reshape(2, 2) @@ -376,7 +376,7 @@ def test_given_col_name_and_align(self) -> None: mat, col_align=col_align, col_names=col_names, - col_index=col_index, + index=index, ) assert ( @@ -393,7 +393,7 @@ def test_given_col_name_and_align(self) -> None: def test_col_align_bad_dimensions(self) -> None: """Bad dimensions of `col_align` is caught.""" - col_index = ["Row 1", "Row 2"] + index = ["Row 1", "Row 2"] col_names = ["My Index", "Col 1", "Col 2"] col_align = ["r", "c"] mat = np.arange(1, 5).reshape(2, 2) @@ -403,7 +403,7 @@ def test_col_align_bad_dimensions(self) -> None: mat, col_align=col_align, col_names=col_names, - col_index=col_index, + index=index, ) assert str(exc.value) == (