Difference between revisions of "Tables"
Line 45: | Line 45: | ||
<td style="background-color:#ffcccc;padding:7px;"> |
<td style="background-color:#ffcccc;padding:7px;"> |
||
<p> |
<p> |
||
+ | <b>Main Text:</b> |
||
The <code>begintable</code>, <code>row</code>, and <code>endtable</code> commands are all predefined in the PG language, so we just need to use them here. Because these are defined perl functions, we need to use the <code>\{ \}</code> constructs to execute them in the text section of the PG file. |
The <code>begintable</code>, <code>row</code>, and <code>endtable</code> commands are all predefined in the PG language, so we just need to use them here. Because these are defined perl functions, we need to use the <code>\{ \}</code> constructs to execute them in the text section of the PG file. |
||
</p> |
</p> |
||
Line 53: | Line 54: | ||
It is possible to use an array in a table row, and also to add more space around each entry in the table to make it look nicer. For example, we can add extra space manually using <code>$SPACE</code>, which must be inside double (not single) quotes. Obviously, if you're making computations using values in these arrays, add the extra <code>$SPACE</code> only after you're done making computations. |
It is possible to use an array in a table row, and also to add more space around each entry in the table to make it look nicer. For example, we can add extra space manually using <code>$SPACE</code>, which must be inside double (not single) quotes. Obviously, if you're making computations using values in these arrays, add the extra <code>$SPACE</code> only after you're done making computations. |
||
<pre> |
<pre> |
||
+ | # Setup |
||
@x = (0..2); # shorthand for (0,1,2) |
@x = (0..2); # shorthand for (0,1,2) |
||
@f = (3..5); |
@f = (3..5); |
||
Line 60: | Line 62: | ||
} |
} |
||
+ | # Main text |
||
BEGIN_TEXT |
BEGIN_TEXT |
||
Fill in the table of values for \(f(x) = x^2\). |
Fill in the table of values for \(f(x) = x^2\). |
||
Line 112: | Line 115: | ||
<td style="background-color:#ffffcc;padding:7px;"> |
<td style="background-color:#ffffcc;padding:7px;"> |
||
<p> |
<p> |
||
+ | <b>Setup:</b> |
||
In this example, we define the start of the table and the values in the table rows as variables that we can then use in the text section of the problem. We also define the function values at the points we're asking the student to compute. |
In this example, we define the start of the table and the values in the table rows as variables that we can then use in the text section of the problem. We also define the function values at the points we're asking the student to compute. |
||
</p> |
</p> |
||
Line 140: | Line 144: | ||
<td style="background-color:#ffcccc;padding:7px;"> |
<td style="background-color:#ffcccc;padding:7px;"> |
||
<p> |
<p> |
||
− | Then we put the table into the text section. |
||
+ | <b>Main Text:</b> |
||
+ | Then we put the table into the text section. If we had defined the table using only one element <code>$table</code>, we would insert it here in the same way as shown to the left. |
||
</p> |
</p> |
||
</td> |
</td> |
||
Line 153: | Line 158: | ||
<td style="background-color:#eeccff;padding:7px;"> |
<td style="background-color:#eeccff;padding:7px;"> |
||
<p> |
<p> |
||
+ | <b>Answer Evalution:</b> |
||
And, given a list of answers, we can automate the insertion of answer checkers. Of course, with only three of them it would be faster to just put them in a list: |
And, given a list of answers, we can automate the insertion of answer checkers. Of course, with only three of them it would be faster to just put them in a list: |
||
</p> |
</p> |
Revision as of 17:51, 18 February 2010
Topic Name: Using Tables
This code snippet shows the essential PG code to display tables of data (or answer blanks, etc.). We give three different examples of how to display tables. Note that these are insertions, not a complete PG file. This code will have to be incorporated into the problem file on which you are working.
You may also be interested in GraphsInTables
- Example 1: Standard tables embedded in the main text section
- Example 2: Standard tables defined in the setup section and displayed in the main text section
- Example 3: Union tables which offer more customization options
Example 1: The simplest way of putting a table in the problem is just to embed it in
the text of the problem using the begintable
, row
, and endtable
commands, as shown below. It is also possible to use these to define variables that are used in the text; this is shown in the second example, below.
PG problem file | Explanation |
---|---|
BEGIN_TEXT Fill in the table of values for \(f(x) = x^2\). $BCENTER \{ begintable(4) \} \{ row( "\(x = \)", "0", "1", "2" ) \} \{ row( "\(f(x) = \)", ans_rule(5), ans_rule(5), ans_rule(5) ) \} \{ endtable() \} $ECENTER END_TEXT |
Main Text:
The
Also note that, perhaps obviously, the
It is possible to use an array in a table row, and also to add more space around each entry in the table to make it look nicer. For example, we can add extra space manually using # Setup @x = (0..2); # shorthand for (0,1,2) @f = (3..5); foreach my $i (0..2) { $x[$i] = "$SPACE $x[$i] $SPACE"; $f[$i] = "$SPACE $f[$i] $SPACE"; } # Main text BEGIN_TEXT Fill in the table of values for \(f(x) = x^2\). $BCENTER \{ begintable(4) \} \{ row( "$SPACE \(x = \) $SPACE", @x ) \} \{ row( "$SPACE \(f(x) = \) $SPACE", @f ) \} \{ endtable() \} $ECENTER END_TEXT |
Example 2: We can also define as much of the table as we like in the problem set-up section of the problem file, as well, as suggested by the following example.
PG problem file | Explanation |
---|---|
# we'll work with the function f(x) = x^2 + a $a = random(2,5,1); # the table data $table_start = begintable(4); $table_row1 = row( "\(x =\)", "0", "1", "2" ); $table_row2 = row( "\(f(x)\) =", ans_rule(5), ans_rule(5), ans_rule(5) ); $table_end = endtable(); # these are the actual function values at the # points @fvals = (); foreach my $i ( 0, 1, 2 ) { push( @fvals, Compute("$i^2 + $a") ); } |
Setup: In this example, we define the start of the table and the values in the table rows as variables that we can then use in the text section of the problem. We also define the function values at the points we're asking the student to compute.
It is also possible to join together these elements into a single value $table = begintable(4) . row( "\(x =\)", "0", "1", "2" ) . row( "\(f(x)\) =",ans_rule(5),ans_rule(5),ans_rule(5) ) . endtable(); |
BEGIN_TEXT If \(f(x) = x^2 + $a\), fill in values of \(f(x)\) as indicated in the table below. $BCENTER $table_start $table_row1 $table_row2 $table_end $ECENTER |
Main Text:
Then we put the table into the text section. If we had defined the table using only one element |
foreach my $fv ( @fvals ) { ANS( $fv->cmp() ); } |
Answer Evalution: And, given a list of answers, we can automate the insertion of answer checkers. Of course, with only three of them it would be faster to just put them in a list: ANS( $fvals[0]->cmp() ); ANS( $fvals[1]->cmp() ); ANS( $fvals[2]->cmp() ); instead of using the loop. |
Example 3: We use the utilities provided by the unionTables.pl
macro. This macro provides more customization than the previous two examples.
PG problem file | Explanation |
---|---|
loadMacros( "PGstandard.pl", "unionTables.pl", # or "PGunion.pl", ); BEGIN_TEXT \{ BeginTable(border=>1, tex_border=>"1pt", spacing=>0, padding=>4). AlignedRow(["\(x = \)","0","1","2"], align=>LEFT, separation=>0). AlignedRow(["\(f(x)=\)","A","B","C"], separation=>0). TableSpace(25,6). AlignedRow(["\(g(x)=\)","E","F","G"], separation=>0). EndTable() \} END_TEXT |
In the initialization section of the PG file, be sure to load
Notice that the commands provided are case-sensitive (e.g., use
For
The
The |