# -*- 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-2013, Geoffrey M. Poore All rights reserved. Licensed under the BSD 3-Clause License: http://www.opensource.org/licenses/BSD-3-Clause ''' # Imports # 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. ''' def __init__(self, fmtr='str'): ''' Initialize, optionally setting the formatter. ''' self._sympy_latex_is_init = False self.set_formatter(fmtr) # If not using the 'sympy_latex' formatter, set up a dummy interface if fmtr != 'sympy_latex': self.set_sympy_latex = self._dummy_set_sympy_latex self.sympy_latex = self._dummy_sympy_latex # The following variables and methods will be created within instances # of the class during execution. They are listed here for bookkeeping # purposes. # # String variables for keeping track of TeX information. Most are # actually needed; the rest are included for completeness. # * inputtype # * inputsession # * inputgroup # * inputinstance # * inputcommand # * inputcontext # * inputline # # Future file handle for output that is saved via macros # * macrofile # # Future formatter function that is used to format output # * formatter # 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, since we don't want to import # anything from SymPy unless it is actually used, to keep things clean and # fast. Part of this involves creating a dummy interface, for when SymPy # isn't available. # # First we create a tuple containing all LaTeX math styles. These are # the contexts that SymPy's latex printer must adapt to. Then we create # a dummy dictionary of settings functions and a dummy interface # function. Both of these raise UserWarning. They attempt to proceed # if warnings happen to be suppressed. # The style order doesn't matter, but it corresponds to that of \mathchoice _sympy_styles = ('display', 'text', 'script', 'scriptscript') def _dummy_set_sympy_latex(self): ''' A dummy function for setting the latex printer. ''' raise UserWarning("Attempt to use sympy_latex before initializing; use method init_sympy_latex()") pass def _dummy_sympy_latex(self, expr, **settings): ''' A dummy interface to SymPy's LatexPrinter ''' raise UserWarning("Attempt to use sympy_latex before initializing; use method init_sympy_latex()") return str(expr) # 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(). The initialization code only runs if initialization hasn't already occurred. ''' if self._sympy_latex_is_init: return else: self._sympy_latex_is_init = True # 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. This folds fractional powers (slash # rather than \frac). Apparently, the folding only applies for # powers that are Rationals; symbolic powers aren't folded. # # 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 = dict() self._sympy_latex_settings['display'] = { 'mat_str': 'pmatrix', 'mat_delim': None} self._sympy_latex_settings['text'] = { #'fold_frac_powers': True, 'mat_str': 'smallmatrix', 'mat_delim': '('} self._sympy_latex_settings['script'] = self._sympy_latex_settings['text'] self._sympy_latex_settings['scriptscript'] = self._sympy_latex_settings['text'] # 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. self.set_sympy_latex = dict() def set_sympy_latex(style, **kwargs): if style in self._sympy_styles: self._sympy_latex_settings[style].update(kwargs) elif style == 'all': for s in self._sympy_styles: self._sympy_latex_settings[s].update(kwargs) else: raise UserWarning('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 dictionaries of functions for modifying the settings are in # place, we are ready to create the actual interface. self._make_sympy_latex() # Finally, we 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{}{}{