## 2017 Problem Authoring Workshop

### Formatting Data Tables in PGML?

by tim Payer -
Number of replies: 10
Greetings!

I am trying to make the transition for Data Tables from PG to PGML.

I understand that while in PG I must write:

\{
DataTable( table contents...);
\}

While in PGML this must be changed to:

[@
DataTable( table contents...);
@]*

However it seems that by doing so that I have lost some formatting options within the Data Table. For instance bold type, and line breaks wont display using either PG or PGML syntax.
Is there a different syntax for bold type and line breaks within data table cells that are built in PGML?

I have attached an example that shows two identical data tables. The first is in PGML the second is in PG. How can I give the same formatting in the PGML table. Thanks for the help!

Tim

A code block showing this example is below....

##DESCRIPTION
## Sign Analysis on a Rational Function

##KEYWORDS('sign analysis')

## DBsubject('Precalculus')
## DBchapter('Rational equations and functions')
## DBsection('Asymptotes')
## Date('6/7/2017')
## Author('Tim Payer')
# WeBWorK problem written by TimPayer <tsp1@humboldt.edu>
# ENDDESCRIPTION

DOCUMENT();

"PGstandard.pl",
"PGunion.pl",
"PGnumericalmacros.pl",
"PGstatisticsmacros.pl",
"MathObjects.pl",
"parserPopUp.pl",
"PGML.pl",
"unionTables.pl",
"niceTables.pl",
"PGcourse.pl",
"PGchoicemacros.pl",
);

#Text(beginproblem()); #uncomment

$showPartialCorrectAnswers = 1; Context("Numeric"); Context()->flags->set( tolerance => 0.01, tolType => "absolute", ); ############ Start Problem HW 1.5 #################$a = non_zero_random(-7,7);
$b = non_zero_random(-7,7);$c = non_zero_random(-7,7);
$d = non_zero_random(-7,7);$va = Formula("(-$d)"); ## Vertical asymptote$sa = Formula("($a)"); ## Horizontal Asymptote$fp1 = Formula("($a*$c*$d-$b)/($c(x+$d)**2)"); # first derivative
$fp2 = Formula("2*($b-$a*$c*$d)/($c*(x+$d)**3)"); # second derivative if ($a*$d*$c*$c <$b*$c ) {$f1s = "dec";
$f2s = "dec";$d1s = "-";
$d2s = "-";$dd1s = "CD";
$dd2s = "CU"; } else {$f1s = "inc";
$f2s = "inc";$d1s = "+";
$d2s = "+";$dd1s = "CU";
$dd2s = "CD"; } ## Delete Below when done$cp1 = Compute("1");
$cp2 = Compute("1");$z1 = Compute("0");

### Row 2, f(x) is incresing or decreasing

$f1 = PopUp( ["?", 'inc', 'dec'],$f1s);
$f2 = PopUp( ["?", 'inc', 'dec'],$f2s);

### Row 3, f'(x) is positive or negative
$d1 = PopUp( ["?", '+', '-'],$d1s);
$d2 = PopUp( ["?", '+', '-'],$d2s);

### Row 4, f''(x) is positive with f(x) CU or f''(x) is negative with f(x) CD
$dd1 = PopUp( ["?", 'CU', 'CD'],$dd1s);

$dd2 = PopUp( ["?", 'CU', 'CD'],$dd2s);

#################### Begin Problem...#######################

BEGIN_PGML
*WeBWorK workshop 2017 HW 1.5)
A sign analysis for a rational function*

*HW 1.5)* Given the rational function [f(x) = \frac{[$a*$c]x +[$b]}{[$c]x+[$c*$d]}], perform a sign analysis for the function and the first and second derivatives.
Find all critical points and asymptotes associated with the function.

*1.5a)* Find the vertical asymptote: [x] = [_______]*

*1.5b)* Find the horizontal asymptote: [y] = [_______]*

*1.5c)* Find the first derivative: [f'(x)] = [_______]*

*1.5d)* Find the second derivative: [f''(x)] = [_______]*

[@
DataTable(
[
['*1.5e)* Complete the table below for a correct sign analysis. Use the key at the right to enter the correct values for the function. Determine the signs of the function, and its first and second derivative before and after the vertical asymptote.', '','inc = f(x) is increasing.
dec = f(x) is decreasing. + = f(x) is positive. - = f(x) is negative. CU = f^{\prime \prime}(x) > 0  and f(x) is concave up. CD = f^{\prime \prime}(x) < 0 and f(x) is concave down.' ],
],
caption => ' ',
midrules=>1,
align => '|p{3in} p{0.2in}|p{3in}|'
);
@]*

END_PGML
BEGIN_TEXT

\{
DataTable(
[
['$BBOLD 1.5e)$EBOLD Complete the table below for a correct sign analysis. Use the key at the right to enter the correct values for the function. Determine the signs of the function, and its first and second derivative before and after the vertical asymptote.', '','inc = $$f(x)$$ is increasing. $BR dec = $$f(x)$$ is decreasing.$BR + = $$f(x)$$ is positive. $BR - = $$f (x)$$ is negative.$BR CU = $$f^{\prime \prime}(x) > 0$$ and $$f(x)$$ is concave up. BR CD = $$f^{\prime \prime}(x) < 0$$ and $$f(x)$$ is concave down.' ], ], caption => ' ', midrules=>1, align => '|p{3in} p{0.2in}|p{3in}|' ); \}PAR

\{
DataTable(
[
['','Before the $BR vertical$BR asymptote:','Vertical $BR asymptote', 'After the$BR vertical $BR asymptote:' ], ['$$x =$$','',''.$va->ans_rule(2).'', '' ],
['$$f(x)$$',''.$f1->menu.'','$$\hspace {20pt} |$$',''.$f2->menu.'' ],
['$$f^\prime (x)$$',''.$d1->menu.'','$$\hspace {20pt} |$$',''.$d2->menu.'' ],
['$$f^{\prime \prime} (x)$$',''.$dd1->menu.'','$$\hspace {20pt} |$$', ''.$dd2->menu.'' ],
],
caption => ' ',
midrules=>1,
align => '|p{0.5in}|p{1in}| p{1in}|p{1in}|p{1in}|'
);
\}
$PAR END_TEXT #Adapted weighted answers values: ## Asymptotes and derivatives ## WEIGHTED_ANS( ($va)->cmp, 1 );
WEIGHTED_ANS( ($sa)->cmp, 10 ); WEIGHTED_ANS( ($fp1)->cmp, 10 );
WEIGHTED_ANS( ($fp2)->cmp, 15 ); ## HW 1.5e Sign Analysis Table: ## ###Row 1: WEIGHTED_ANS( ($va)->cmp, 2 );

###Row 2:
WEIGHTED_ANS( ($f1)->cmp, 1 ); WEIGHTED_ANS( ($f2)->cmp, 1 );

###Row 3:
WEIGHTED_ANS( ($d1)->cmp, 1 ); WEIGHTED_ANS( ($d2)->cmp, 1 );

###Row 4:
WEIGHTED_ANS( ($dd1)->cmp, 1 ); WEIGHTED_ANS( ($dd2)->cmp, 1 );

##########################################

BEGIN_PGML_SOLUTION

END_PGML_SOLUTION

ENDDOCUMENT();

### Re: Formatting Data Tables in PGML?

by Davide Cervone -
There are several things that are going on, here, most of them having to do with how BEGIN_TEXT/END_TEXT works. In particular, it has to do with the fact that BEGIN_TEXT/END_TEXT works by doing several passes over the text, one for each type of substitution that it makes. That means the order in which those are done is crucial. For BEGIN_TEXT/END_TEXT, all the commands (\{...\}) are substituted first, then variable substitutions ($...) are made, and finally LaTeX expressions ($$...$$ and $...$) are processed. Because the substitutions are performed serially, the results of earlier substitutions can be affected by the following substitutions. So if a command substitution, for example, produces output that includes dollar signs, those dollar signs will cause variable substitutions later. Similar, if the command output includes LaTeX math delimiters, they will be processed in the later phases. In your case, the text of your table is given within single quotations marks ('...'). In perl, there is a difference between strings quoted with single quotes and ones that use double quotes: those with double quotes have variable substitution performed on them, while single-quoted strings don't. That means that when you use  '$BBOLD 1.5e) $EBOLD...'  The values of $BBOLD and $EBOLD are not substituted into the string at this point. These are included verbatim in the body of the table. Because you are inserting the table into BEGIN_TEXT/END_TEXT via \{...\}, however, once that output is inserted into the text on the first pass, the second pass performs variable substitution, and the bold variables are inserted at that point. Similarly, the raw LaTeX becomes part of the table output and is inserted into the text output during the \{...\} phase, but the later LaTeX phase causes it to be interpreted as math. So your table works as expected in the BEGIN_TEXT/END_TEXT setting because of the way that BEGIN_TEXT/END_TEXT processes the output in several phases. On the other hand, PGML does not do multiple passes (it does not use regular expression substitutions, as BEGIN_TEXT/END_TEXT does). So the output from the DataTable() would be inserted into the PGML output with no further processing (that is, the dollar signs and the LaTeX source code would not be interpreted and would be included in the output literally). It turns out, however, that the empty cell is causing a problem, and the output is not being produced. If you remove the '' entry, or change it to ' ' with a space, then you will see the output of the table, but with dollar signs in tact. In order to get the output you want, you need to change the single quotes to double quotes so that the variable substitution is done when the strings are formed (i.e., before the DataTable() macro is called) so that they will already be substituted by the time the output is created. To handle the mathematics, you need to do two things: double the backslashes (since in double-quoted strings, the backslash is an escape character), and use three stars after the [@ ... @] command. The three stars cause PGML to perform the standard PG LaTeX processing. Because many PG macros (like DataTable() expect LaTeX to be processed in their output (as it would be in BEGIN_TEXT/END_TEXT), PGML has the triple-star option in order to do that processing. So the modified code that works for your table is the following: BEGIN_PGML [@ DataTable( [ [ "$BBOLD 1.5e) $EBOLD Complete the table below for a correct sign analysis. Use the key at the right to enter the correct values for the function. Determine the signs of the function, and its first and second derivative before and after the vertical asymptote.", "inc = \$$f(x)\$$ is increasing.$BR dec = \$$f(x)\$$ is
decreasing. $BR + = \$$f(x)\$$ is positive.$BR - = \$$f(x)\$$ is
negative. $BR CU = \$$f''(x) > 0\$$ and \$$f(x)\$$ is concave up.$BR CD = \$$f''(x) < 0\$$ and \$$f(x)\$$ is concave down."
],
],
caption => ' ',
midrules=>1,
align => '|p{3in}|p{3in}|'
)
@]***
END_PGML


### Re: Formatting Data Tables in PGML?

by tim Payer -
Wow! Thank you Davide for the detailed explanation!

Double back-slashes enclosed within double quotes and a triple asterisk to close the data table to be able to stay within PGML. I will share this with my fellow webwork user.

Sincerely, Tim

### Re: Formatting Data Tables in PGML?

by Andrew Dabrowski -
Thanks for that explanation. Is it also possible to use PGML style answer blanks inside the table?

### Re: Formatting Data Tables in PGML?

by tim Payer -
Yes! you can use both popups and numeric answer blanks within a table.

Here is and example problem. The images of sand crabs wont come through when you load this example but you can see how PGML answers are used within a PGML data table...

# DESCRIPTION
# Data type identification
# WeBWorK problem written by TimPayer <tsp1@humboldt.edu>
# ENDDESCRIPTION

## DBsubject(Statistics)
## DBchapter(Exploratory data analysis/descriptive statistics)
## DBsection(Summary statistics)
## Institution(Humboldt State University)
## Author(Tim Payer)
## KEYWORDS(summations, mean, sd)

DOCUMENT();
"PGstandard.pl",
"PGunion.pl",
"niceTables.pl",
"unionTables.pl",
"PGstatisticsmacros.pl",
"PGnumericalmacros.pl",
"MathObjects.pl",
"parserPopUp.pl",
"parserFormulaUpToConstant.pl",
"PGML.pl",
);

TEXT(beginproblem());
$showPartialCorrectAnswers = 1; Context("Numeric");$pop7 = PopUp(
["Choose:", "= X =", "< X >", "< X <","> X >", "> X <", "= Y =", "< Y >", "< Y <","> Y >", "> Y <","= Z =", "< Z >", "< Z <","> Z >", "> Z <"], "< X <");
$pop8 = PopUp( ["Choose:", "= X =", "< X >", "< X <","> X >", "> X <", "= Y =", "< Y >", "< Y <","> Y >", "> Y <","= Z =", "< Z >", "< Z <","> Z >", "> Z <"], "< Z <");$pop9 = PopUp(
["Choose:", "X =", "X >", "X <","Y =", "Y >","Y <","Z =", "Z >","Z <"], "Y =");

Context()->flags->set(
tolerance => 0.0001,
tolType => "absolute",
);

$m = random(20,35,1);$s =random(4,8,1);
$h =random(1,3,1);$l =random(1,3,1);
$hi =Compute("$m + $s+$h");
$lo =Compute("$m - $s-$l");
$zl =Compute("($lo - $m)/$s");
$zh =Compute("($hi - $m)/$s");

## Calculating the probability of a single success
## using Z-tables.

$zl4 = sprintf("%0.4f",$zl);
$zh4 = sprintf("%0.4f",$zh);
$ans7 =Compute("$zl4");
$ans8 =Compute("$zh4");
$zl2 =sprintf("%0.2f",$zl);
$zh2 =sprintf("%0.2f",$zh);
$ans9 =Compute("$zl2");
$ans10 =Compute("$zh2");
$pc = normal_prob($ans9, $ans10, mean=>0, deviation=>1);$ans11 =Compute("$pc");$crab = list_random('four', 'five', 'six', 'seven', 'eight');

#$crab ='eight'; if($crab eq four )
{
$pop10 = PopUp( ["Choose:", "4", "5", "6","7", "8"], "4");$ans12 = Compute("$ans11**4"); } elsif ($crab eq five)
{
$pop10 = PopUp( ["Choose:", "4", "5", "6","7", "8"], "5");$ans12 = Compute("$ans11**5"); } elsif ($crab eq six)
{
$pop10 = PopUp( ["Choose:", "4", "5", "6","7", "8"], "6");$ans12 = Compute("$ans11**6"); } elsif ($crab eq seven)
{
$pop10 = PopUp( ["Choose:", "4", "5", "6","7", "8"], "7");$ans12 = Compute("$ans11**7"); } else {$pop10 = PopUp(
["Choose:", "4", "5", "6","7", "8"], "8");
$ans12 = Compute("$ans11**8");
}

#### BEGIN PROBLEM DISPLAY
BEGIN_PGML
*Drawn From Lecture Notes: Week 4, Day 2 and Day 3.*

[@
DataTable(
[
[["$BBOLD 7.2)$EBOLD The Pacific sand crab, $BITALIC Emerita analoga,$EITALIC inhabits sandy shores from Baja California to Alaska and feeds via antennae filters that are periodically
retracted and scraped clean of small organisms into their waiting mouths. The female is nearly twice as large as the male sand crab. Suppose the length of the female Pacific sand crab
is normally distributed with a mean of \$$\\mu = \$$ $m mm and a standard deviation of \$$\\sigma =\$$$s mm. Find the probability of randomly drawing $crab female sand crabs that all have lengths between$lo and $hi mm.$BR $BR Use the following declared event variables within probability notation to answer the question with accuracy to the fourth decimal.$BR $BR \$$X \$$ = The length in mm for a randomly drawn female sand crab,$BITALIC Emerita analoga.$EITALIC$BR $BR \$$Y \$$ = The number of$crab randomly drawn female sand crabs that are between $lo and$hi mm in length. $BR$BR
Hint: Break this problem into two parts: $BR$BR
a.) Consider the probability of drawing one female sand crab between $lo and$hi mm. Use the closest second decimal Z-scores to determine this probability of a single success. $BR b.) Now consider the probability of drawing$crab successes in a row. $BR$BR
$BBOLD 7.2a)$EBOLD \$$\\quad\\large{P(lo}\$$ ".$pop7->menu." \$$\\large{hi) \\approx}\$$$BR \$$\\large{\\approx P(}\$$".$ans9->ans_rule(5).$pop8->menu.$ans10->ans_rule(5)." \$$\\large{) \\approx}\$$".$ans11->ans_rule(5)." $BR$BR $BBOLD 7.2b)$EBOLD \$$\\quad \\large{P(}\$$ ".$pop9->menu.$pop10->menu." \$$\\large{) \\approx}\$$
".$ans12->ans_rule(5)."$BR $BR$BR $BR$BR"], "The Pacific Sand Crab,$BR$BITALIC Emerita_analoga. $EITALIC ".image("Careers_in_Science_Interns.png", width=>500, height=>376, tex_size=>700, extra_html_tags=>'alt="A sand crab rests in the palm of a student."').$BR."image by Careers in Science Interns".$BR.image("A_Snails_Odyssey2.png", width=>379, height=>308, tex_size=> 700, extra_html_tags=>'alt="Four sand crabs poke through the sandy bottom of an aquarium."').$BR."image by A Snails Odyssey".$BR.image("A_Snails_Odyssey.png", width=>260, height=>224, tex_size=>700, extra_html_tags=>'alt="A diagram showing the order and direction that each appendage moves to enable the sand crab to burrow beneath the sand."').$BR."image by A Snails Odyssey"],
],
caption => "7.2) Calculating the Probability of Drawing a Female Sand Crab of a Predtermined Length: ",
midrules => 0,
align => "p{5.1in}p{2.9in}",
);
@]***

END_PGML

WEIGHTED_ANS( ($pop7)->cmp, 2 ); WEIGHTED_ANS( ($ans9)->cmp, 20 );
WEIGHTED_ANS( ($pop8)->cmp, 2 ); WEIGHTED_ANS( ($ans10)->cmp, 20 );
WEIGHTED_ANS( ($ans11)->cmp, 20 ); WEIGHTED_ANS( ($pop9)->cmp, 3 );
WEIGHTED_ANS( ($pop10)->cmp, 3 ); WEIGHTED_ANS( ($ans12)->cmp, 30 );

BEGIN_PGML_SOLUTION
*SOLUTION*

Solution coming in 2017...

END_PGML_SOLUTION

ENDDOCUMENT();

### Re: Formatting Data Tables in PGML?

by Andrew Dabrowski -
Thanks for that intricate example. It uses popups, but it doesn't seem to use PGML style blanks. I mean is it possible to somehow put

[____]{7}

into a table as an answer blank?

### Re: Formatting Data Tables in PGML?

by Davide Cervone -

Is it also possible to use PGML style answer blanks inside the table?

Yes, that is easily done. If you are using the WeBWorK 2.13 or later, you can use the PGML() command to process the contents of the entries in the data table, as in the example below:

loadMacros(
"PGstandard.pl",
"PGML.pl",
"niceTables.pl"
);

TEXT(
DataTable([
[PGML('[____]{1}'), PGML('[____]{2}')],
[PGML('[____]{3}'), PGML('[____]{4}')]
])
);

If you are using an earlier version of WeBWorK, than you can use
sub PGML {PGML::Format2(@_)}

to define the PGML() macro before you use it.

### Re: Formatting Data Tables in PGML?

by Andrew Dabrowski -
Thanks, I didn't know PGML was available as a command.

### Re: Formatting Data Tables in PGML?

by Davide Cervone -
It is not as well known as it should be, but I think the addition of the PGML() macro in PGML.pl in 2.13 should make it easier to use. That's easier than PGML::Format2().

### Re: Formatting Data Tables in PGML?

by tim Payer -
Thank you Davide!

That clarifies the answer blanks in Data tables, I had imposed the weighted grader and data tables together which in that case I think does require an altered entry for answer blanks

Tim
You could still do it by using plain PGML blanks with no answer and then using WEIGHTED_ANS() (or whatever the command is) to give them their answer checkers. But I'm not sure that helps all that much.