Creating Custom Contexts

From WeBWorK_wiki
Revision as of 18:59, 12 August 2012 by Dpvc (talk | contribs) (Created page from data originally in Modifying Contexts (advanced). More will need to be added.)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Suppose that you have accumulated a nontrivial number of context modifications which you use frequently when writing problems. You can combine all of these modifications into a single file and define your own context.

First, what do you want to call your context? Let's say we will name it MyContext. Whatever you end up naming your context, it is recommended practice to begin your filename with context and it must end with .pl as all macro files do. So your filename might be When we're done we'll put the file in the /pg/macros/ sudbdirectory of the WebWork installation, or in your course's templates/macros directory.

You will then include the file in the loadMacros() call at the top of your file along with the other macros you load. You should place your context file after the standard macro files. So, for example,


When defining a new context in your file, the code will proceed as follows:

  1. copy an existing context which is close to what you want
  2. modify what you want changed.

For example, this line copies Context("Numeric") into a new Context("MyContext"):

   $context{MyContext} = Parser::Context->getCopy("Numeric");

The following lines modify some aspects of this context:


The last line in the file should be


which makes the file return a non-zero value (a zero value would indicate an error within the file).

That's it. Save it, put it in /pg/macros/ or your course templates/macros directory, and load it in your problems with loadMacros().