plasTeX 3.0 — A Python Framework for Processing LaTeX Documents

6.5.1 Context Objects

class Context([load])

Instantiate a new context.

If the load  argument is set to true, the context will load all of the base macros defined in plasTeX. This includes all of the macros used in the standard TeX and LaTeX distributions.

contexts

stack of all macro and category code collections currently in the document being processed. The item at index 0 include the global macro set and default category codes.

counters

a dictionary of counters.

currentlabel

the object that is given the label when a \label macro is invoked.

isMathMode

boolean that specifies if we are currently in TeX’s math mode or not.

labels

a dictionary of labels and the objects that they refer to.

addGlobal(key, value )

add a macro value  with name key  to the global namespace.

addLocal(key, value )

add a macro value  with name key  to the current namespace.

append([context])

same as push()

catcode(char, code )

set the category code for a character in the current scope. char  is the character that will have its category code changed. code  is the TeX category code (0-15) to change it to.

chardef(name, num )

create a new TeX chardef like \chardef.

name  is the name of the command to create.

num  is the character number to use.

__getitem__(key )

look through the stack of macros and return the one with the name key . The return value is an instance of the requested macro, not a reference to the macro class. This method allows you to use Python’s dictionary syntax to retrieve the item from the context as shown below.

tex.context['section']

importMacros(context )

import macros from another context into the global namespace. The argument, context , must be a dictionary of macros.

label(label )

set the given label to the currently labelable object. An object can only have one label associated with it.

let(dest, source )

create a new TeX let like \let.

dest  is the command sequence to create.

source  is the token to set the command sequence equivalent to.

Example

c.let('bgroup', BeginGroup('{'))

loadBaseMacros()

imports all of the base macros defined by plasTeX. This includes all of the macros specified by the TeX and LaTeX systems.

loadLanguage(language, document )

loads a language package to configure names such as \figurename, \tablename, etc. See Section 6.5.2 for more information.

language  is a string containing the name of the language file to load.

document  is the document object being processed.

loadINIPackage(inifile )

load an INI formatted package file (see section 4.3 for more information).

loadPackage(tex, file, [options])

loads a LaTeX package.

tex  is the TeX processor to use in parsing the package content

file  is the name of the package to load

options  is a dictionary containing the options to pass to the package. This generally comes from the optional argument on a \usepackage or \documentclass macro.

The package being loaded by this method can be one of three type: 1) a native LaTeX package, 2) a Python package, or 3) an INI formatted file. The Python version of the package is searched for first. If it is found, it is loaded and an INI version of the package is also loaded if it exists. If there is no Python version, the true LaTeX version of the package is loaded. If there is an INI version of the package in the same directory as the LaTeX version, that file is loaded also.

newcommand(name[, nargs[, definition[, opt]]])

create a new LaTeX command like \newcommand.

name  is the name of the macro to create.

nargs  is the number of arguments including optional arguments.

definition  is a string containing the macro definition.

opt  is a string containing the default optional value.

Examples

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

newcount(name[, initial])

create a new count like \newcount.

newcounter(name, [resetby, initial, format])

create a new counter like \newcounter.

name  is the name of the counter to create.

resetby  is the counter that, when incremented, will reset the new counter.

initial  is the initial value for the counter.

format  is the printed format of the counter.

In addition to creating a new counter macro, another macro corresponding to the \thename  is created which prints the value of the counter just like in LaTeX.

newdef(name[, args[, definition[, local]]])

create a new TeX definition like \def.

name  is the name of the definition to create.

args  is a string containing the TeX argument profile.

definition  is a string containing the macro code to expand when the definition is invoked.

local  is a boolean that specifies that the definition should only exist in the local scope. The default value is true.

Examples

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

newdimen(name[, initial])

create a new dimen like \newdimen.

newenvironment(name[, nargs[, definition[, opt]]])

create a new LaTeX environment like \newenvironment. This works exactly like the newcommand() method, except that the definition  argument is a two element tuple where the first element is a string containing the macro content to expand at the \begin, and the second element is the macro content to expand at the \end.

Example

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

newif(name[, initial])

create a new if like \newif. This also creates macros corresponding to \name true and \name false.

newmuskip(name[, initial])

create a new muskip like \newmuskip.

newskip(name[, initial])

create a new skip like \newskip.

packages

a dictionary of LaTeX packages. The keys are the names of the packages. The values are dictionaries containing the options that were specified when the package was loaded.

pop([obj])

pop the top scope off of the stack. If obj  is specified, continue to pop scopes off of the context stack until the scope that was originally added by obj  is found.

push([context])

add a new scope to the stack. If a macro instance context  is specified, the new scope’s namespace is given by that object.

ref(obj, label )

set up a reference for resolution.

obj  is the macro object that is doing the referencing.

label  is the label of the node that obj  is looking for.

If the item that obj  is looking for has already been labeled, the idref attribute of obj  is set to the abject. Otherwise, the reference is stored away to be resolved later.

setVerbatimCatcodes()

set the current set of category codes to the set used for the verbatim environment.

whichCode(char )

return the character code that char  belongs to. The category codes are the same codes used by TeX and are defined in the Token class.