(Answer) (Category) LON-CAPA User Help : (Category) Authoring : (Category) Authoring Problems :
How do I put a table in my problem?
Make your variables in a script block:

$a = 1;
$b = 2;
$c = 3;
$d = 4;

Two ways to make a table:

First Method:

Make a table in dreamweaver (or any other html editor).  Use the
variables ($a, $b, etc.) in the table.  Take the html code from your
table (everything between <table > and </table >) and paste into the text
block.  

Auxilliary First Method: 

(I've never tried this, but give it a shot).  Save your table as some
file name, example table.html, and upload it to your construction
space.  In the textblock of your problem, type:
        <import >/res/msu/ciskepau/your-directory/table.html</import >
Tell me if this works!

Second Method:

Type the HTML code into the textblock yourself:

<table >
  <tr >
    <td > stuff for row one, column one </td >
    <td > stuff for row one, column two </td >
    <td > $a </td >
  </tr >
  <tr >
    <td > stuff for row two, column one </td >
    <td > stuff for row two, column two </td >
    <td > $b </td >
  </tr >
  <tr >
    <td > stuff for row three, column one </td >
    <td > stuff for row three, column two </td >
    <td > $c </td >
  </tr >
  <tr >
    <td > stuff for row four, column one </td >
    <td > stuff for row four, column two </td >
    <td > $d </td >
  </tr >
</table >
I've written a routine to produce tables easily for my problems which 
might be useful to people.  Here's a quick description of how to use it:

1) import the library
put this at the top of your problem (before any script blocks)
   <import>/res/msu/hamlinmi/lib/MyLib.library</import>

2) construct the table
In your perl script block, collect your table entries into an 
array (each row in sequence):
   $p1 = random(450,500);  # some random data...
      ...
   # the table entries in order...
   @data = ('age','number of births (thousands)',
      '18-25',$p1,
      '26-35',$p2,
      '36-50',$p3,
      '51-65',$p4);
   $table = &DTable(2,@data);   # create a 2-column table

3) display the table
In your problem text (or even outside of an "outtext" block)
simply
   <display>$table</display>


Notes using DTable:

1) the table is centered.  If you don't like that, use the 
GTable routine instead.  Currently thats the only difference 
between them.

2) because the table is displayed usnig <display>, entries in the 
table cannot contain markup (HTML), unless you specifically prepare 
them for both output modes, by using the web() function for example.
In other words, don't feed this to DTable:
    @data = ( '<i>heading</i>', 'data',   ... );
because although this would look fine on the web, it would NOT 
print properly.  If you really want those italics, do something 
like this instead, which should print fine:
    @data = ( web('','{\em heading}','<i>heading</i>'), '', ... );

3) columns are usually center aligned by default, although you can 
change this:
    $o_calign = 'rl';  # column alignments: right, left
    $table = &DTable(2,@data);
The $o_calign string has one letter for each column, acceptable
letters are {r, l, c}.  For those familiar with LaTeX, this 
string is passed as the first parameter for a tabular environment,
and you may also include vertical bars "|" for vertical dividers
in your table:
    $o_calign = 'c|cl';   # vertical bar between column 1 and 2
    $table = &DTable(3,@data);  # 3 columns
Currently you can only add bars to the LaTeX output, not the web
output.. (hope to get it for web too eventually)

4) other options to control the display of the table are also 
working.  Simply set the value of these variables before calling 
the DTable routine.
This one sets the absolute text size:
    $o_tsize = 3;   # use a large text font
    $o_tsize = -2;  # use small text (any value -4 to 5 accepted)
This one appends (or prepends) text to all table entries:
    $o_utag = ' cm';  # append ' cm' to every table entry
    $o_utag = '^the ';  # prepend 'the ' to every entry
    $o_utag = web('^$','^\$','^$');  # prepend $
This one prepends each table entry with a label (Note: if you use 
this, column alignment defaults to left instead of center):  
    $o_labelt = 'A';  # label entries with capital letters
    $o_labelt = 'a';  # label with lowercase letters
    $o_labelt = '1';  # label with numbers
These options currently affecting LaTeX output only (at some point 
they should affect web output too):
    $o_vskip = 2;        # add 2mm space after each line 
    $o_hbars = ' -  ';   # horizontal line below 1st row (3-row table)
    $o_hbars = '-   -';  # horizontal line above and below 4-row table

More improvements are eventually coming...
hamlinmi@msu.edu
Click on the link titled "Show this answer as Text" to see the code examples I gave in the correct form.
  
The FAQ-o-Matic has its own markup rules and it doesn't show the text as I typed it...
hamlinmi@msu.edu
[Append to This Answer]
Previous: (Answer) Formula response questions with XML example
Next: (Answer) What do I do about warnings?
This document is: http://help.loncapa.org/cgi-bin/fom?file=118
[Search] [Appearance] [Show This Answer As Text]
This is a Faq-O-Matic 2.719.
This FAQ administered by the LON-CAPA team at MSU. Submit a help request ticket to contact us.