diff --git a/Slides/math_helper.py b/Slides/math_helper.py index ce4c1da..cb2adc1 100644 --- a/Slides/math_helper.py +++ b/Slides/math_helper.py @@ -1,140 +1,140 @@ #!/usr/bin/env pyton ################################################################ import numpy as np from sympy import latex from IPython.display import display, Math from . import presentation_helper as ph ################################################################ def print_latex(sstr, *argv, ret=False): args = [] # print argv for a in argv: try: ff = a._repr_latex_() - ff = ff[2:-2] + ff = ff[1:-1] except Exception: # print(e) ff = latex(a) args.append(ff) sstr = sstr.format(*args) # print (sstr) res = Math(latex(sstr)) display(res) if ret is True: return res ################################################################ class ColoredMatrix: color_def = { 'b': 'blue', 'g': 'green', 'r': 'red', 'p': 'purple' } def __init__(self, mat): if isinstance(mat, ColoredMatrix): self.mat = mat.mat else: self.mat = mat self.colors = np.zeros(self.mat.shape, dtype='S10') self.alternative = np.zeros(self.mat.shape, dtype='S10') self.sym = False def __len__(self): return self.mat.__len__() def dot(self, mat): return self.mat.dot(mat) def __mul__(self, n): return self.mat.__mul__(n) def __rmul__(self, n): return n*self.mat def __div__(self, n): temp = ColoredMatrix(self.mat.__div__(n)) temp.colors[:] = self.colors[:] return temp def evalf(self, *args): mat_eval = self.mat.evalf(*args) new_mat = ColoredMatrix(mat_eval) new_mat.colors = self.colors.copy() return new_mat def __getitem__(self, index): return self.mat.__getitem__(index) def __setitem__(self, index, value): return self.mat.__setitem__(index, value) def _get_coeff(self, i, j=0): if self.alternative[i, j] != '': return self.alternative[i, j] else: return latex(self.mat[i, j]) def _colored_coeff(self, i, j=0): if self.sym is True: if i == self.mat.shape[0]-1 and j == 0: return 'Sym.' elif i > j: return '' if self.colors[i, j] in self.color_def: color = self.color_def[self.colors[i, j]] else: color = 'black' coeff = self._get_coeff(i, j) if coeff == '': return '' return (r'{\color{' + color + '}{' + coeff + '}}') def _repr_latex_(self): m = self.mat.shape[0] if len(self.mat.shape) > 1 and self.mat.shape[1] > 1: n = self.mat.shape[1] result = '' for i in range(0, m): row = [] for j in range(0, n): row.append(self._colored_coeff(i, j)) result += ' & '.join(row) if i < m-1: result += r'\\' result = (r'\left[\begin{array}{' + 'c'*n + '}' + result + r'\end{array}\right]') else: rows = [] for i in range(0, m): rows.append(self._colored_coeff(i)) result = r'\\'.join(rows) result = (r'\left\{\begin{matrix}' + result + r'\end{matrix}\right\}') return '$$' + result + '$$' ################################################################ def init_printing(): ph.registerFormatter(ColoredMatrix, 'image/png', ph._print_latex_png) ################################################################ init_printing() diff --git a/scripts/pLatex b/scripts/pLatex index eb294c7..c597be2 100755 --- a/scripts/pLatex +++ b/scripts/pLatex @@ -1,39 +1,40 @@ #! /usr/bin/python3 import subprocess import os.path import argparse from PythonLatex.generate_latex import interpretPython ################################################################ parser = argparse.ArgumentParser(description='Python embedded in Latex tool') parser.add_argument('filename_in', type=str, help='The path of the file to parse') parser.add_argument('--reexec_flag', action='store_true', help='TODO') parser.add_argument('-g', '--generate_pdf', action='store_true', help='Ask to generate the pdf directly') parser.add_argument('-c', '--continue_flag', action='store_true', help='Ask to continue even after errors') args = parser.parse_args() filename_in = args.filename_in reexec_flag = args.reexec_flag _continue = args.continue_flag generate_pdf = args.generate_pdf basename, ext = os.path.splitext(filename_in) if not ext == '.ptex': - raise Exception('invalid file extension: "{0}" in file {1}'.format(ext, filename_in)) + raise Exception( + 'invalid file extension: "{0}" in file {1}'.format(ext, filename_in)) filename_out = basename + '.tex' ################################################################ out = interpretPython(filename_in, _continue, reexec_flag) fout = open(filename_out, 'w') fout.write(out) fout.close() log = open('log.latex', 'w') if generate_pdf is True: ret = subprocess.call( 'pdflatex -interaction nonstopmode ' + filename_out, shell=True, stdout=log, stderr=log) if ret: subprocess.call('rubber -d ' + filename_out, shell=True) ################################################################ diff --git a/setup.py b/setup.py index f8e7ad1..d6f3434 100644 --- a/setup.py +++ b/setup.py @@ -1,46 +1,46 @@ from setuptools import setup, find_packages try: from sphinx.setup_command import BuildDoc - cmdclass={'doc': BuildDoc} + cmdclass = {'doc': BuildDoc} except Exception: - cmdclass={} + cmdclass = {} name = 'slides' version = '1.0' release = '1.0.0' setup(name=name, packages=find_packages(), version="0.0.1", author="Guillaume Anciaux", author_email="guillaume.anciaux@epfl.ch", description=("Slides making/convertion tools"), install_requires=["sphinx", "jupyter", "sympy", "pypdf2", "matplotlib", "wand", "dill", "jupyter_contrib_nbextensions"], tests_requires=["pytest"], setup_requires=["pytest-runner"], test_suite="tests", cmdclass=cmdclass, command_options={ 'doc': { 'project': ('setup.py', name), 'version': ('setup.py', version), 'release': ('setup.py', release), 'source_dir': ('setup.py', 'doc')}}, scripts=['scripts/slides', 'scripts/pLatex'], license=""" This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . """)