# -*- coding: utf-8 -*- ''' PythonTeX utilities class for Python scripts. The utilities class provides variables and methods for the individual Python scripts created and executed by PythonTeX. An instance of the class named "pytex" is automatically created in each individual script. Copyright (c) 2012-2014, Geoffrey M. Poore All rights reserved. Licensed under the BSD 3-Clause License: http://www.opensource.org/licenses/BSD-3-Clause ''' # Imports import sys import warnings if sys.version_info.major == 2: import io # Most imports are only needed for SymPy; these are brought in via # "lazy import." Importing unicode_literals here shouldn't ever be necessary # under Python 2. If unicode_literals is imported in the main script, then # all strings in this script will be treated as bytes, and the main script # will try to decode the strings from this script as necessary. The decoding # shouldn't cause any problems, since all strings in this file may be decoded # as valid ASCII. (The actual file is encoded in utf-8, but only characters # within the ASCII subset are actually used). class PythonTeXUtils(object): ''' A class of PythonTeX utilities. Provides variables for keeping track of TeX-side information, and methods for formatting and saving data. The following variables and methods will be created within instances of the class during execution. String variables for keeping track of TeX information. Most are actually needed; the rest are included for completeness. * family * session * restart * command * context * args * instance * line Future file handle for output that is saved via macros * macrofile Future formatter function that is used to format output * formatter ''' def __init__(self, fmtr='str'): ''' Initialize ''' self.set_formatter(fmtr) # We need a function that will process the raw `context` into a # dictionary with attributes _context_raw = None class _DictWithAttr(dict): pass def set_context(self, expr): ''' Convert the string `{context}` into a dict with attributes ''' if not expr or expr == self._context_raw: pass else: self._context_raw = expr self.context = self._DictWithAttr() k_and_v = [map(lambda x: x.strip(), kv.split('=')) for kv in expr.split(',')] for k, v in k_and_v: if v.startswith('!!int '): v = int(float(v[6:])) elif v.startswith('!!float '): v = float(v[8:]) elif v.startswith('!!str '): v = v[6:] self.context[k] = v setattr(self.context, k, v) # A primary use for contextual information is to pass dimensions from the # TeX side to the Python side. To make that as convenient as possible, # we need some length conversion functions. # Conversion reference: http://tex.stackexchange.com/questions/41370/what-are-the-possible-dimensions-sizes-units-latex-understands def pt_to_in(self, expr): ''' Convert points to inches. Accepts numbers, strings of digits, and strings of digits that end with `pt`. ''' try: ans = expr/72.27 except: if expr.endswith('pt'): expr = expr[:-2] ans = float(expr)/72.27 return ans def pt_to_cm(self, expr): ''' Convert points to centimeters. ''' return self.pt_to_in(expr)*2.54 def pt_to_mm(self, expr): ''' Convert points to millimeters. ''' return self.pt_to_in(expr)*25.4 def pt_to_bp(self, expr): ''' Convert points to big (DTP or PostScript) points. ''' return self.pt_to_in(expr)*72 # We need a context-aware interface to SymPy's latex printer. The # appearance of typeset math should depend on where it appears in a # document. (We will refer to the latex printer, rather than the LaTeX # printer, because the two are separate. Compare sympy.printing.latex # and sympy.galgebra.latex_ex.) # # Creating this interface takes some work. We don't want to import # anything from SymPy unless it is actually used, to keep things clean and # fast. # First we create a tuple containing all LaTeX math styles. These are # the contexts that SymPy's latex printer must adapt to. # The style order doesn't matter, but it corresponds to that of \mathchoice _sympy_latex_styles = ('display', 'text', 'script', 'scriptscript') # Create the public functions for the user, and private functions that # they call. Two layers are necessary, because we need to be able to # redefine the functions that do the actual work, once things are # initialized. But we don't want to redefine the public functions, since # that could cause problems if the user defines a new function to be one # of the public functions--the user's function would not change when # the method was redefined. def _sympy_latex(self, expr, **settings): self._init_sympy_latex() return self._sympy_latex(expr, **settings) def sympy_latex(self, expr, **settings): return self._sympy_latex(expr, **settings) def _set_sympy_latex(self, style, **kwargs): self._init_sympy_latex() self._set_sympy_latex(style, **kwargs) def set_sympy_latex(self, style, **kwargs): self._set_sympy_latex(style, **kwargs) # Temporary compatibility with deprecated methods def init_sympy_latex(self): warnings.warn('Method init_sympy_latex() is deprecated; init is now automatic.') self._init_sympy_latex() # Next we create a method that initializes the actual context-aware # interface to SymPy's latex printer. def _init_sympy_latex(self): ''' Initialize a context-aware interface to SymPy's latex printer. This consists of creating the dictionary of settings and creating the sympy_latex method that serves as an interface to SymPy's LatexPrinter. This last step is actually performed by calling self._make_sympy_latex(). ''' # Create dictionaries of settings for different contexts. # # Currently, the main goal is to use pmatrix (or an equivalent) # in \displaystyle contexts, and smallmatrix in \textstyle, # \scriptstyle (superscript or subscript), and \scriptscriptstyle # (superscript or subscript of a superscript or subscript) # contexts. Basically, we want matrix size to automatically # scale based on context. It is expected that additional # customization may prove useful as SymPy's LatexPrinter is # further developed. # # The 'fold_frac_powers' option is probably the main other # setting that might sometimes be nice to invoke in a # context-dependent manner. # # In the default settings below, all matrices are set to use # parentheses rather than square brackets. This is largely a # matter of personal preference. The use of parentheses is based # on the rationale that parentheses are less easily confused with # the determinant and are easier to write by hand than are square # brackets. The settings for 'script' and 'scriptscript' are set # to those of 'text', since all of these should in general # require a more compact representation of things. self._sympy_latex_settings = {'display': {'mat_str': 'pmatrix', 'mat_delim': None}, 'text': {'mat_str': 'smallmatrix', 'mat_delim': '('}, 'script': {'mat_str': 'smallmatrix', 'mat_delim': '('}, 'scriptscript': {'mat_str': 'smallmatrix', 'mat_delim': '('} } # Now we create a function for updating the settings. # # Note that EVERY time the settings are changed, we must call # self._make_sympy_latex(). This is because the _sympy_latex() # method is defined based on the settings, and every time the # settings change, it may need to be redefined. It would be # possible to define _sympy_latex() so that its definition remained # constant, simply drawing on the settings. But most common # combinations of settings allow more efficient versions of # _sympy_latex() to be defined. def _set_sympy_latex(style, **kwargs): if style in self._sympy_latex_styles: self._sympy_latex_settings[style].update(kwargs) elif style == 'all': for s in self._sympy_latex_styles: self._sympy_latex_settings[s].update(kwargs) else: warnings.warn('Unknown LaTeX math style ' + str(style)) self._make_sympy_latex() self._set_sympy_latex = _set_sympy_latex # Now that the dictionaries of settings have been created, and # the function for modifying the settings is in place, we are ready # to create the actual interface. self._make_sympy_latex() # Finally, create the actual interface to SymPy's LatexPrinter def _make_sympy_latex(self): ''' Create a context-aware interface to SymPy's LatexPrinter class. This is an interface to the LatexPrinter class, rather than to the latex function, because the function is simply a wrapper for accessing the class and because settings may be passed to the class more easily. Context dependence is accomplished via LaTeX's \mathchoice macro. This macros takes four arguments: \mathchoice{}{}{