Difference between revisions of "Creating Custom Contexts"
(Created page from data originally in Modifying Contexts (advanced). More will need to be added.) |
|||
(2 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
− | Suppose that you have accumulated a nontrivial number of context modifications |
+ | Suppose that you have accumulated a nontrivial number of context modifications that 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 <code>MyContext</code>. Whatever you end up naming your context, it is recommended practice to begin your filename with <code>context</code> and it must end with <code>.pl</code> as all macro files do. So your filename might be <code>contextMyContext.pl</code>. When we're done we'll put the file in the <code>/pg/macros/</code> sudbdirectory of the WebWork installation, or in your course's <code>templates/macros</code> directory. |
First, what do you want to call your context? Let's say we will name it <code>MyContext</code>. Whatever you end up naming your context, it is recommended practice to begin your filename with <code>context</code> and it must end with <code>.pl</code> as all macro files do. So your filename might be <code>contextMyContext.pl</code>. When we're done we'll put the file in the <code>/pg/macros/</code> sudbdirectory of the WebWork installation, or in your course's <code>templates/macros</code> directory. |
||
Line 13: | Line 13: | ||
When defining a new context in your file, the code will proceed as follows: |
When defining a new context in your file, the code will proceed as follows: |
||
− | # copy an existing context which is close to what you want |
+ | # copy an existing context which is close to what you want, |
# modify what you want changed. |
# modify what you want changed. |
||
− | For example, this line copies <code> |
+ | For example, this line copies the <code>Numeric</code> Context into a new <code>MyContext</code> Context: |
− | |||
− | $context{MyContext} = Parser::Context->getCopy("Numeric"); |
||
+ | $main::context{MyContext} = Parser::Context->getCopy("Numeric"); |
||
+ | Here, the <code>%main::context</code> hash is used to store custom Contexts. The <code>Context()</code> command knows to look here first for contexts, and then to look in the default Context list, so when you put a new Context here, that makes it available via <code>Context("MyContext")</code>. |
||
The following lines modify some aspects of this context: |
The following lines modify some aspects of this context: |
||
− | $context{MyContext}->functions->disable("Trig"); |
+ | $main::context{MyContext}->functions->disable("Trig"); |
− | $context{MyContext}->parens->set('('=>{type=>'Vector'}); |
+ | $main::context{MyContext}->parens->set('('=>{type=>'Vector'}); |
etc... |
etc... |
||
Line 33: | Line 35: | ||
That's it. Save it, put it in <code>/pg/macros/</code> or your course <code>templates/macros</code> directory, and load it in your problems with <code>loadMacros()</code>. |
That's it. Save it, put it in <code>/pg/macros/</code> or your course <code>templates/macros</code> directory, and load it in your problems with <code>loadMacros()</code>. |
||
+ | |||
+ | You can save more complex changes as well, like the added functions or operators listed in the [[Modifying Contexts (advanced)]] page. |
||
+ | |||
+ | <br> |
||
+ | |||
+ | [[Category:Contexts]] |
||
+ | [[Category:MathObjects]] |
Latest revision as of 10:52, 7 June 2013
Suppose that you have accumulated a nontrivial number of context modifications that 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 contextMyContext.pl
. 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,
loadMacros( "PGstandard.pl", "MathObjects.pl", "contextMyContext.pl", "PGcourse.pl", );
When defining a new context in your file, the code will proceed as follows:
- copy an existing context which is close to what you want,
- modify what you want changed.
For example, this line copies the Numeric
Context into a new MyContext
Context:
$main::context{MyContext} = Parser::Context->getCopy("Numeric");
Here, the %main::context
hash is used to store custom Contexts. The Context()
command knows to look here first for contexts, and then to look in the default Context list, so when you put a new Context here, that makes it available via Context("MyContext")
.
The following lines modify some aspects of this context:
$main::context{MyContext}->functions->disable("Trig"); $main::context{MyContext}->parens->set('('=>{type=>'Vector'}); etc...
The last line in the file should be
1;
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()
.
You can save more complex changes as well, like the added functions or operators listed in the Modifying Contexts (advanced) page.