Difference between revisions of "Tables"
Line 166: | Line 166: | ||
<p> |
<p> |
||
Notice that the commands provided are case-sensitive (e.g., use <code>BeginTable</code>, not <code>begintable</code>). Because these are defined perl functions, we need to use the <code>\{ \}</code> constructs with curly braces to execute them in the text section of the PG file. |
Notice that the commands provided are case-sensitive (e.g., use <code>BeginTable</code>, not <code>begintable</code>). Because these are defined perl functions, we need to use the <code>\{ \}</code> constructs with curly braces to execute them in the text section of the PG file. |
||
+ | Noe that periods are used at the end of each command (i.e., at the end of every line) except for the last command, since nothing will be joined to it. |
||
</p> |
</p> |
||
<p> |
<p> |
||
Line 172: | Line 173: | ||
<p> |
<p> |
||
The <code>AlignedRow()</code> and <code>Row()</code> commands both take several arguments that allow further customization of how a table is displayed. The first argument is always a square bracketed list of entries in that row. Putting quotes around each entry is advised to prevent unexpected behavior (for example, try using E instead of "E"). For column separation, <code>separation=>30</code> is the default, which is useful for a table of images, but less useful for a table of text. To insert a blank column on the left, use <code>indent=>n</code> for some number n (the default is n=0). The alignment of the initial column can be controlled by <code>align => LEFT</code> where the type is LEFT, RIGHT, or CENTER. The vertical alignment of a row is <code>valign => MIDDLE</code> where the type is TOP, MIDDLE, or BOTTOM. |
The <code>AlignedRow()</code> and <code>Row()</code> commands both take several arguments that allow further customization of how a table is displayed. The first argument is always a square bracketed list of entries in that row. Putting quotes around each entry is advised to prevent unexpected behavior (for example, try using E instead of "E"). For column separation, <code>separation=>30</code> is the default, which is useful for a table of images, but less useful for a table of text. To insert a blank column on the left, use <code>indent=>n</code> for some number n (the default is n=0). The alignment of the initial column can be controlled by <code>align => LEFT</code> where the type is LEFT, RIGHT, or CENTER. The vertical alignment of a row is <code>valign => MIDDLE</code> where the type is TOP, MIDDLE, or BOTTOM. |
||
− | </p> |
||
− | <p> |
||
− | Notice that periods are used at the end of each command (i.e., at the end of every line) except for the last command, since nothing will be joined to it. |
||
</p> |
</p> |
||
</td> |
</td> |
Revision as of 19:01, 11 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.
- Standard tables embedded in the main text section
- Standard tables defined in the setup section and displayed in the main text section
- 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 |
The
Also note that, perhaps obviously, the |
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") ); } |
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. |
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 |
Then we put the table into the text section. |
foreach my $fv ( @fvals ) { ANS( $fv->cmp() ); } |
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("unionTables.pl"); BEGIN_TEXT \{ BeginTable(border=>1, tex_border=>"1pt", spacing=>0, padding=>4). AlignedRow(["\(x = \)","0","1","2"],separation=>0). AlignedRow(["\(f(x)=\)","A","B","C"],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 |