WeBWorK Problems

Syntax for Data table usage in column displays?

Syntax for Data table usage in column displays?

by tim Payer -
Number of replies: 4
Greetings,

I am trying to display a data table in column format within a homework problem. I would like to have text on the left and a data table displayed on the right. Unfortunately I am missing something in the display of the data table.

Could you let me know where I have gone wrong?

Many thanks, Tim

The error messages are as follows:
  • Use of uninitialized value within @alignmentcolumns in numeric ne (!=) at line 369 of [PG]/macros/niceTables.pl
  • Use of uninitialized value $footerline in string ne at line 588 of [PG]/macros/niceTables.pl

The Code for the problem is below:

# DESCRIPTION   Normal probability and Continuity Correction
# Find the associated probability given mu and sigma.
# Use correct notation.
# WeBWorK problem written by TimPayer <tsp1@humboldt.edu>
# ENDDESCRIPTION

## DBsubject(Probability)
## DBchapter(Random variables)
## DBsection(Expectation)
## Institution(Humboldt State University)
## Author(Tim Payer)
## KEYWORDS(probability, translate, notation)
DOCUMENT();

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

install_weighted_grader();

#Text(beginproblem());   #uncomment

install_problem_grader(~~&std_problem_grader);
$showPartialCorrectAnswers = 1;

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


## Problem 9.4 ##
$L = random(20, 40, 1);
$N = list_random(20, 25, 40, 50, 100, 125);

if($N == 20 || $N == 25)
{
$Lo =random(2.5, 3.5, 0.5);
$Hi =random(4, 5.5, 0.5);
} elsif($N == 40)
{
$Lo =random(1.5, 3.0, 0.5);
$Hi =random(4, 5.5, 0.5);
} elsif( $N == 50)
{
$Lo =random(1, 2.5, 0.5);
$Hi =random(3.5, 5.5, 0.5);
} else {
$Lo =0.5;
$Hi =random(2.5, 5.5, 0.5);
}



## Problem 9.4a ##
$popup1 = PopUp(
["Choose:",  $Lo - 0.5, $Lo, $Lo + 0.5, ($Lo - 0.5)/10, $Lo/10, ($Lo + 0.5)/10,($Lo - 0.5)/100, $Lo/100, ($Lo + 0.5)/100 ], $Lo/100);
$popup2 = PopUp(
["Choose:",  $Hi - 0.5, $Hi, $Hi + 0.5, ($Hi - 0.5)/10, $Hi/10, ($Hi + 0.5)/10,($Hi - 0.5)/100, $Hi/100, ($Hi + 0.5)/100 ], $Hi/100);
$popup3 = PopUp(
["Choose:",  $Lo - 0.5, $Lo, $Lo - 0.05, $Lo/100 - 0.5/100, $Lo/100, $Lo/100 - 0.5/$N, ($Lo - 0.05)/100, ($Lo - 0.05)/$N, ($Lo - 0.5)/$N ], $Lo/100 - 0.5/$N);
$popup4 = PopUp(
["Choose:",  $Hi + 0.5, $Hi, $Hi + 0.05, $Hi/100 + 0.5/100, $Hi/100, $Hi/100 + 0.5/$N, ($Hi + 0.05)/100, ($Hi + 0.05)/$N, ($Hi + 0.5)/$N ], $Hi/100 + 0.5/$N);

$zlo = Compute("($Lo/100 - 0.5/$N-1/$L)/sqrt((1/$L)*(1-1/$L)/$N)");
$zlo2 =sprintf("%0.2f",$zlo);
$zl2 = Compute("$zlo2");
$zhi = Compute("($Hi/100 + 0.5/$N-1/$L)/sqrt((1/$L)*(1-1/$L)/$N)");
$zhi2 =sprintf("%0.2f",$zhi);
$zh2 = Compute("$zhi2");
$p = normal_prob($zl2, $zh2,  mean=>0, deviation=>1);


####  Begin Problem...


$column1 = PGML::Format(<<END_PGML);

 
   *Drawn from Lecture Notes: Week 5 Day 2.*  

*9.4)*   The fiddler crab gets its name for the having a single over-sized claw. The male fiddler crab uses its over-sized claw to fight other males for territory and to attract females. 
END_PGML


$column2 ="Italic text: $BITALIC Uca vocans vomeris $EITALIC ".DataTable(
[
[['p-values from R:  ', ], 'Scientific Notation:','Scientific Notation for Webwork:'],
[[' p = 0.8576' ],' \(8.58 \times 10^{-1}\) ',' 8.58E-1 '],
[[' p = 0.004237' ],'  \(4.24 \times 10^{-3}\)', '4.24E-3  '],
[[' p = 7.526e-04' ],'  \(7.53 \times 10^{-4}\)', '7.53E-4  '],
], 
caption => ' Note! Express all p-values in scientific notation. Use a capital "E" with three significant digits.',
midrules => 1,
align => '|p{3.5in}|p{2in}|p{2}|',
);





TEXT(ColumnTable($column1,$column2));


 
BEGIN_PGML 
  In most species of fiddler crabs the proportions of "right-clawed" and "left-clawed" individuals are roughly equal. But in one particular species, _Uca vocans vomeris_, the left-clawed individuals are strongly outnumbered. In this species the incidence of left clawed fiddler crabs ranges between about 1% to 4%. Patricia R.Y. Backwell of the Australian National University in Canberra and her colleagues studied the fighting ability of these left-clawed crabs. Their findings show that the lefties are less likely to fight and more likely to lose when they did fight. Blackwell et al were puzzled as to why the small proportion of lefties among _U.vomeris_ persists if there appears to be no evolutionary advantage. They suggest further study to answer the question as to what advantage the species would have in maintaining a small population of left clawed individuals that are poor fighters.  
 
Source: Henry Fountain, science writer, New York Times, Section F3: "In a Righty World, Left Clawed Crabs are at a Loss", September 4, 2007.
 
*9.4a)*  Given that one out of every [$L] crabs of a population of _Uca vocans vomeris_ are left clawed, what is the probability that a random sample of [$N] _U.vomeris_ will have between [$Lo]% and [$Hi]% (inclusive) left clawed crabs?  

   Let  [``\Large{\hat p}``] = The sample proportion of left clawed crabs from a random draw of [$N] _U.vomeris_.  

Direct translation:  
 
[``\Large{P(} ``] [$popup1->menu]* [``\Large{\le  \hat p \le}``] [$popup2->menu]* [``\Large{ ) \, \approx }``]
 
Continuity correction if needed:  
 
[``\Large{P(} ``] [$popup3->menu]* [``\Large{\le  \hat p \le}``] [$popup4->menu]* [``\Large{ ) \, \approx }``]

Complete the notation translation using the closest second decimal Z-score approximation with a fourth decimal probability answer:  
 
[``\Large{P(} ``]  [_____][``\Large{\le  Z \le}``] [_____] [``\Large{) \, \approx }``] [_____]
 

END_PGML

#Adapted weighted answers values:  

## Problem 9.4a ##
WEIGHTED_ANS( ($popup1)->cmp, 2 );
WEIGHTED_ANS( ($popup2)->cmp, 2 );
WEIGHTED_ANS( ($popup3)->cmp, 8 );
WEIGHTED_ANS( ($popup4)->cmp, 8 );
WEIGHTED_ANS( ($zl2)->cmp, 35 );
WEIGHTED_ANS( ($zh2)->cmp, 35 );
WEIGHTED_ANS( ($p)->cmp, 10 );



BEGIN_PGML_SOLUTION


The correct answers are coming....in 2017, Hah!



END_PGML_SOLUTION

ENDDOCUMENT();              

In reply to tim Payer

Re: Syntax for Data table usage in column displays?

by Alex Jordan -
As best as I can tell, Perl is being more picky about comparing uninitialized strings in the setup block of the problem than it would be if you were using DataTable in a TEXT block, and niceTables should be coded better to prevent these comparisons with uninitialized variables.

There are two such errors here. One is a flagrant issue with the niceTables code, where $footerline is being used on line 586, but it appears nowhere else in the code. It is probably an artifact of some earlier attempt to allow for a footerline=>yes option or something like that.

The other comparison error comes from (I have to assume) a bug with the mechanism that identifies which columns have which type of alignment. That was a complicated thing when I wrote it, because it had to allow for cells with colspan. But I feel more comfortable with Perl now and I could probably rewrite this more cleanly.

If you remove the align specification from your table, the second error goes away. And I would recommend doing this anyway, because the 3.5in+2in+2in is going to be too wide in hardcopy. Perhaps you would like the text in the 3rd columns column header to wrap. One way to achieve that is to force a line break by using 'Scientific Notation.$BR.'for Webwork:' there.

But the first error has no workaround, since it comes down to $footerline not being defined anywhere. I'm sorry, but you'll either have to wait until I can get around to addressing this, or you could locally remove the mention of $footerline from your niceTables.pl. (Or make a myniceTables.pl and remove it from there.) Or compromise and not use the columns. The errors don't lead to any error messages if the DataTable is just used in TEXT.
In reply to Alex Jordan

Re: Syntax for Data table usage in column displays?

by tim Payer -
Thanks for the quick Reply Alex.

And yes, I would like to pursue the use of data tables in the column format.
The layout is very clean, with the look of a text book where the table's contents are described at length on the left with the table contents on the right.

1.) That said I did not know that we could edit Macros.
I would attempt this edit but how would I start? Where are these macros kept?

2.) Supposing that I manage to make this edit on the "niceTables.pl" work. I still see a problem with removing the align specifications.
For while it removes the second error, but then all vertical bars of the table's cells are removed with it. And then it is no longer a "Table", rather it is a series of horizontal lines, yes?

3.)  Do you think that an array or some other table format would display without an error in the column format?

4.)  I can wait, but I would really like to use this format in my homework sets. Would you mind if I prompted you with a reminder every two or three months about the prospect of making data tables and column formats work together?

Thanks for all the work and consideration,

Sincerely, Tim Payer
In reply to tim Payer

Re: Syntax for Data table usage in column displays?

by Alex Jordan -
Hi Tim,

> 1.) That said I did not know that we could edit Macros.I would attempt this edit but how would I start? Where are these macros kept?

Before I answer, I'd like to warn about some consequences of doing this. If your problem ends up working, but only because it is using a macro that you fixed locally on your end, then you will not easily be able to share that problem with others. It will only work in your course on your server.

Also, in the meantime there may be fixes and enhancements to the original macro file, and yours won't benefit from that.

But perhaps the worst thing is that someone might contribute overall improvements to WeBWorK in a future version, doing so in a way that respects or simultaneously upgrades the distribution's macro file. But that may break your local version of that macro file, and you'll be unhappy you upgraded to 2.14 or whatever until you sort it all out.

All these things have bitten me in the past, so I want to give fair warning.

Now, in a course's templates folder (form the File Manager) there is a macros folder. You can put a copy of niceTables.pl there (https://github.com/openwebwork/pg/blob/master/macros/niceTables.pl) then edit it as you like. When the problem loads niceTables.pl it will load this copy that is local to your course instead of the server's copy.

You could also edit the server's copy if you want this to affect all your servers' courses. To do that you need access to the server, and you need to go into opt/pg/macros and edit the file there.

Fixing the alignment error message you are seeing might be too much to try given how convoluted the surrounding code is to anyone not familiar with it. But you might be able to rewire the comparisons to only happen if both sides are defined. Fixing the footer error message is probably just a matter of deleting the comparison that uses that footer variable from the boolean expression it's part of.

>2.) Supposing that I manage to make this edit on the "niceTables.pl" work. I still see a problem with removing the align specifications. For while it removes the second error, but then all vertical bars of the table's cells are removed with it. And then it is no longer a "Table", rather it is a series of horizontal lines, yes?

If you want vertical bars throughout the table, then yes you have to use an align specification. I'd argue that tables are just fine without vertical bars. But that is a somewhat famously contentious point. To get the vertical lines with no error message, you'll have to have niceTables.pl fixed.

> 3.) Do you think that an array or some other table format would display without an error in the column format?

I can't suggest a macro that would help. But here is another option to this whole issue. You could just not use macros at all. If you use:

BEGIN_TEXT
\{ MODES ( TeX => "Output this in TeX mode",
HTML => "output this in HTML mode",
)
\}
END_TEXT


you could just build tex and HTML directly, if you are familiar enough with these things. The HTML should be one long string of HTML that has two divs (one for the text section, one for the table) with exactly what you want inside each HTML element. And you'd use inline CSS to get the divs next to each other, to get vertical bars, etc. The TeX would be one long string as well. If your hard copy theme is two-column, I think you would want to *not* layout the tex to also be in two column, or there just won't be enough width for the table.

> 4.) I can wait, but I would really like to use this format in my homework sets. Would you mind if I prompted you with a reminder every two or three months about the prospect of making data tables and column formats work together?

I don't mind that, as long as you don't mind if it still takes me a while. I'm involved with some open textbook writing projects right now that take up all of my time. I'll take responsibility for niceTables.pl, but sometimes for me these things have to wait until summer.

By the way, if you go to work on a local version of niceTables.pl that you think fixes the issue without breaking anything else, I'd be willing to help evaluate it and help you submit the correction as a pull request.

In reply to tim Payer

Re: Syntax for Data table usage in column displays?

by Alex Jordan -
I just made a pull request with a patch to niceTables.pl that makes these two errors go away.

https://github.com/openwebwork/pg/pull/287

Monitor that PR. Once it is merged into master then you can run `git pull origin master` from your pg folder on your server and you should be OK.

If other niceTables.pl issues arise, please let me know.