From WeBWorK_wiki
Revision as of 20:41, 11 February 2010 by Pearson (talk | contribs) (New page: <h2>Putting Graphs into Tables</h2> <!-- Header for these sections -- no modification needed --> <p style="background-color:#eeeeee;border:black solid 1px;padding:3px;"> <em>This PG c...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Putting Graphs into Tables

This PG code shows how to put graphs into tables so that they will be displayed compactly and a proper size in both HTML and TeX modes.

  • Example 1: Putting a text and graphics side-by-side using ColumnTable from

Problem Techniques Index

PG problem file Explanation




Initialization: Be sure to load Set $refreshCachedImages=1; if you desire the graphics files to be refreshed each time.


$a = random(2,3,1); # left x-intercept
$b = random(2,4,2); # right x-intercept
$c = random(-4,-1,1); # y-intercept

$k = -($c)/($a * $b);

$A = $k;
$B = $k*($a - $b);
$C = -($k * $a * $b);

$gr = init_graph(-5,-5,5,5,axes=>[0,0],grid=>[10,10]);

add_functions($gr, "$A*x**2+$B*x+$C for x in <-5,5> using color:blue and weight:2");
$gr -> lb(new Label ( 4.5,0,'x','black','left','bottom'));
$gr -> lb(new Label ( 0.25,8.5,'y','black','left','bottom'));
$gr -> lb(new Label ( 0.25,$c,'y = f(x)','black','left','bottom'));

Setup: Generate a graph to be included in the main text. This graph has

"Use the graph to find the missing values.  
There may be more than one correct answer, 
in which case you should enter your answers
as a comma separated list.  If there are no
correct answers, enter ${BITALIC}NONE.${EITALIC}".
"(a) \( f(0) = \) ". 
"(b) \( f \big( \) ".ans_rule(7)." \( \big) = 0 \). ", # comma!
image(insertGraph($gr), width=>400, height=>400, tex_size=>700).
"(Click on graph to enlarge)".
$ECENTER, # comma!
indent => 0, separation => 30, valign => "TOP"

Main Text: We use ColumnTable( columnA, columnB, options ) to display the text and answer blanks in the left column, and the graph in the right column. Notice that within each column, every line except for the last ends with a period to join together things like strings, answer rules, etc. The last line in both columns ends with a comma, as noted in the commented out portions # comma!.

Using ColumnTable has two main advantages.

  • Using a ColumnTable to put the graphics and text side-by-side is very useful when the text is long, because if the graphic were below a long block of text, the student would have to scroll up and down frequently between the questions and the graphic, making it harder to use.
  • ColumnTable behaves well in TeX mode. The default in TeX mode is a two-column format for the printed page, which means that each column is very narrow. Using an ordinary table to put text and graphics side-by-side would likely mean that the graphics would spill over from the left column of the printed page to the right column of the printed page (causing text and graphics to overlap) or, worse yet, spill over from the right column of the printed page into oblivion. However, using a ColumnTable, the graphics will appear underneath the text, which is desirable.

$showPartialCorrectAnswers = 1;

ANS( List($c)->cmp() );

ANS( List(-$a,$b)->cmp() );


Answer Evaluation: Since there may be multiple answers, we use List(), even when there is only one answer (to avoid error messages if a student enters multiple answers when there is only one correct answer).

Problem Techniques Index