4.2.3 The Document Context

It is possible to define commands using the same interface that is used by the plasTeX engine itself. This interface belongs to the Context object (usually accessed through the document object’s context attribute). Defining commands using the context object is generally done in the ProcessOptions function of a package. The following methods of the context object create new commands.

Method

Purpose

newcounter

creates a new counter, and also creates a command called \thecounter which generates the formatted version of the counter. This macro corresponds to the \newcounter macro in LaTeX.

newcount

corresponds to TeX’s \newcount command.

newdimen

corresponds to TeX’s \newdimen command.

newskip

corresponds to TeX’s \newskip command.

newmuskip

corresponds to TeX’s \newmuskip command.

newif

corresponds to TeX’s \newif command. This command also generates macros for \ifcommandtrue and \ifcommandfalse.

newcommand

corresponds to LaTeX’s \newcommand macro.

newenvironment

corresponds to LaTeX’s \newenvironment macro.

newdef

corresponds to TeX’s \def command.

chardef

corresponds to TeX’s \chardef command.

Note: Since many of these methods accept strings containing LaTeX markup, you need to remember that the category codes of some characters can be changed during processing. If you are defining macros using these methods in the ProcessOptions function in a package, you should be safe since this function is executed in the preamble of the document where category codes are not changed frequently. However, if you define a macro with this interface in a context where the category codes are not set to the default values, you will have to adjust the markup in your macros accordingly.

Below is an example of using this interface within the context of a package to define some commands. For the full usage of these methods see the API documentation of the Context object in section 6.5.

def ProcessOptions(options, document):
    context = document.context

    # Create some counters
    context.newcounter('secnumdepth', initial=3)
    context.newcounter('tocdepth', initial=2)

    # \newcommand{\config}[2][general]{\textbf{#2:#1}
    context.newcommand('config', 2, r'\textbf{#2:#1}', opt='general')

    # \newenvironment{note}{\textbf{Note:}}{}
    context.newenvironment('note', 0, (r'\textbf{Note:}', r''))