├── setup.cfg ├── .vscode └── settings.json ├── requirements.txt ├── pytest.ini ├── MANIFEST ├── .gitignore ├── .travis.yml ├── license.rst.txt ├── Makefile ├── setup.py ├── README.rst ├── array_to_latex └── __init__.py └── Examples.ipynb /setup.cfg: -------------------------------------------------------------------------------- 1 | [metadata] 2 | description-file = README.rst 3 | -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "esbonio.sphinx.confDir": "" 3 | } -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | numpy 2 | scipy 3 | pandas 4 | array_to_latex 5 | -------------------------------------------------------------------------------- /pytest.ini: -------------------------------------------------------------------------------- 1 | [pytest] 2 | addopts = --ignore=setup.py --doctest-modules 3 | doctest_optionflags= NORMALIZE_WHITESPACE ELLIPSIS 4 | -------------------------------------------------------------------------------- /MANIFEST: -------------------------------------------------------------------------------- 1 | # file GENERATED by distutils, do NOT edit 2 | setup.cfg 3 | setup.py 4 | array_to_latex/__init__.py 5 | array_to_latex/array_to_latex.py 6 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # 2 | array_to_latex/__pycache__ 3 | Untitled*.* 4 | 5 | __pycache__/* 6 | .cache/* 7 | *.gz 8 | 9 | array_to_latex.egg* 10 | .DS_Store 11 | .ipynb_checkpoints/Untitled-checkpoint.ipynb 12 | .ipynb_checkpoints/Examples-checkpoint.ipynb 13 | .ipynb_checkpoints/Untitled1-checkpoint.ipynb 14 | .ipynb_checkpoints/Examples-checkpoint.ipynb 15 | array_to_latex.code-workspace 16 | borg-env 17 | .ipynb_checkpoints/* 18 | .venv 19 | *.whl 20 | build/* 21 | dist/* -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | # numpy, sympy not whitelisted by travis; need sudo 2 | sudo: true 3 | 4 | language: python 5 | 6 | python: 7 | - "3.5" 8 | # - "3.3" 9 | # - "3.4" 10 | # - "3.2" 11 | 12 | 13 | #virtualenv: 14 | # system_site_packages: true 15 | 16 | before_install: 17 | - sudo apt-get install -y python-numpy python3-numpy 18 | 19 | install: 20 | - pip install clipboard 21 | - pip install . 22 | 23 | script: 24 | - nosetests --with-doctest --doctest-tests 25 | 26 | #--doctest-extension= 'array_to_latex' 27 | -------------------------------------------------------------------------------- /license.rst.txt: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | Copyright (c) 2016 Joseph C. Slater 3 | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 5 | 6 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 7 | 8 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 9 | SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | # This Makefile was created by transforming the one for oct2py 2 | # The line #------------------ shows where editing to date has been completed 3 | # for the first attempt. 4 | # Note: This is meant for vibration_toolbox developer use only 5 | 6 | .PHONY: all clean test cover release gh-pages docs 7 | 8 | # I don't know what the following line means/does 9 | #export TEST_ARGS=--exe -v --with-doctest 10 | export NAME=array_to_latex 11 | 12 | export GHP_MSG="Generated gh-pages for `git log master -1 --pretty=short --abbrev-commit`" 13 | export BDR_MSG="Generated binder branch" 14 | export VERSION=`python -c "import $(NAME); print($(NAME).__version__)"` 15 | 16 | #all: clean 17 | # python setup.py install 18 | 19 | #---------------------------------------------------- 20 | 21 | help: 22 | @echo "Please use \`make ' where is one of:" 23 | @echo " clean to clear build files" 24 | @echo " test to test all docstring examples" 25 | @echo " cover to test coverage (not working yet)" 26 | @echo " release to edit version, build docs and release" 27 | @echo " wheel build wheel file (for local use)" 28 | @echo " wheel-dist build wheel and push to github" 29 | @echo " docs build docs using sphin" 30 | @echo " html alias for docs" 31 | @echo " gh-pages build and release docs" 32 | @echo " binder make binder branch" 33 | 34 | clean: 35 | rm -rf build 36 | rm -rf dist 37 | find . -name "*.pyc" -o -name "*.py,cover"| xargs rm -f 38 | # killall -9 nosetests; true 39 | 40 | test: 41 | 42 | release: clean 43 | pip install --user readme_renderer 44 | #python setup.py check -r -s 45 | #python setup.py register 46 | rm -rf dist 47 | python setup.py bdist_wheel 48 | # python setup.py sdist 49 | git tag v$(VERSION) 50 | twine upload --repository array_to_latex dist/* --verbose 51 | # printf '\nUpgrade vibration toolbox with release and sha256 sum:' 52 | # printf '\nOK, no sha256 sum yet:' 53 | # shasum -a 256 dist/*.tar.gz 54 | 55 | wheel: 56 | rm -rf dist 57 | python setup.py bdist_wheel 58 | 59 | gh-pages: 60 | git checkout master 61 | git pull origin master 62 | git commit -a -m "Keep examples in sync"; true 63 | git push origin; true 64 | make docs 65 | ghp-import -n -p -m $(GHP_MSG) docs/_build/html 66 | 67 | binder: 68 | git checkout master 69 | git pull origin master 70 | git commit -a -m "Keep examples in sync"; true 71 | git push origin; true 72 | ghp-import -n -p -b binder -m $(BDR_MSG) docs/tutorial 73 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | """array_to_latex converts Numpy and Pandas arrays to formatted LaTeX.""" 2 | 3 | from setuptools import setup 4 | import os 5 | 6 | # Utility function to read the README file. 7 | # Used for the long_description. It's nice, because now 1) we have a top level 8 | # README file and 2) it's easier to type in the README file than to put a raw 9 | # string in below ... 10 | 11 | 12 | def read(fname): 13 | """Read the readme.rst file.""" 14 | return open(os.path.join(os.path.dirname(__file__), fname)).read() 15 | 16 | 17 | with open('array_to_latex/__init__.py', 'rb') as fid: 18 | for line in fid: 19 | line = line.decode('utf-8') 20 | if line.startswith('__version__'): 21 | version = line.strip().split()[-1][1:-1] 22 | break 23 | 24 | setup(name='array_to_latex', 25 | # Note: Version must also be set in __init__.py 26 | # Version must also be set in download_url. 27 | version=version, 28 | description='Return Numpy and Pandas arrays as formatted LaTeX arrays.', 29 | author='Joseph C. Slater', 30 | author_email='joseph.c.slater@gmail.com', 31 | url='https://github.com/josephcslater/array_to_latex/', 32 | # download_url='https://github.com/josephcslater 33 | # /array_to_latex/archive/0.42.tar.gz', 34 | packages=['array_to_latex'], 35 | long_description=read('README.rst'), 36 | keywords=['latex', 'array', 'format', 'numpy', 'scipy'], 37 | install_requires=['numpy', 'pandas', 'clipboard'], 38 | classifiers=['Development Status :: 5 - Production/Stable', 39 | 'Intended Audience :: Science/Research', 40 | 'License :: OSI Approved :: MIT License', 41 | 'Environment :: Console', 42 | 'Intended Audience :: End Users/Desktop', 43 | 'Intended Audience :: Education', 44 | 'Intended Audience :: Science/Research', 45 | 'Programming Language :: Python', 46 | 'Programming Language :: Python :: 3.4', 47 | 'Programming Language :: Python :: 3.5', 48 | 'Programming Language :: Python :: 3.6', 49 | 'Topic :: Scientific/Engineering', 50 | 'Topic :: Text Processing :: Markup :: LaTeX', 51 | 'Operating System :: Microsoft :: Windows', 52 | 'Operating System :: POSIX', 53 | 'Operating System :: Unix', 54 | 'Operating System :: MacOS', 55 | 'Topic :: Utilities'] 56 | ) 57 | 58 | 59 | # https://pypi.python.org/pypi?%3Aaction=list_classifiers 60 | -------------------------------------------------------------------------------- /README.rst: -------------------------------------------------------------------------------- 1 | Convert NumPy/SciPy arrays and Pandas Dataframes to formatted LaTeX arrays 2 | ========================================================================== 3 | 4 | .. image:: https://badge.fury.io/py/array-to-latex.png/ 5 | :target: http://badge.fury.io/py/array-to-latex 6 | 7 | .. image:: https://img.shields.io/badge/Say%20Thanks-!-1EAEDB.svg 8 | :target: https://saythanks.io/to/josephcslater 9 | 10 | .. image:: http://pepy.tech/badge/array-to-latex 11 | :target: http://pepy.tech/project/array-to-latex 12 | :alt: PyPi Download stats 13 | 14 | .. image:: https://mybinder.org/badge_logo.svg 15 | :target: https://mybinder.org/v2/gh/josephcslater/array_to_latex/master?filepath=Examples.ipynb 16 | 17 | The module ``array_to_latex`` converts a NumPy/SciPy array or Pandas Numerical DataFrame to a LaTeX 18 | array or table using `Python 3.x style`_ formatting of the result or a format usable for plotting within LaTeX using pgfplots. Note that as of *0.83* it **does** add 19 | the column formatting arguments (for example, `{ccc...}`) when the user chooses to use `array`. I'm happy 20 | to have someone create a better solution but this at least makes a copied array immediately usable. I 21 | prefer `bmatrix` or similar. ` 22 | 23 | Play with it on `mybinder.org`_! 24 | 25 | A NumPy-focused re-envisioned converter based, in part, on this is 26 | `numpyarray_to_latex `_. Also, available 27 | *pip install*. It incorporates more sophisticated "sub" markup capabilities. Check it out! 28 | 29 | Recent updates follow, with a more complete list towards the end of this document. If you don't see the current version in this list it's likely because I (again) forgot to update it when pushing out a new version. Please see the `readme`_ on GitHub. 30 | 31 | | *0.81*: Bug fixes in requirements and to_clp 32 | | *0.82*: Raise ImportError exception when incorrect datatype used. 33 | | *0.83*: Now puts a default format in when returning as an `array` object. 34 | | *0.90*: Add ``coords`` format for use in plotting within LaTeX using pgfplots. 35 | | *0.91*: Improve the output for scientific `e` notation. 36 | | *0.92*: Allow np.array consisting of strings to be converted (Thanks to Tesla2000) 37 | 38 | Install using ``pip install --user array_to_latex`` from your command prompt, **not the Python prompt**. 39 | 40 | Please read the help. It explains all options. To try it, see `the online mybinder.org demo `_. It documents illustrates application to numerical Pandas DataFrames. 41 | 42 | .. code:: python 43 | 44 | import numpy as np 45 | import array_to_latex as a2l 46 | A = np.array([[1.23456, 23.45678],[456.23, 8.239521]]) 47 | a2l.to_ltx(A, frmt = '{:6.2f}', arraytype = 'array') 48 | 49 | will print the LaTeX code to your output. 50 | 51 | .. code:: python 52 | 53 | import numpy as np 54 | import array_to_latex as a2l 55 | A = np.array([[1.23456, 23.45678],[456.23, 8.239521]]) 56 | latex_code = a2l.to_ltx(A, frmt = '{:6.2f}', arraytype = 'bmatrix', print_out=False) 57 | 58 | will put the LaTeX code into variable ``latex_code``. 59 | 60 | .. code:: python 61 | 62 | import numpy as np 63 | import array_to_latex as a2l 64 | A = np.array([[1.23456, 23.45678],[456.23, 8.239521]]) 65 | a2l.to_clp(A, frmt = '{:6.2f}', arraytype = 'bmatrix') 66 | 67 | will put the array onto your clipboard. 68 | 69 | If you will be using the same conversion over and over, you can define your own by using a ``lambda`` function: 70 | 71 | .. code:: python 72 | 73 | to_tex = lambda A : a2l.to_ltx(A, frmt = '{:6.2f}', arraytype = 'bmatrix', mathform=True) 74 | to_tex(A) 75 | 76 | so you can now use your function ``to_tex`` repeatedly with your specified settings. More detailed information on usage is in the help. 77 | 78 | .. code:: python 79 | 80 | import array_to_latex as a2l 81 | help(a2l.to_ltx) 82 | 83 | Interesting alternative approaches are `np_array_to_latex `_ and `tab2latex (convert numpy array to longtable file) `_. 84 | 85 | Like this module, `buy me a coffee! `_. 86 | 87 | | *New in* 0.37: Now handles complex arrays. 88 | | *New in* 0.38: Aligns columns neatly. 89 | | **0.40: Critical upgrade- 0.37-0.38 formatted incorrectly.** 90 | | **0.41: Critical upgrade- 0.37-0.40 formatted incorrectly.** 91 | | *New in* 0.43: Now handles 1-D Arrays. See new option ``row`` 92 | | *New in* 0.50: Now works with Pandas DataFrames 93 | | *0.51*: Bug fix- remove extra blank lines in DataFrame tabular output 94 | | *0.52*: A few documentation typos fixed. No code changed. 95 | | *0.60*: Now handles strings in Pandas Dataframes. Fixes bug in exponentials and handling of exponentials. Please report errors! 96 | | *0.61*: Minor documentation improvements. No code changed. 97 | | *0.70*: Added ``mathform``. When set to ``True`` (default), returns 10 to superscript form. 98 | | *0.71*: Line breaks broke ``readme.rst`` on ``pypi``. No code change. 99 | | *0.72*: Line breaks broke ``readme.rst`` on ``pypi``. No code change. 100 | | *0.73*: pypi won't handle mathjax. It makes me sad. No code change. 101 | | *0.74*: Not released 102 | | *0.75*: output improvements (short-lived release) 103 | | *0.76*: Printing made better, allows outputs, added ``print_out`` 104 | | boolean to turn off printing 105 | | *0.80*: Return to previous interface while still enabling returned 106 | | LaTeX string. 107 | | *0.81*: Bug fixes in requirements and to_clp 108 | | *0.82*: Raise ImportError exception when incorrect datatype used. 109 | | *0.83*: Add formatting defaults when using array versus bmatrix, etc. 110 | | *0.90*: Add ``coords`` format for use in plotting within LaTeX using pgfplots. 111 | | *0.91*: Improve the output for scientific `e` notation. 112 | | *0.92*: Allow np.array consisting of strings to be converted (Thanks to Tesla2000) 113 | 114 | 115 | .. _`Python 3.x style`: https://docs.python.org/3.7/library/string.html 116 | .. _`mybinder.org`: https://mybinder.org/v2/gh/josephcslater/array_to_latex/master?filepath=Examples.ipynb 117 | .. _`readme`: https://github.com/josephcslater/array_to_latex/blob/master/README.rst 118 | -------------------------------------------------------------------------------- /array_to_latex/__init__.py: -------------------------------------------------------------------------------- 1 | """ 2 | Return numpy arrays and Pandas dataframes as LaTeX. 3 | 4 | Provides `to_ltx` and `to_clp` which convert numpy arrays and Pandas dataframes 5 | arrays to LaTeX form. 6 | """ 7 | 8 | # Note- version must also be set in setup.py 9 | __version__ = '0.92' 10 | __all__ = ['to_clp', 'to_ltx', '__version__'] 11 | 12 | __author__ = u'Joseph C. Slater' 13 | __license__ = 'MIT' 14 | __copyright__ = 'Copyright 2018 Joseph C. Slater' 15 | 16 | import numpy as _np 17 | import pandas as _pd 18 | 19 | 20 | def to_clp(a, frmt='{:1.2f}', arraytype='bmatrix', imstring='j'): 21 | r""" 22 | Return a LaTeX array to the clipboard given a numpy array. 23 | 24 | Parameters 25 | ---------- 26 | a : float array 27 | frmt : string 28 | python 3 formatter, optional- 29 | https://mkaz.tech/python-string-format.html 30 | arraytype : string 31 | latex array type- `bmatrix` default, optional 32 | imstring : string (optional) 33 | Character for square root of -1. Usually i or j 34 | 35 | Returns 36 | ------- 37 | out: str 38 | LaTeX array 39 | 40 | See Also 41 | -------- 42 | array_to_latex 43 | 44 | Examples 45 | -------- 46 | >>> import numpy as np 47 | >>> import array_to_latex as a2l 48 | >>> A = np.array([[1.23456, 23.45678],[456.23, 8.239521]]) 49 | >>> a2l.to_clp(A, frmt = '{:6.2f}', arraytype = 'array') 50 | 51 | Note that the output is in your clipboard, so you won't see any results. 52 | See `to_ltx` for further examples. 53 | 54 | """ 55 | b = to_ltx(a, frmt=frmt, arraytype=arraytype, nargout=1, imstring=imstring, print_out=False) 56 | try: 57 | import clipboard as _clipboard 58 | _clipboard.copy(b) 59 | except ImportError: 60 | print('\nPackage ''clipboard'' is not installed') 61 | print('pip install clipboard\nor install via other ', 62 | 'means to use this function') 63 | 64 | 65 | def _numpyarraytolatex(a, frmt='{:6.2f}', arraytype='bmatrix', nargout=0, 66 | imstring='j', row=True, mathform=True): 67 | r"""Return a LaTeX array given a numpy array. 68 | 69 | Parameters 70 | ---------- 71 | a : float array 72 | frmt : string 73 | python 3 formatter, optional- 74 | https://mkaz.tech/python-string-format.html 75 | arraytype : string 76 | latex array type- `bmatrix` default, optional 77 | imstring : string (optional) 78 | Character for square root of -1. Usually i or j 79 | row : Boolean 80 | If the array is 1-D, should the output be 81 | a row (True) or column (False) 82 | 83 | Returns 84 | ------- 85 | out: str 86 | LaTeX array 87 | 88 | See Also 89 | -------- 90 | to_clp 91 | 92 | Examples 93 | -------- 94 | >>> import numpy as np 95 | >>> import array_to_latex as a2l 96 | >>> A = np.array([[1.23456, 23.45678],[456.23, 8.239521]]) 97 | >>> a2l.to_ltx(A, frmt = '{:6.2f}', arraytype = 'array') 98 | \begin{array}{c, c} 99 | 1.23 & 23.46\\ 100 | 456.23 & 8.24 101 | \end{array} 102 | None 103 | >>> a2l.to_ltx(A, frmt = '{:6.2e}', arraytype = 'array') 104 | \begin{array}{c, c} 105 | 1.23e+00 & 2.35e+01\\ 106 | 4.56e+02 & 8.24e+00 107 | \end{array} 108 | None 109 | >>> a2l.to_ltx(A, frmt = '{:.3g}', arraytype = 'array') 110 | \begin{array}{c, c} 111 | 1.23 & 23.5\\ 112 | 456 & 8.24 113 | \end{array} 114 | None 115 | >>> a2l.to_ltx(A, frmt = '{:1.2f}', arraytype = 'coords') 116 | {(1.23,23.46),(456.23,8.24)} 117 | None 118 | 119 | """ 120 | if len(a.shape) > 2: 121 | raise ValueError('bmatrix can at most display two dimensions') 122 | 123 | if len(a.shape) == 1: 124 | a = _np.array([a]) 125 | if row is False: 126 | a = a.T 127 | 128 | if arraytype == "coords": 129 | coords = ['(' + ','.join([frmt.format(x) for x in r]) + ')' for r in a] 130 | return '{' + ','.join(coords) + '}' 131 | 132 | arrayformat = '' 133 | 134 | if arraytype == 'array': 135 | arrayformat = '{' 136 | for _ in _np.arange(a.shape[1]): 137 | arrayformat = arrayformat + ' c,' 138 | arrayformat = arrayformat[:-1] + '}' 139 | 140 | out = r'\begin{' + arraytype + '}' + arrayformat + '\n' 141 | for i in _np.arange(a.shape[0]): 142 | out = out + ' ' 143 | for j in _np.arange(a.shape[1]): 144 | if isinstance(a[i, j], str): 145 | leadstr = ' ' 146 | dot_space = (((max(len(pet) for pet in a[:, j]) - len(a[i, j]))) * ' ') 147 | out = (out + leadstr + a[i, j] + dot_space + ' & ') 148 | else: 149 | leadstr = '' if _np.real(a[i, j]) < 0 else ' ' 150 | dot_space = ' ' if '.' not in frmt.format(a[i, j]) else '' 151 | if _np.iscomplexobj(a[i, j]): 152 | out = (out + leadstr 153 | + math_form(frmt.format(_np.real(a[i, j])), 154 | mathform=mathform) 155 | + ' + ' 156 | + math_form(frmt.format(_np.imag(a[i, j])), 157 | is_imaginary=True, 158 | mathform=mathform) 159 | + imstring 160 | + dot_space + ' & ') 161 | else: 162 | out = (out 163 | + leadstr 164 | + math_form(frmt.format(_np.real(a[i, j])), 165 | mathform=mathform) 166 | + dot_space 167 | + r' & ') 168 | 169 | out = out[:-3] 170 | out = out + '\\\\\n' 171 | 172 | out = out[:-3] + '\n' + r'\end{' + arraytype + '}' 173 | 174 | return out 175 | 176 | 177 | def _dataframetolatex(df, 178 | frmt='{:6.2f}', 179 | arraytype='tabular', 180 | nargout=0, 181 | imstring='j', 182 | row=True, 183 | mathform=True): 184 | r""" 185 | Return a LaTeX array given a Pandas DataFrame array. 186 | 187 | Parameters 188 | ---------- 189 | a : float array 190 | frmt : string 191 | python 3 formatter, optional- 192 | https://mkaz.tech/python-string-format.html 193 | arraytype : string 194 | latex array type- `bmatrix` default, optional 195 | imstring : string (optional) 196 | Character for square root of -1. Usually i or j 197 | row : Boolean (optional: default True) 198 | If the array is 1-D, should the output be 199 | a row (True) or column (False) 200 | mathform : Boolean (optional: default True) 201 | Replace #E# with #\times10^{#} 202 | 203 | 204 | Returns 205 | ------- 206 | out: str 207 | LaTeX array 208 | 209 | See Also 210 | -------- 211 | to_clp 212 | 213 | Examples 214 | -------- 215 | >>> import numpy as np 216 | >>> import array_to_latex as a2l 217 | >>> A = np.array([[1.23456, 23.45678],[456.23, 8.239521]]) 218 | >>> a2l.to_ltx(A, frmt = '{:6.2f}', arraytype = 'array') 219 | \begin{array} 220 | 1.23 & 23.46\\ 221 | 456.23 & 8.24 222 | \end{array} 223 | None 224 | >>> a2l.to_ltx(A, frmt = '{:6.2e}', arraytype = 'array') 225 | \begin{array} 226 | 1.23e+00 & 2.35e+01\\ 227 | 4.56e+02 & 8.24e+00 228 | \end{array} 229 | None 230 | >>> a2l.to_ltx(A, frmt = '{:.3g}', arraytype = 'array') 231 | \begin{array} 232 | 1.23 & 23.5\\ 233 | 456 & 8.24 234 | \end{array} 235 | None 236 | 237 | """ 238 | columns = df.columns 239 | rows = df.transpose().columns 240 | a = _np.array(df) 241 | out = r'\begin{' + arraytype + '}' 242 | 243 | if arraytype == 'tabular': 244 | out += r'{l' 245 | for _ in columns: 246 | out += 'r' 247 | out += r'}' 248 | 249 | out += '\n' 250 | 251 | if arraytype == 'tabular': 252 | out += '\\toprule\n' 253 | 254 | out += ' ' 255 | for column in columns: 256 | out += '& ' + column + ' ' 257 | out += r'\\\n' 258 | 259 | out += '\\midrule\n' 260 | 261 | for i in _np.arange(a.shape[0]): 262 | out = out + ' ' + str(rows[i]) + ' & ' 263 | for j in _np.arange(a.shape[1]): 264 | if isinstance(a[i, j], str): 265 | leadstr = ' ' 266 | dot_space = (((max(len(pet) for pet in a[:, j]) - len(a[i, j]))) * ' ') 267 | out = (out + leadstr + a[i, j] + dot_space + ' & ') 268 | else: 269 | leadstr = '' if _np.real(a[i, j]) < 0 else ' ' 270 | dot_space = ' ' if '.' not in frmt.format(a[i, j]) else '' 271 | if _np.iscomplexobj(a[i, j]): 272 | out = (out + leadstr 273 | + math_form(frmt.format(_np.real(a[i, j])), 274 | mathform=mathform) 275 | + ' + ' 276 | + math_form(frmt.format(_np.imag(a[i, j])), 277 | is_imaginary=True, 278 | mathform=mathform) 279 | + imstring 280 | + dot_space + ' & ') 281 | else: 282 | out = (out + leadstr 283 | + math_form(frmt.format(a[i, j]), 284 | mathform=mathform) 285 | + dot_space + ' & ') 286 | 287 | out = out[:-3] 288 | out += '\\\\\n' 289 | 290 | if arraytype == 'tabular': 291 | out += '\\bottomrule\n' 292 | out += r'\end{' + arraytype + '}' 293 | else: 294 | out = out[:-3] + '\n' + r'\end{' + arraytype + '}' 295 | 296 | return out 297 | 298 | def to_ltx(a, frmt='{:1.2f}', arraytype=None, nargout=0, 299 | imstring='j', row=True, mathform=True, print_out=True): 300 | r""" 301 | Print or return a LaTeX array given a numpy array or Pandas dataframe. 302 | 303 | Parameters 304 | ---------- 305 | a : float array 306 | frmt : string 307 | python 3 formatter, optional- 308 | https://mkaz.tech/python-string-format.html 309 | arraytype : string 310 | latex array type- `bmatrix` default, optional 311 | imstring : string (optional) 312 | Character for square root of -1. Usually i or j 313 | row : Boolean (optional: default True) 314 | If the array is 1-D, should the output be 315 | a row (True) or column (False) 316 | mathform : Boolean (optional: default True) 317 | Replace #E# with #\times10^{#} 318 | 319 | 320 | Returns 321 | ------- 322 | out: str 323 | LaTeX array 324 | 325 | See Also 326 | -------- 327 | to_clp 328 | 329 | Examples 330 | -------- 331 | >>> import numpy as np 332 | >>> import array_to_latex as a2l 333 | >>> A = np.array([[1.23456, 23.45678],[456.23, 8.239521]]) 334 | >>> a2l.to_ltx(A, frmt = '{:6.2f}', arraytype = 'array') 335 | \begin{array} 336 | 1.23 & 23.46\\ 337 | 456.23 & 8.24 338 | \end{array} 339 | None 340 | >>> a2l.to_ltx(A, frmt = '{:6.2e}', arraytype = 'array') 341 | \begin{array} 342 | 1.23e+00 & 2.35e+01\\ 343 | 4.56e+02 & 8.24e+00 344 | \end{array} 345 | None 346 | >>> a2l.to_ltx(A, frmt = '{:.3g}', arraytype = 'array') 347 | \begin{array} 348 | 1.23 & 23.5\\ 349 | 456 & 8.24 350 | \end{array} 351 | None 352 | >>> a2l.to_ltx(A, frmt = '{:1.2f}', arraytype = 'coords') 353 | {(1.23,23.46),(456.23,8.24)} 354 | None 355 | 356 | """ 357 | if isinstance(a, _np.ndarray): 358 | 359 | if arraytype is None: 360 | arraytype = 'bmatrix' 361 | latex = _numpyarraytolatex(a, frmt=frmt, arraytype=arraytype, 362 | nargout=nargout, imstring=imstring, 363 | row=row, mathform=mathform) 364 | 365 | elif isinstance(a, _pd.core.frame.DataFrame): 366 | 367 | if arraytype is None: 368 | arraytype = 'tabular' 369 | latex = _dataframetolatex(a, frmt=frmt, arraytype=arraytype, 370 | nargout=nargout, imstring=imstring) 371 | else: 372 | raise TypeError("Argument should be a " 373 | "numpy array or a pandas DataFrame.") 374 | if print_out is True: 375 | print(latex) 376 | return 377 | 378 | return latex 379 | 380 | 381 | def math_form(number, is_imaginary=False, mathform=True): 382 | if 'e' in number: 383 | if mathform: 384 | number = number.replace('e', '\\times 10^{') + '}' 385 | else: 386 | number = number.replace('e', '\\mathrm{e}{') + '}' 387 | return number 388 | -------------------------------------------------------------------------------- /Examples.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Some brief examples on using array_to_latex to get nicely formatted latex versions of your arrays. " 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 1, 13 | "metadata": {}, 14 | "outputs": [], 15 | "source": [ 16 | "import pandas as pd\n", 17 | "import numpy as np\n", 18 | "import array_to_latex as a2l\n", 19 | "%load_ext autoreload\n", 20 | "%autoreload 2" 21 | ] 22 | }, 23 | { 24 | "cell_type": "markdown", 25 | "metadata": {}, 26 | "source": [ 27 | "Let's create an array and output it as $\\LaTeX$. We are going to use [Python 3.0 string formatting](https://docs.python.org/3.7/library/string.html) \n", 28 | "\n", 29 | "The following shows a float style output with 2 decimal places. " 30 | ] 31 | }, 32 | { 33 | "cell_type": "code", 34 | "execution_count": 2, 35 | "metadata": { 36 | "scrolled": true 37 | }, 38 | "outputs": [ 39 | { 40 | "name": "stdout", 41 | "output_type": "stream", 42 | "text": [ 43 | "\\begin{bmatrix}\n", 44 | " 1.23 + 0.00j & 23.46 + 0.00j\\\\\n", 45 | " 456.23 + 1.00j & 8.24 + 0.00j\n", 46 | "\\end{bmatrix}\n" 47 | ] 48 | } 49 | ], 50 | "source": [ 51 | "A = np.array([[1.23456, 23.45678],[456.23+1j, 8.239521]])\n", 52 | "a2l.to_ltx(A, frmt = '{:.2f}', arraytype = 'bmatrix', mathform = True)\n" 53 | ] 54 | }, 55 | { 56 | "cell_type": "markdown", 57 | "metadata": {}, 58 | "source": [ 59 | "Design is to print results to the screen with no output being available. However, new usages have highlighted the need to enable outputs and hide printing. Thus the addition of the ``print_out`` boolean to turn off printing but instead return an output." 60 | ] 61 | }, 62 | { 63 | "cell_type": "code", 64 | "execution_count": 3, 65 | "metadata": {}, 66 | "outputs": [], 67 | "source": [ 68 | "A = np.array([[1.23456, 23.45678],[456.23+1j, 8.239521]])\n", 69 | "latex_code = a2l.to_ltx(A, frmt = '{:.2f}', arraytype = 'bmatrix', mathform = True, print_out=False)" 70 | ] 71 | }, 72 | { 73 | "cell_type": "markdown", 74 | "metadata": {}, 75 | "source": [ 76 | "We can still print the returned formatted latex code:" 77 | ] 78 | }, 79 | { 80 | "cell_type": "code", 81 | "execution_count": 4, 82 | "metadata": {}, 83 | "outputs": [ 84 | { 85 | "name": "stdout", 86 | "output_type": "stream", 87 | "text": [ 88 | "\\begin{bmatrix}\n", 89 | " 1.23 + 0.00j & 23.46 + 0.00j\\\\\n", 90 | " 456.23 + 1.00j & 8.24 + 0.00j\n", 91 | "\\end{bmatrix}\n" 92 | ] 93 | } 94 | ], 95 | "source": [ 96 | "print(latex_code)" 97 | ] 98 | }, 99 | { 100 | "cell_type": "markdown", 101 | "metadata": {}, 102 | "source": [ 103 | "One can use a number before the decimal place. This defines the minimum width to use for the number, padding with spaces at the beginning. \n", 104 | "\n", 105 | "Since the largest number needs 6 characters (3 before the decimal, the decimal, and 2 after), putting a 6 in this location makes everything line up nicely. This would also be a nice default to code up. " 106 | ] 107 | }, 108 | { 109 | "cell_type": "code", 110 | "execution_count": 5, 111 | "metadata": {}, 112 | "outputs": [ 113 | { 114 | "name": "stdout", 115 | "output_type": "stream", 116 | "text": [ 117 | "\\begin{bmatrix}\n", 118 | " 1.23 + 0.00j & 23.46 + 0.00j\\\\\n", 119 | " 456.23 + 1.00j & 8.24 + 0.00j\n", 120 | "\\end{bmatrix}\n" 121 | ] 122 | } 123 | ], 124 | "source": [ 125 | "A = np.array([[1.23456, 23.45678],[456.23+1j, 8.239521]])\n", 126 | "a2l.to_ltx(A, frmt = '{:6.2f}', arraytype = 'bmatrix', mathform = True)" 127 | ] 128 | }, 129 | { 130 | "cell_type": "markdown", 131 | "metadata": {}, 132 | "source": [ 133 | "Let's put it in exponential form. " 134 | ] 135 | }, 136 | { 137 | "cell_type": "code", 138 | "execution_count": 6, 139 | "metadata": { 140 | "scrolled": true 141 | }, 142 | "outputs": [ 143 | { 144 | "name": "stdout", 145 | "output_type": "stream", 146 | "text": [ 147 | "\\begin{bmatrix}\n", 148 | " 1.23e+00 + 0.00e+00j & 2.35e+01 + 0.00e+00j\\\\\n", 149 | " 4.56e+02 + 1.00e+00j & 8.24e+00 + 0.00e+00j\n", 150 | "\\end{bmatrix}\n" 151 | ] 152 | } 153 | ], 154 | "source": [ 155 | "a2l.to_ltx(A, frmt = '{:.2e}', arraytype = 'bmatrix', mathform=False)" 156 | ] 157 | }, 158 | { 159 | "cell_type": "markdown", 160 | "metadata": {}, 161 | "source": [ 162 | "That's not how humans/textbooks write exponential form. Let's use `mathform=True` (which is the default). " 163 | ] 164 | }, 165 | { 166 | "cell_type": "code", 167 | "execution_count": 7, 168 | "metadata": {}, 169 | "outputs": [ 170 | { 171 | "name": "stdout", 172 | "output_type": "stream", 173 | "text": [ 174 | "\\begin{bmatrix}\n", 175 | " 1.23\\times 10^{+00} + 0.00\\times 10^{+00}j & 2.35\\times 10^{+01} + 0.00\\times 10^{+00}j\\\\\n", 176 | " 4.56\\times 10^{+02} + 1.00\\times 10^{+00}j & 8.24\\times 10^{+00} + 0.00\\times 10^{+00}j\n", 177 | "\\end{bmatrix}\n" 178 | ] 179 | } 180 | ], 181 | "source": [ 182 | "a2l.to_ltx(A, frmt = '{:6.2e}', arraytype = 'bmatrix', mathform=True)" 183 | ] 184 | }, 185 | { 186 | "cell_type": "markdown", 187 | "metadata": {}, 188 | "source": [ 189 | "It's easier to make these columns line up than when using `f` format styling- so I believe it is working. " 190 | ] 191 | }, 192 | { 193 | "cell_type": "markdown", 194 | "metadata": {}, 195 | "source": [ 196 | "Of course, the typeset $\\LaTeX$ will look better than the raw $\\LaTeX$.\n", 197 | "\n", 198 | "One can also capture the string in the output. " 199 | ] 200 | }, 201 | { 202 | "cell_type": "markdown", 203 | "metadata": {}, 204 | "source": [ 205 | "It will also do column and row-vectors. It's the array is 1-D, the default is a row. " 206 | ] 207 | }, 208 | { 209 | "cell_type": "code", 210 | "execution_count": 8, 211 | "metadata": { 212 | "scrolled": false 213 | }, 214 | "outputs": [ 215 | { 216 | "name": "stdout", 217 | "output_type": "stream", 218 | "text": [ 219 | "\\begin{bmatrix}\n", 220 | " 1.23 & 23.46 & 456.23 & 8.24\n", 221 | "\\end{bmatrix}\n" 222 | ] 223 | } 224 | ], 225 | "source": [ 226 | "A = np.array([1.23456, 23.45678, 456.23, 8.239521])\n", 227 | "a2l.to_ltx(A, frmt = '{:6.2f}', arraytype = 'bmatrix')" 228 | ] 229 | }, 230 | { 231 | "cell_type": "code", 232 | "execution_count": 9, 233 | "metadata": { 234 | "scrolled": false 235 | }, 236 | "outputs": [ 237 | { 238 | "name": "stdout", 239 | "output_type": "stream", 240 | "text": [ 241 | "\\begin{bmatrix}\n", 242 | " 1.23 & 23.46 & 456.23 & 8.24\n", 243 | "\\end{bmatrix}\n" 244 | ] 245 | } 246 | ], 247 | "source": [ 248 | "A = np.array([[1.23456, 23.45678, 456.23, 8.239521]])\n", 249 | "a2l.to_ltx(A, frmt='{:6.2f}', arraytype='bmatrix')\n" 250 | ] 251 | }, 252 | { 253 | "cell_type": "code", 254 | "execution_count": 10, 255 | "metadata": { 256 | "scrolled": false 257 | }, 258 | "outputs": [ 259 | { 260 | "name": "stdout", 261 | "output_type": "stream", 262 | "text": [ 263 | "\\begin{bmatrix}\n", 264 | " 1.23\\\\\n", 265 | " 23.46\\\\\n", 266 | " 456.23\\\\\n", 267 | " 8.24\n", 268 | "\\end{bmatrix}\n" 269 | ] 270 | } 271 | ], 272 | "source": [ 273 | "A = np.array([[1.23456, 23.45678, 456.23, 8.239521]]).T\n", 274 | "a2l.to_ltx(A, frmt='{:6.2f}', arraytype='bmatrix')\n" 275 | ] 276 | }, 277 | { 278 | "cell_type": "markdown", 279 | "metadata": {}, 280 | "source": [ 281 | "If you want to plot a few nods or points in latex using tizk, like\n", 282 | "```latex\n", 283 | "\\documentclass{standalone}\n", 284 | "\\usepackage{pgfplots}\n", 285 | "\\begin{document}\n", 286 | "\\begin{tikzpicture}\n", 287 | " \\foreach \\Point in \n", 288 | " {(0.23,2.46),(1.23,2.24)}\n", 289 | " {\\node at \\Point {$\\circ$};}\n", 290 | "\\end{tikzpicture}\n", 291 | "\\end{document}\n", 292 | "```\n", 293 | "you may want to output a list of coordinates." 294 | ] 295 | }, 296 | { 297 | "cell_type": "code", 298 | "execution_count": 11, 299 | "metadata": {}, 300 | "outputs": [ 301 | { 302 | "name": "stdout", 303 | "output_type": "stream", 304 | "text": [ 305 | "{(0.23,2.46),(1.23,2.24)}\n" 306 | ] 307 | } 308 | ], 309 | "source": [ 310 | "A = np.array([[0.23456, 2.45678],[1.23, 2.239521]])\n", 311 | "a2l.to_ltx(A, frmt='{:1.2f}', arraytype='coords')" 312 | ] 313 | }, 314 | { 315 | "cell_type": "markdown", 316 | "metadata": {}, 317 | "source": [ 318 | "We can use the `lambda` function method to create a function with personalized defaults. This makes for a much more compact call, and one that can be adjusted for an entire session. " 319 | ] 320 | }, 321 | { 322 | "cell_type": "code", 323 | "execution_count": 12, 324 | "metadata": {}, 325 | "outputs": [ 326 | { 327 | "name": "stdout", 328 | "output_type": "stream", 329 | "text": [ 330 | "\\begin{bmatrix}\n", 331 | " 2.35\\times 10^{-01} & 2.46\\times 10^{+00}\\\\\n", 332 | " 1.23\\times 10^{+00} & 2.24\\times 10^{+00}\n", 333 | "\\end{bmatrix}\n" 334 | ] 335 | } 336 | ], 337 | "source": [ 338 | "def to_tex(A): return a2l.to_ltx(\n", 339 | " A, frmt='{:6.2e}', arraytype='bmatrix', mathform=True)\n", 340 | "to_tex(A)\n" 341 | ] 342 | }, 343 | { 344 | "cell_type": "code", 345 | "execution_count": 13, 346 | "metadata": {}, 347 | "outputs": [ 348 | { 349 | "name": "stdout", 350 | "output_type": "stream", 351 | "text": [ 352 | "\\begin{bmatrix}\n", 353 | " 0.23 & 2.46\\\\\n", 354 | " 1.23 & 2.24\n", 355 | "\\end{bmatrix}\n" 356 | ] 357 | } 358 | ], 359 | "source": [ 360 | "def to_tex(A): return a2l.to_ltx(\n", 361 | " A, frmt='{:6.2f}', arraytype='bmatrix', mathform=True)\n", 362 | "to_tex(A)\n" 363 | ] 364 | }, 365 | { 366 | "cell_type": "markdown", 367 | "metadata": {}, 368 | "source": [ 369 | "# Panda DataFrames\n", 370 | "\n", 371 | "You can also produce tables or math arrays from Panda DataFrames.\n", 372 | "\n" 373 | ] 374 | }, 375 | { 376 | "cell_type": "code", 377 | "execution_count": 14, 378 | "metadata": {}, 379 | "outputs": [], 380 | "source": [ 381 | "df = pd.DataFrame(np.random.randint(low=0, high=10, size=(5, 5)),\n", 382 | "... columns=['a', 'b', 'c', 'd', 'e'])" 383 | ] 384 | }, 385 | { 386 | "cell_type": "code", 387 | "execution_count": 15, 388 | "metadata": {}, 389 | "outputs": [ 390 | { 391 | "data": { 392 | "text/html": [ 393 | "
\n", 394 | "\n", 407 | "\n", 408 | " \n", 409 | " \n", 410 | " \n", 411 | " \n", 412 | " \n", 413 | " \n", 414 | " \n", 415 | " \n", 416 | " \n", 417 | " \n", 418 | " \n", 419 | " \n", 420 | " \n", 421 | " \n", 422 | " \n", 423 | " \n", 424 | " \n", 425 | " \n", 426 | " \n", 427 | " \n", 428 | " \n", 429 | " \n", 430 | " \n", 431 | " \n", 432 | " \n", 433 | " \n", 434 | " \n", 435 | " \n", 436 | " \n", 437 | " \n", 438 | " \n", 439 | " \n", 440 | " \n", 441 | " \n", 442 | " \n", 443 | " \n", 444 | " \n", 445 | " \n", 446 | " \n", 447 | " \n", 448 | " \n", 449 | " \n", 450 | " \n", 451 | " \n", 452 | " \n", 453 | " \n", 454 | " \n", 455 | " \n", 456 | " \n", 457 | " \n", 458 | " \n", 459 | " \n", 460 | "
abcde
051832
184896
229689
375621
472433
\n", 461 | "
" 462 | ], 463 | "text/plain": [ 464 | " a b c d e\n", 465 | "0 5 1 8 3 2\n", 466 | "1 8 4 8 9 6\n", 467 | "2 2 9 6 8 9\n", 468 | "3 7 5 6 2 1\n", 469 | "4 7 2 4 3 3" 470 | ] 471 | }, 472 | "execution_count": 15, 473 | "metadata": {}, 474 | "output_type": "execute_result" 475 | } 476 | ], 477 | "source": [ 478 | "df" 479 | ] 480 | }, 481 | { 482 | "cell_type": "code", 483 | "execution_count": 16, 484 | "metadata": {}, 485 | "outputs": [ 486 | { 487 | "data": { 488 | "text/plain": [ 489 | "array([[5, 1, 8, 3, 2],\n", 490 | " [8, 4, 8, 9, 6],\n", 491 | " [2, 9, 6, 8, 9],\n", 492 | " [7, 5, 6, 2, 1],\n", 493 | " [7, 2, 4, 3, 3]])" 494 | ] 495 | }, 496 | "execution_count": 16, 497 | "metadata": {}, 498 | "output_type": "execute_result" 499 | } 500 | ], 501 | "source": [ 502 | "np.array(df)" 503 | ] 504 | }, 505 | { 506 | "cell_type": "code", 507 | "execution_count": 17, 508 | "metadata": { 509 | "scrolled": true 510 | }, 511 | "outputs": [ 512 | { 513 | "name": "stdout", 514 | "output_type": "stream", 515 | "text": [ 516 | "\\begin{bmatrix}\n", 517 | " 0 & 5.00 & 1.00 & 8.00 & 3.00 & 2.00\\\\\n", 518 | " 1 & 8.00 & 4.00 & 8.00 & 9.00 & 6.00\\\\\n", 519 | " 2 & 2.00 & 9.00 & 6.00 & 8.00 & 9.00\\\\\n", 520 | " 3 & 7.00 & 5.00 & 6.00 & 2.00 & 1.00\\\\\n", 521 | " 4 & 7.00 & 2.00 & 4.00 & 3.00 & 3.00\n", 522 | "\\end{bmatrix}\n" 523 | ] 524 | } 525 | ], 526 | "source": [ 527 | "a2l.to_ltx(df, arraytype='bmatrix')" 528 | ] 529 | }, 530 | { 531 | "cell_type": "code", 532 | "execution_count": 18, 533 | "metadata": { 534 | "scrolled": true 535 | }, 536 | "outputs": [ 537 | { 538 | "name": "stdout", 539 | "output_type": "stream", 540 | "text": [ 541 | "\\begin{tabular}{lrrrrr}\n", 542 | "\\toprule\n", 543 | " & a & b & c & d & e \\\\\\n\\midrule\n", 544 | " 0 & 5.00 & 1.00 & 8.00 & 3.00 & 2.00\\\\\n", 545 | " 1 & 8.00 & 4.00 & 8.00 & 9.00 & 6.00\\\\\n", 546 | " 2 & 2.00 & 9.00 & 6.00 & 8.00 & 9.00\\\\\n", 547 | " 3 & 7.00 & 5.00 & 6.00 & 2.00 & 1.00\\\\\n", 548 | " 4 & 7.00 & 2.00 & 4.00 & 3.00 & 3.00\\\\\n", 549 | "\\bottomrule\n", 550 | "\\end{tabular}\n" 551 | ] 552 | } 553 | ], 554 | "source": [ 555 | "a2l.to_ltx(df, arraytype='tabular')" 556 | ] 557 | }, 558 | { 559 | "cell_type": "code", 560 | "execution_count": 19, 561 | "metadata": {}, 562 | "outputs": [ 563 | { 564 | "data": { 565 | "text/html": [ 566 | "
\n", 567 | "\n", 580 | "\n", 581 | " \n", 582 | " \n", 583 | " \n", 584 | " \n", 585 | " \n", 586 | " \n", 587 | " \n", 588 | " \n", 589 | " \n", 590 | " \n", 591 | " \n", 592 | " \n", 593 | " \n", 594 | " \n", 595 | " \n", 596 | " \n", 597 | " \n", 598 | " \n", 599 | " \n", 600 | " \n", 601 | " \n", 602 | " \n", 603 | " \n", 604 | " \n", 605 | " \n", 606 | " \n", 607 | " \n", 608 | " \n", 609 | "
pets
0cat
1dog
2bird
3snake
4honey badger
\n", 610 | "
" 611 | ], 612 | "text/plain": [ 613 | " pets\n", 614 | "0 cat\n", 615 | "1 dog\n", 616 | "2 bird\n", 617 | "3 snake\n", 618 | "4 honey badger" 619 | ] 620 | }, 621 | "execution_count": 19, 622 | "metadata": {}, 623 | "output_type": "execute_result" 624 | } 625 | ], 626 | "source": [ 627 | "df2 = pd.DataFrame(['cat', 'dog', 'bird', 'snake', 'honey badger'], columns=['pets'])\n", 628 | "df2" 629 | ] 630 | }, 631 | { 632 | "cell_type": "code", 633 | "execution_count": 20, 634 | "metadata": {}, 635 | "outputs": [ 636 | { 637 | "data": { 638 | "text/html": [ 639 | "
\n", 640 | "\n", 653 | "\n", 654 | " \n", 655 | " \n", 656 | " \n", 657 | " \n", 658 | " \n", 659 | " \n", 660 | " \n", 661 | " \n", 662 | " \n", 663 | " \n", 664 | " \n", 665 | " \n", 666 | " \n", 667 | " \n", 668 | " \n", 669 | " \n", 670 | " \n", 671 | " \n", 672 | " \n", 673 | " \n", 674 | " \n", 675 | " \n", 676 | " \n", 677 | " \n", 678 | " \n", 679 | " \n", 680 | " \n", 681 | " \n", 682 | " \n", 683 | " \n", 684 | " \n", 685 | " \n", 686 | " \n", 687 | " \n", 688 | " \n", 689 | " \n", 690 | " \n", 691 | " \n", 692 | " \n", 693 | " \n", 694 | " \n", 695 | " \n", 696 | " \n", 697 | " \n", 698 | " \n", 699 | " \n", 700 | " \n", 701 | " \n", 702 | " \n", 703 | " \n", 704 | " \n", 705 | " \n", 706 | " \n", 707 | " \n", 708 | " \n", 709 | " \n", 710 | " \n", 711 | " \n", 712 | "
abcdepets
051832cat
184896dog
229689bird
375621snake
472433honey badger
\n", 713 | "
" 714 | ], 715 | "text/plain": [ 716 | " a b c d e pets\n", 717 | "0 5 1 8 3 2 cat\n", 718 | "1 8 4 8 9 6 dog\n", 719 | "2 2 9 6 8 9 bird\n", 720 | "3 7 5 6 2 1 snake\n", 721 | "4 7 2 4 3 3 honey badger" 722 | ] 723 | }, 724 | "execution_count": 20, 725 | "metadata": {}, 726 | "output_type": "execute_result" 727 | } 728 | ], 729 | "source": [ 730 | "df_mixed = df.join(df2)\n", 731 | "df_mixed" 732 | ] 733 | }, 734 | { 735 | "cell_type": "code", 736 | "execution_count": 21, 737 | "metadata": {}, 738 | "outputs": [ 739 | { 740 | "name": "stdout", 741 | "output_type": "stream", 742 | "text": [ 743 | "\\begin{tabular}{lrrrrrr}\n", 744 | "\\toprule\n", 745 | " & a & b & c & d & e & pets \\\\\\n\\midrule\n", 746 | " 0 & 5.00 & 1.00 & 8.00 & 3.00 & 2.00 & cat \\\\\n", 747 | " 1 & 8.00 & 4.00 & 8.00 & 9.00 & 6.00 & dog \\\\\n", 748 | " 2 & 2.00 & 9.00 & 6.00 & 8.00 & 9.00 & bird \\\\\n", 749 | " 3 & 7.00 & 5.00 & 6.00 & 2.00 & 1.00 & snake \\\\\n", 750 | " 4 & 7.00 & 2.00 & 4.00 & 3.00 & 3.00 & honey badger\\\\\n", 751 | "\\bottomrule\n", 752 | "\\end{tabular}\n" 753 | ] 754 | } 755 | ], 756 | "source": [ 757 | "a2l.to_ltx(df_mixed, arraytype='tabular')" 758 | ] 759 | }, 760 | { 761 | "cell_type": "code", 762 | "execution_count": 22, 763 | "metadata": {}, 764 | "outputs": [ 765 | { 766 | "name": "stdout", 767 | "output_type": "stream", 768 | "text": [ 769 | "\\begin{array}{ c, c}\n", 770 | " 1.23 & 23.46\\\\\n", 771 | " 456.23 & 8.24\n", 772 | "\\end{array}\n" 773 | ] 774 | } 775 | ], 776 | "source": [ 777 | "A = np.array([[1.23456, 23.45678],[456.23, 8.239521]])\n", 778 | "a2l.to_ltx(A, frmt = '{:6.2f}', arraytype = 'array')" 779 | ] 780 | }, 781 | { 782 | "cell_type": "code", 783 | "execution_count": 23, 784 | "metadata": {}, 785 | "outputs": [], 786 | "source": [ 787 | "A = np.array([[1.23456, 23.45678],[456.72+392.71j, 8.239521]])" 788 | ] 789 | }, 790 | { 791 | "cell_type": "code", 792 | "execution_count": null, 793 | "metadata": {}, 794 | "outputs": [], 795 | "source": [] 796 | } 797 | ], 798 | "metadata": { 799 | "kernelspec": { 800 | "display_name": "Python 3 (ipykernel)", 801 | "language": "python", 802 | "name": "python3" 803 | }, 804 | "language_info": { 805 | "codemirror_mode": { 806 | "name": "ipython", 807 | "version": 3 808 | }, 809 | "file_extension": ".py", 810 | "mimetype": "text/x-python", 811 | "name": "python", 812 | "nbconvert_exporter": "python", 813 | "pygments_lexer": "ipython3", 814 | "version": "3.8.13" 815 | }, 816 | "latex_envs": { 817 | "LaTeX_envs_menu_present": true, 818 | "autoclose": false, 819 | "autocomplete": true, 820 | "bibliofile": "biblio.bib", 821 | "cite_by": "apalike", 822 | "current_citInitial": 1, 823 | "eqLabelWithNumbers": true, 824 | "eqNumInitial": 1, 825 | "hotkeys": { 826 | "equation": "Ctrl-E", 827 | "itemize": "Ctrl-I" 828 | }, 829 | "labels_anchors": false, 830 | "latex_user_defs": false, 831 | "report_style_numbering": false, 832 | "user_envs_cfg": false 833 | }, 834 | "toc": { 835 | "base_numbering": 1, 836 | "nav_menu": {}, 837 | "number_sections": true, 838 | "sideBar": true, 839 | "skip_h1_title": false, 840 | "title_cell": "Table of Contents", 841 | "title_sidebar": "Contents", 842 | "toc_cell": false, 843 | "toc_position": {}, 844 | "toc_section_display": true, 845 | "toc_window_display": false 846 | }, 847 | "varInspector": { 848 | "cols": { 849 | "lenName": 16, 850 | "lenType": 16, 851 | "lenVar": 40 852 | }, 853 | "kernels_config": { 854 | "python": { 855 | "delete_cmd_postfix": "", 856 | "delete_cmd_prefix": "del ", 857 | "library": "var_list.py", 858 | "varRefreshCmd": "print(var_dic_list())" 859 | }, 860 | "r": { 861 | "delete_cmd_postfix": ") ", 862 | "delete_cmd_prefix": "rm(", 863 | "library": "var_list.r", 864 | "varRefreshCmd": "cat(var_dic_list()) " 865 | } 866 | }, 867 | "types_to_exclude": [ 868 | "module", 869 | "function", 870 | "builtin_function_or_method", 871 | "instance", 872 | "_Feature" 873 | ], 874 | "window_display": false 875 | }, 876 | "vscode": { 877 | "interpreter": { 878 | "hash": "8264b34d39e23030c6fea90141f98fb73263e8f59eb03375f5597f19271b8965" 879 | } 880 | } 881 | }, 882 | "nbformat": 4, 883 | "nbformat_minor": 2 884 | } 885 | --------------------------------------------------------------------------------