Difference between revisions of "Tables"

From WeBWorK_wiki
Jump to navigation Jump to search
Line 2: Line 2:
   
 
<p style="background-color:#eeeeee;border:black solid 1px;padding:3px;">
 
<p style="background-color:#eeeeee;border:black solid 1px;padding:3px;">
<em>This code snippet shows the essential PG code to display tables of data (or answer blanks, etc.). We give three different ways to display tables. Note that these are <b>insertions</b>, not a complete PG file. This code will have to be incorporated into the problem file on which you are working.</em>
+
<em>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 <b>insertions</b>, not a complete PG file. This code will have to be incorporated into the problem file on which you are working.</em>
 
<ol>
 
<ol>
 
<li>Standard tables embedded in the main text section</li>
 
<li>Standard tables embedded in the main text section</li>
Line 15: Line 15:
   
 
<p style="background-color:#eeeeee;border:black solid 1px;padding:3px;">
 
<p style="background-color:#eeeeee;border:black solid 1px;padding:3px;">
<em><b>Method 1:</b> The simplest way of putting a table in the problem is just to embed it in
+
<em><b>Example 1:</b> The simplest way of putting a table in the problem is just to embed it in
 
the text of the problem using the <code>begintable</code>, <code>row</code>, and <code>endtable</code> 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.
 
the text of the problem using the <code>begintable</code>, <code>row</code>, and <code>endtable</code> 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.
 
</em>
 
</em>
Line 54: Line 54:
   
 
<p style="background-color:#eeeeee;border:black solid 1px;padding:3px;">
 
<p style="background-color:#eeeeee;border:black solid 1px;padding:3px;">
<em><b>Method 2:</b> 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.
+
<em><b>Example 2:</b> 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.
 
</em>
 
</em>
 
</p>
 
</p>
Line 132: Line 132:
 
</tr>
 
</tr>
 
</table>
 
</table>
  +
  +
  +
  +
<p style="background-color:#eeeeee;border:black solid 1px;padding:3px;">
  +
<em><b>Example 3:</b> We use the utilities provided by the <code>unionTables.pl</code> macro. This macro provides more customization than the previous two examples.
  +
</em>
  +
</p>
  +
  +
  +
<table cellspacing="0" cellpadding="2" border="0">
  +
<tr valign="top">
  +
<th> PG problem file </th>
  +
<th> Explanation </th>
  +
</tr>
  +
<tr valign="top">
  +
<td style="background-color:#ffdddd;border:black 1px dashed;">
  +
<pre>
  +
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
  +
</pre>
  +
<td style="background-color:#ffcccc;padding:7px;">
  +
<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.
  +
</p>
  +
<p>
  +
For <code>BeginTable()</code>, the argument <code>border=&gt;n</code> sets the border thickness in pixels in HTML mode, while <code>tex_border=&gt;1pt</code> sets the border thickness in TeX mode (for pdf hardcopies). Notice that the units <code>pt</code> must be included, where 1 point is 1/72 of an inch. The options <code>spacing</code> and <code>padding</code> are like the HTML commands <code>cellspacing</code> and <code>cellpadding</code>. The default is <code>center=>1</code> to center the table, but this could also be set to 0.
  +
</p>
  +
</td>
  +
</tr>
  +
</table>
  +
  +
   
 
<p style="text-align:center;">
 
<p style="text-align:center;">

Revision as of 17:46, 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.

  1. Standard tables embedded in the main text section
  2. Standard tables defined in the setup section and displayed in the main text section
  3. Union tables which offer more customization options

Problem Techniques Index


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 begintable, row, and endtable 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 \{ \} constructs to execute them in the text section of the PG file.

Also note that, perhaps obviously, the begintable takes as its argument the number of columns in the table, and the row command takes a list of strings to put in the data elements in the row. In this example, we use the ans_rule function to put an answer blank in some table elements.



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

Notice that the commands provided are case-sensitive (e.g., use BeginTable, not begintable). Because these are defined perl functions, we need to use the \{ \} constructs with curly braces to execute them in the text section of the PG file.

For BeginTable(), the argument border=>n sets the border thickness in pixels in HTML mode, while tex_border=>1pt sets the border thickness in TeX mode (for pdf hardcopies). Notice that the units pt must be included, where 1 point is 1/72 of an inch. The options spacing and padding are like the HTML commands cellspacing and cellpadding. The default is center=>1 to center the table, but this could also be set to 0.


Problem Techniques Index