コード例 #1
0
ファイル: Context.py プロジェクト: NextThought/nti.plasTeX
    def newenvironment(self, name, nargs=0, definition=None, opt=None):
        """
        Create a \\newenvironment

        Required Arguments:
        name -- name of the macro to create
        nargs -- integer number of arguments that the macro has
        definition -- two-element tuple containing the LaTeX definition.
            Each element should be a string.  The first element
            corresponds to the beginning of the environment, and the
            second element is the end of the environment.
        opt -- string containing the LaTeX code to use in the
            optional argument

        Examples::
            c.newenvironment('mylist', 0, (r'\\begin{itemize}', r'\\end{itemize}'))

        """
        name = str(name)
        # Macro already exists
        if name in self:
            if not issubclass(self[name],
                              (plasTeX.NewCommand, plasTeX.Definition)):
                return
            macrolog.debug('redefining environment "%s"', name)

        if nargs is None:
            nargs = 0
        assert isinstance(nargs, int), 'nargs must be an integer'

        if definition is not None:
            assert isinstance(definition, (tuple,list)), \
                'definition must be a list or tuple'
            assert len(definition) == 2, 'definition must have 2 elements'

            if isinstance(definition[0], string_types):
                definition[0] = [x for x in Tokenizer(definition[0], self)]
            if isinstance(definition[1], string_types):
                definition[1] = [x for x in Tokenizer(definition[1], self)]

        if isinstance(opt, string_types):
            opt = [x for x in Tokenizer(opt, self)]

        macrolog.debug1('creating newenvironment %s', name)

        # Begin portion
        newclass = type(name, (plasTeX.NewCommand, ), {
            'nargs': nargs,
            'opt': opt,
            'definition': definition[0]
        })
        self.addGlobal(name, newclass)

        # End portion
        newclass = type('end' + name, (plasTeX.NewCommand, ), {
            'nargs': 0,
            'opt': None,
            'definition': definition[1]
        })
        self.addGlobal('end' + name, newclass)
コード例 #2
0
    def newenvironment(self,
                       name,
                       nargs=0,
                       def_before=None,
                       def_after=None,
                       opt=None):
        """
        Create a \\newenvironment

        Required Arguments:
        name -- name of the macro to create
        nargs -- integer number of arguments that the macro has
        def_before -- string corresponding to TeX code inserted before the
            environment
        def_after -- string corresponding to TeX code inserted after the
            environment
        opt -- string containing the LaTeX code to use in the
            optional argument

        Examples::
            c.newenvironment('mylist', 0, [r'\\begin{itemize}', r'\\end{itemize}'])

        """
        # Macro already exists
        if name in list(self.keys()):
            if not issubclass(self[name],
                              (plasTeX.NewCommand, plasTeX.Definition)):
                return
            macrolog.debug('redefining environment "%s"', name)

        if nargs is None:
            nargs = 0
        assert isinstance(nargs, int), 'nargs must be an integer'

        if def_before:
            def_before = list(Tokenizer(def_before, self))
        if def_after:
            def_after = list(Tokenizer(def_after, self))

        if isinstance(opt, str):
            opt = list(Tokenizer(opt, self))

        macrolog.debug('creating newenvironment %s', name)

        # Begin portion
        newclass = type(name, (plasTeX.NewCommand, ), {
            'nargs': nargs,
            'opt': opt,
            'definition': def_before
        })
        self.addGlobal(name, newclass)

        # End portion
        newclass = type('end' + name, (plasTeX.NewCommand, ), {
            'nargs': 0,
            'opt': None,
            'definition': def_after
        })
        self.addGlobal('end' + name, newclass)
コード例 #3
0
ファイル: Context.py プロジェクト: nyraghu/plastex
    def newcommand(self,
                   name,
                   nargs=0,
                   definition=None,
                   opt=None,
                   can_overwrite=False,
                   must_overwrite=False):
        """
        Create a \\newcommand

        Required Arguments:
        name -- name of the macro to create
        nargs -- integer number of arguments that the macro has
        definition -- string containing the LaTeX definition
        opt -- string containing the LaTeX code to use in the
            optional argument
        can_overwrite -- bool controlling whether we can overwrite an existing definition
        must_overwrite -- bool controlling whether we must overwrite an existing definition

        Examples::
            c.newcommand('bold', 1, r'\\textbf{#1}')
            c.newcommand('foo', 2, r'{\\bf #1#2}', opt='myprefix')

        """
        if must_overwrite and not name in self:
            log.warning(
                'Cannot redefine {} which is not defined.'.format(name))
            return

        # Macro already exists
        if name in self:
            if can_overwrite or issubclass(
                    self[name],
                (plasTeX.NewCommand, plasTeX.UnrecognizedMacro,
                 plasTeX.Definition, relax, plasTeX.TheCounter)):
                macrolog.debug('redefining command "%s"', name)
            else:
                log.warning('{} is already defined. '.format(name) +\
                            'The new definition is ignored.')
                return

        if nargs is None:
            nargs = 0
        assert isinstance(nargs, int), 'nargs must be an integer'

        if isinstance(definition, str):
            definition = list(Tokenizer(definition, self))

        if isinstance(opt, str):
            opt = list(Tokenizer(opt, self))

        macrolog.debug('creating newcommand %s', name)
        newclass = type(name, (plasTeX.NewCommand, ), {
            'nargs': nargs,
            'opt': opt,
            'definition': definition
        })

        self.addGlobal(name, newclass)
コード例 #4
0
def newcommand(self, name, nargs=0, definition=None, opt=None):
    if nargs is None:
        nargs = 0
    assert isinstance(nargs, int), 'nargs must be an integer'

    if isinstance(definition, str):
        definition = [x for x in Tokenizer(definition, self)]

    if isinstance(opt, str):
        opt = [x for x in Tokenizer(opt, self)]

    newclass = type(name, (NewCommand,), {'nargs': nargs, 'opt': opt, 'definition': definition})

    self.addGlobal(name, newclass)
コード例 #5
0
    def newdef(self, name, args=None, definition=None, local=True):
        """
        Create a \\def

        Required Arguments:
        name -- name of the macro to create
        args -- string containing the TeX argument profile
        definition -- string containing the LaTeX definition

        Keyword Arguments:
        local -- indicates whether this macro is local or global

        Examples::
            c.newdef('bold', '#1', '{\\bf #1}')
            c.newdef('put', '(#1,#2)#3', '\\dostuff{#1}{#2}{#3}')

        """
        # Macro already exists
#       if self.has_key(name):
#           if not issubclass(self[name], (plasTeX.NewCommand,
#                                          plasTeX.Definition)):
#               return
#           macrolog.debug('redefining definition "%s"', name)

        if isinstance(definition, str):
            definition = [x for x in Tokenizer(definition, self)]

        macrolog.debug('creating def %s', name)
        newclass = type(name, (plasTeX.Definition,),
                       {'args':args, 'definition':definition})

        if local:
            self.addLocal(name, newclass)
        else:
            self.addGlobal(name, newclass)
コード例 #6
0
    def newcommand(self, name, nargs=0, definition=None, opt=None):
        """
        Create a \\newcommand

        Required Arguments:
        name -- name of the macro to create
        nargs -- integer number of arguments that the macro has
        definition -- string containing the LaTeX definition
        opt -- string containing the LaTeX code to use in the
            optional argument

        Examples::
            c.newcommand('bold', 1, r'\\textbf{#1}')
            c.newcommand('foo', 2, r'{\\bf #1#2}', opt='myprefix')

        """
        # Macro already exists
        if name in list(self.keys()):
            if not issubclass(self[name],
                              (plasTeX.NewCommand, plasTeX.UnrecognizedMacro,
                               plasTeX.Definition, relax)):
                if not issubclass(self[name], plasTeX.TheCounter):
                    return
            macrolog.debug('redefining command "%s"', name)

        if nargs is None:
            nargs = 0
        assert isinstance(nargs, int), 'nargs must be an integer'

        if isinstance(definition, str):
            definition = list(Tokenizer(definition, self))

        if isinstance(opt, str):
            opt = list(Tokenizer(opt, self))

        macrolog.debug('creating newcommand %s', name)
        newclass = type(name, (plasTeX.NewCommand, ), {
            'nargs': nargs,
            'opt': opt,
            'definition': definition
        })

        self.addGlobal(name, newclass)
コード例 #7
0
def newenvironment(self, name, nargs=0, def_before=None, def_after=None, opt=None):
    if nargs is None:
        nargs = 0
    assert isinstance(nargs, int), 'nargs must be an integer'

    if def_before:
        def_before = list(Tokenizer(def_before, self))
    if def_after:
        def_after = list(Tokenizer(def_after, self))

    if isinstance(opt, str):
        opt = [x for x in Tokenizer(opt, self)]

    # Begin portion
    newclass = type(name, (NewCommand,), {'nargs': nargs, 'opt': opt, 'definition': def_before})
    self.addGlobal(name, newclass)

    # End portion
    newclass = type('end' + name, (NewCommand,), {'nargs': 0, 'opt': None, 'definition': def_after})
    self.addGlobal('end' + name, newclass)