Forum archive 2000-2006

Michael Gage - PGbasicmacros.pl

Michael Gage - PGbasicmacros.pl

by Arnold Pizer -
Number of replies: 0
inactiveTopicPGbasicmacros.pl topic started 5/22/2000; 9:38:55 PM
last post 6/16/2000; 11:10:39 AM
userMichael Gage - PGbasicmacros.pl  blueArrow
5/22/2000; 9:38:55 PM (reads: 6533, responses: 7)


NAME

        PGbasicmacros.pl --- located in the courseScripts directory


SYNPOSIS


DESCRIPTION

Answer blank macros:

These produce answer blanks of various sizes or pop up lists or radio answer buttons. The names for the answer blanks are generated implicitly.

    ans_rule( width )
tex_ans_rule( width )
ans_radio_buttons(value1=>label1, value2,label2 => value3,label3=>...)
pop_up_list(@list) # list consists of (value => label, PR => "Product rule",...)
pop_up_list([@list]) # list consists of values

In the last case, one can use pop_up_list(['?', 'yes', 'no']) to produce a pop-up list containing the three strings listed, and then use str_cmp to check the answer.

To indicate the checked position of radio buttons put a '%' in front of the value: ans_radio_buttons(1, 'Yes','%2','No') will have 'No' checked. tex_ans_rule works inside math equations in HTML_tth mode. It does not work in Latex2HTML mode since this mode produces gif pictures.

The following method is defined in PG.pl for entering the answer evaluators corresponding to answer rules with automatically generated names. The answer evaluators are matched with the answer rules in the order in which they appear on the page.

    ANS(ans_evaluator1, ans_evaluator2,...);

These are more primitive macros which produce answer blanks for specialized cases when complete control over the matching of answers blanks and answer evaluators is desired. The names of the answer blanks must be generated manually, and it is best if they do NOT begin with the default answer prefix (currently AnSwEr).

    labeled_ans_rule(name, width)   # an alias for NAMED_ANS_RULE where width defaults 
to 20 if omitted.
    NAMED_ANS_RULE(name, width)
NAMED_ANS_BOX(name, rows, cols)
NAMED_ANS_RADIO(name, value,label,)
NAMED_ANS_RADIO_EXTENSION(name, value,label)
NAMED_ANS_RADIO_BUTTONS(name,value1,label1,value2,label2,...)
check_box('-name' =>answer5,'-value' =>'statement3','-label' =>'I loved this
course!' )
NAMED_POP_UP_LIST($name, @list) # list consists of (value => tag, PR => "Product
rule",...)
NAMED_POP_UP_LIST($name, [@list]) # list consists of a list of values (and each
tag will be set to the corresponding value)

(Name is the name of the variable, value is the value given to the variable when this option is selected, and label is the text printed next to the button or check box. Check box variables can have multiple values.)

NAMED_ANS_RADIO_BUTTONS creates a sequence of NAMED_ANS_RADIO and NAMED_ANS_RADIO_EXTENSION items which are output either as an array or, in scalar context, as the array glued together with spaces. It is usually easier to use this than to manually construct the radio buttons by hand. However, sometimes extra flexibility is desiredin which case:

When entering radio buttons using the "NAMED" format, you should use NAMED_ANS_RADIO button for the first button and then use NAMED_ANS_RADIO_EXTENSION for the remaining buttons. NAMED_ANS_RADIO requires a matching answer evalutor, while NAMED_ANS_RADIO_EXTENSION does not. The name used for NAMED_ANS_RADIO_EXTENSION should match the name used for NAMED_ANS_RADIO (and the associated answer evaluator).

The following method is defined in PG.pl for entering the answer evaluators corresponding to answer rules with automatically generated names. The answer evaluators are matched with the answer rules in the order in which they appear on the page.

    NAMED_ANS(name1 => ans_evaluator1, name2 => ans_evaluator2,...);

These auxiliary macros are defined in PG.pl

    NEW_ANS_NAME( number ); # produces a new answer blank name from a number by adding 
a prefix (AnSwEr)
# and registers this name as an implicitly labeled answer
# Its use is paired with each answer evaluator being entered
using ANS()
    ANS_NUM_TO_NAME(number);    # adds the prefix (AnSwEr) to the number, but does 
nothing else.
    RECORD_ANS_NAME( name );    # records the order in which the answer blank   is 
rendered
# This is called by all of the constructs above, but must
# be called explicitly if an input blank is constructed explictly
# using HTML code.

These are legacy macros:

    ANS_RULE( number, width );                      # equivalent to NAMED_ANS_RULE( 
NEW_ANS_NAME(number), width)
ANS_BOX( question_number,height, width ); # equivalent to NAMED_ANS_BOX(
NEW_ANS_NAME(number), height,
width)
ANS_RADIO( question_number, value,tag ); # equivalent to NAMED_ANS_RADIO(
NEW_ANS_NAME(number), value,tag)
ANS_RADIO_OPTION( question_number, value,tag ); # equivalent to NAMED_ANS_RADIO_EXTENSION(
ANS_NUM_TO_NAME(number),
value,tag)

answer_matrix
        Usage   \[ \{   answer_matrix(rows,columns,width_of_ans_rule, @options) \} 
\]
        Creates an array of answer blanks and passes it to display_matrix which returns
text which represents the matrix in TeX format used in math display mode.
Answers
are then passed back to whatever answer evaluators you write at the end of
the problem.
(note, if you have an m x n matrix, you will need mn answer evaluators, and
they will be
returned to the evaluaters starting in the top left hand corner and proceed
to the left
and then at the end moving down one row, just as you would read them.)
        The options are passed on to display_matrix.

Hints and solutions macros

    solution('text','text2',...);
SOLUTION('text','text2',...); # equivalent to TEXT(solution(...));
    hint('text', 'text2', ...);
HINT('text', 'text2',...); # equivalent to TEXT("$BR$HINT" . hint(@_) . "$BR")
if hint(@_);

Solution prints its concatenated input when the check box named 'ShowSol' is set and the time is after the answer date. The check box 'ShowSol' is visible only after the answer date or when the problem is viewed by a professor.

$main::envir{'displaySolutionsQ'} is set to 1 when a solution is to be displayed.

Hints are shown only after the number of attempts is greater than $:showHint ($main::showHint defaults to 1) and the check box named 'ShowHint' is set. The check box 'ShowHint' is visible only after the number of attempts is greater than $main::showHint.

$main::envir{'displayHintsQ'} is set to 1 when a hint is to be displayed.

Comments to instructors

    COMMENT('text','text2',...);

Takes the text to be lines of a comment to be shown only in the Library Browser below the rendered problem.

The function COMMENT stores the needed html in the variable pgComment, which gets transfered to the flag 'comment' in PG_FLAGS.

Pseudo-random number generator

    Usage:
random(0,5,.1) # produces a random number between 0 and 5 in increments
of .1
non_zero_random(0,5,.1) # gives a non-zero random number
    list_random(2,3,5,6,7,8,10) # produces random value from the list
list_random(2,3, (5..8),10) # does the same thing
    SRAND(seed) # resets the main random generator -- use very cautiously

SRAND(time) will create a different problem everytime it is called. This makes it difficult to check the answers :-).

SRAND($envir->{'inputs_ref'}->{'key'} ) will create a different problem for each login session. This is probably what is desired.

Display Macros

These macros produce different output depending on the display mode being used to show the problem on the screen, or whether the problem is being converted to TeX to produce a hard copy output.

    MODES   ( TeX =>    "Output this in TeX mode",
HTML => "output this in HTML mode",
HTML_tth => "output this in HTML_tth mode",
HTML_dpng => "output this in HTML_dpng mode",
Latex2HTML => "output this in Latex2HTML mode",
)
    TEX (tex_version, html_version) #obsolete
    M3  (tex_version, latex2html_version, html_version) #obsolete

Display constants

    @ALPHABET       ALPHABET()          capital letter alphabet -- ALPHABET[0] = 
'A'
$PAR PAR() paragraph character (\par or <p>)
$BR BR() line break character
$LQ LQ() left double quote
$RQ RQ() right double quote
$BM BM() begin math
$EM EM() end math
$BDM BDM() begin display math
$EDM EDM() end display math
$LTS LTS() strictly less than
$GTS GTS() strictly greater than
$LTE LTE() less than or equal
$GTE GTE() greater than or equal
$BEGIN_ONE_COLUMN BEGIN_ONE_COLUMN() begin one-column mode
$END_ONE_COLUMN END_ONE_COLUMN() end one-column mode
$SOL SOLUTION_HEADING() solution headline
$SOLUTION SOLUTION_HEADING() solution headline
$HINT HINT_HEADING() hint headline
$US US() underscore character
$SPACE SPACE() space character (tex and latex only)
$BBOLD BBOLD() begin bold typeface
$EBOLD EBOLD() end bold typeface
$BITALIC BITALIC() begin italic typeface
$EITALIC EITALIC() end italic typeface
$BCENTER BCENTER() begin centered environment
$ECENTER ECENTER() end centered environment
$HR HR() horizontal rule
$LBRACE LBRACE() left brace
$LB LB () left brace
$RBRACE RBRACE() right brace
$RB RB () right brace
$DOLLAR DOLLAR() a dollar sign
$PERCENT PERCENT() a percent sign
$CARET CARET() a caret sign
$PI PI() the number pi
$E E() the number e

TEXT macros

    Usage:
TEXT(@text);

This is the simplest way to print text from a problem. The strings in the array @text are concatenated with spaces between them and printed out in the text of the problem. The text is not processed in any other way. TEXT is defined in PG.pl.

    Usage:
BEGIN_TEXT
text.....
END_TEXT

This is the most common way to enter text into the problem. All of the text between BEGIN_TEXT and END_TEXT is processed by the EV3 macro described below and then printed using the TEXT command. The two key words must appear on lines by themselves. The preprocessing that makes this construction work is done in PGtranslator.pm. See EV3 below for details on the processing.

Evaluation macros

EV3

    TEXT(EV3("This is a formulat \( \int_0^5 x^2 \, dx \) ");
TEXT(EV3(@text));
        TEXT(EV3(<<'END_TEXT'));
text stuff...
END_TEXT

The BEGIN_TEXT/END_TEXT construction is translated into the construction above by PGtranslator.pm. END_TEXT must appear on a line by itself and be left justified. (The << construction is known as a "here document" in UNIX and in PERL.)

The single quotes around END_TEXT mean that no automatic interpolation of variables takes place in the text. Using EV3 with strings which have been evaluated by double quotes may lead to unexpected results.

The evaluation macro E3 first evaluates perl code inside the braces: \{ code \}. Any perl statment can be put inside the braces. The result of the evaluation (i.e. the last statement evaluated) replaces the \{ code \} construction.

Next interpolation of all variables (e.g. $var or @array ) is performed.

Then mathematical formulas in TeX are evaluated within the \( tex math mode \) and \[ tex display math mode \] constructions, in that order:

FEQ

    FEQ($string);   # processes and outputs the string

The mathematical formulas are run through the macro FEQ (Format EQuations) which performs several substitutions (see below). In HTML_tth mode the resulting code is processed by tth to obtain an HTML version of the formula. (In the future processing by WebEQ may be added here as another option.) The Latex2HTML mode does nothing at this stage; it creates the entire problem before running it through TeX and creating the GIF images of the equations.

The resulting string is output (and usually fed into TEXT to be printed in the problem).

    Usage:
        $string2 = FEQ($string1);

This is a filter which is used to format equations by EV2 and EV3, but can also be used on its own. It is best understood with an example.

        $string1 = "${a}x^2 + ${b}x + {$c:%.1f}"; $a = 3;, $b = -2; $c = -7.345;

when interpolated becomes:

        $string1 = '3x^2 + -2x + {-7.345:%0.1f}

FEQ first changes the number of decimal places displayed, so that the last term becomes -7.3 Then it removes the extraneous plus and minus signs, so that the final result is what you want:

        $string2 = '3x^2 - 2x -7.3';

(The %0.1f construction is the same formatting convention used by Perl and nearly identical to the one used by the C printf statement. Some common usage: %0.3f 3 decimal places, fixed notation; %0.3e 3 significant figures exponential notation; %0.3g uses either fixed or exponential notation depending on the size of the number.)

Two additional legacy formatting constructions are also supported:

?{$c:%0.3f} will give a number with 3 decimal places and a negative sign if the number is negative, no sign if the number is positive.

!{$c:%0.3f} determines the sign and prints it whether the number is positive or negative.

EV2

        TEXT(EV2(@text));
        TEXT(EV2(<<END_OF_TEXT));
text stuff...
END_OF_TEXT

This is a precursor to EV3. In this case the constants are interpolated first, before the evaluation of the \{ ...code...\} construct. This can lead to unexpected results. For example \{ join(" ", @text) \} with @text = ("Hello","World"); becomes, after interpolation, \{ join(" ",Hello World) \} which then causes an error when evaluated because Hello is a bare word. EV2 can still be useful if you allow for this, and in particular it works on double quoted strings, which lead to unexpected results with EV3. Using single quoted strings with EV2 may lead to unexpected results.

The unexpected results have to do with the number of times backslashed constructions have to be escaped. It is quite messy. For more details get a good Perl book and then read the code. :-)

Formatting macros

    beginproblem()  # generates text listing number and the point value of
# the problem. It will also print the file name containing
# the problem for users listed in the PRINT_FILE_NAMES_FOR PG_environment
# variable.
OL(@array) # formats the array as an Ordered List ( <OL> </OL> ) enumerated
by letters.
    htmlLink($url, $text)
# Places a reference to the URL with the specified text in the problem.
# A common usage is \{ htmlLink(alias('prob1_help.html') \}, 'for help')
# where alias finds the full address of the prob1_help.html file in the
same directory
# as the problem file
appletLink($url, $parameters)
# For example
# appletLink(q! archive="http: //webwork.math.rochester.edu/gage/xFunctions/xFunctions.zip"
code="xFunctionsLauncher.class" width=100 height=14!,
" parameter text goes here")
# will link to xFunctions.
    low level:
    spf($number, $format)   # prints the number with the given format
sspf($number, $format) # prints the number with the given format, always including
a sign.
nicestring($coefficients, $terms) # print a linear combinations of terms using
coefficients
nicestring($coefficients) # uses the coefficients to make a polynomial
# For example
# nicestring([1,-2, 0]) produces 'x^2-2x'
# nicestring([2,0,-1],[", 't', 't^2']) produces '2-t^2'
protect_underbar($string) # protects the underbar (class_name) in strings which
may have to pass through TeX.

Sorting and other list macros

    Usage:
lex_sort(@list); # outputs list in lexigraphic (alphabetical) order
num_sort(@list); # outputs list in numerical order
uniq( @list); # outputs a list with no duplicates. Order is unspecified.
    PGsort( \&sort_subroutine, @list);
# &sort_subroutine defines order. It's output must be 1 or 0 (true or false)

Macros for handling tables

    Usage:
begintable( number_of_columns_in_table)
row(@dataelements)
endtable()

Example of useage:

    BEGIN_TEXT
This problem tests calculating new functions from old ones:$BR
From the table below calculate the quantities asked for:$BR
\{begintable(scalar(@firstrow)+1)\}
\{row(" \(x\) ",@firstrow)\}
\{row(" \(f(x)\) ", @secondrow)\}
\{row(" \(g(x)\) ", @thirdrow)\}
\{row(" \(f'(x)\) ", @fourthrow)\}
\{row(" \(g'(x)\) ", @fifthrow)\}
\{endtable()\}
        (The arrays contain numbers which are placed in the table.)
    END_TEXT

Macros for displaying static images

    Usage:
$string = image($image, width => 100, height => 100, tex_size => 800)
$string = image($image, width => 100, height => 100, extra_html_tags => 'align="middle"',
tex_size => 800)
$string = image([$image1, $image2], width => 100, height => 100, tex_size =>
800)
$string = caption($string);
$string = imageRow([$image1, $image2 ], [$caption1, $caption2]);
# produces a complete table with rows of pictures.
File path = /ww/webwork/pg/macros/PGbasicmacros.pl

<| Post or View Comments |>


userDavid Etlinger - Re: PGbasicmacros.pl  blueArrow
6/9/2000; 11:44:49 AM (reads: 4726, responses: 1)
The $BEGIN_ONE_COLUM and $END_ONE_COLUMN constants are not described in the pod docs, and END_ONE_COLUMN is misspelled. Also, could they be lined up with the other constants? It would just look so much nicer!

<| Post or View Comments |>


userMichael Gage - Re: PGbasicmacros.pl  blueArrow
6/9/2000; 3:00:14 PM (reads: 5469, responses: 0)
OK. How do you think it looks now? Some people are using small screens so I don't want to assume a really wide screen and make the columns wider apart than they are now.

<| Post or View Comments |>


userDavid Etlinger - Re: PGbasicmacros.pl  blueArrow
6/12/2000; 2:55:14 PM (reads: 4646, responses: 1)
The table macros need much more documentation than is provided here, and also a working example. It appears that tables only work within a TEXT() block, and not within EV2 or EV3. See, in course WW_Prob_Lib1, setDerivatives1, problem c1s5p7.pg for a working example.

(We'll need to modify the table macros to work in EV3)

<| Post or View Comments |>


userMichael Gage - Re: PGbasicmacros.pl -fixed in gage_system  blueArrow
6/13/2000; 9:13:20 AM (reads: 5406, responses: 0)
I agree about the documentation. The table macros do work with EV3 -- they would probably give surprising results with EV2 because of premature evaluation of arguments. here is a fragment.

 

BEGIN_TEXT
This problem tests calculating new functions from old ones:$BR
From the table below calculate the quantities asked for:$BR
\{begintable(scalar(@firstrow)+1)\}
\{row(" ${BM}x${EM} ",@firstrow)\}
\{row(" ${BM}f(x)${EM} ", @secondrow)\}
\{row(" ${BM}g(x)${EM} ", @thirdrow)\}
\{row(" ${BM}f'(x)${EM} ", @fourthrow)\}
\{row(" ${BM}g'(x)${EM} ", @fifthrow)\}
\{endtable()\}



END_TEXT

It would be best to replace the ${BM}, ${EM} with \(, \) to get the full effect of the EV3 evaluation. (e.g. the tth typesetting)

<| Post or View Comments |>


userDavid Etlinger - Re: PGbasicmacros.pl  blueArrow
6/13/2000; 10:44:54 AM (reads: 4713, responses: 2)
the documentation for ans_radio_buttons should note that ans_radio_buttons( value1 => label1, value2 => label2 ... ) is also a valid (and probably more readable) format.

<| Post or View Comments |>


userMichael Gage - Re: PGbasicmacros.pl-fixed in gage_system  blueArrow
6/16/2000; 11:10:39 AM (reads: 5400, responses: 0)
Fixed in gage_system. Thanks.

<| Post or View Comments |>