WeBWorK Main Forum

Documentation for Tables in PGML

Documentation for Tables in PGML

by Gustavo Felisberto-Valente -
Number of replies: 5

https://webwork.maa.org/wiki/Tables

The official documentation tables are not compatible with BEGIN_PGML and END_PGML.

What would be the "basic" template for a table in PGML?

The documentation ( https://webwork.maa.org/wiki/Introduction_to_PGML ) says that PGML is based on Markdown, but the Markdown equivalent does not work:

| Column 1 | Column 2 | Column 3 |
| -------- | -------- | -------- |
| Row 1, Column 1 | Row 1, Column 2 | Row 1, Column 3 |
| Row 2, Column 1 | Row 2, Column 2 | Row 2, Column 3 |


In reply to Gustavo Felisberto-Valente

Re: Documentation for Tables in PGML

by Alex Jordan -

PGML is inspired by Markdown but was developed from scratch, not using an existing Markdown implementation. It does not currently have a table-making markup feature.

Any command you could execute inside \{...\} in a TEXT block can be executed in a PGML block, but delimiting with [@...@]. See:

https://webwork.maa.org/wiki/Command_substitution_-_PGML

In this case, you want to delimit with [@...@]* with that asterisk at the end, so that the HTML tags for the table structure will not be escaped. So:

[@ DataTable([
    [ Row 1 Column 1,  Row 1 Column 2,  Row 1 Column 3 ],
    [ Row 2 Column 1,  Row 2 Column 2,  Row 2 Column 3 ],
  ],
  options
)@]*

In WeBWorK 2.18, niceTables.pl has been updated a lot, and there are new features. Its documentation is currently just in the file:
https://github.com/openwebwork/pg/blob/PG-2.18/macros/ui/niceTables.pl
That is written for the 2.18 version of niceTables.pl.

In reply to Alex Jordan

Re: Documentation for Tables in PGML

by Gustavo Felisberto-Valente -

Thank you very much. I was able to write a table enclosed by BEGIN_PGML and END_PGML using your reply, and your documentation for niceTables.pl is really good. The example provided in line 224 was very helpful for a beginner like me (I know LaTeX and HTML, but still starting with PGML and Perl). I suggest adding other lines like that, for example, in line 170 it would be helpful to add an example like:

DataTable(
  [
    ['row 1 column 1','row 1 column 2'],
    ['row 2 column 1','row 2 column 2']
  ],
  tablecss => 'border: 1px solid black;',
  );

There were only a few things I could not implement. Specifically, I want to convert this HTML table to PGML. Notice the cell "Gender" spanning three rows, do you think it is possible to write this? This is what I have so far:

BEGIN_PGML
[@ 
  DataTable(
    [
      ['','',['Academic Major', colspan => 5,headerrow => 1]],
      ['','','Business','Science','Liberal Arts','Other','Total'],
      [['Gender', rowspan => 3, writing-mode => 'vertical-rl'],'Female',10,9,9,7,35],
      ['','Male',12,11,10,7,40],
      ['','Total',22,20,19,14,75],
    ],
    tablecss => 'border: 1px solid black;',
    midrules => 1,
    align => '| c | c | c | c | c | c | c |',
    empty-cells => 'hide',
  );
@]*
END_PGML
In reply to Gustavo Felisberto-Valente

Re: Documentation for Tables in PGML

by Alex Jordan -

Rowspan is not currently supported. I may look into making that possible before 2.18 is finalized.

It's maybe neither here nor there, but I advise caution with using tables that have colspan or rowspan. It can be hard for an assistive technology user to navigate such tables.

Here is a sample problem file that (pretty much) demonstrates everything that the 2.18 version of niceTables.pl can do:

https://github.com/openwebwork/pg/blob/PG-2.18/t/output_macros/niceTables.pg

Some features are new to 2.18 and not possible with an earlier version. Some features are done a new (better) way in the 2.18 version even though they could be done before. For those, the old way should still work, but the old way might not be demonstrated in that file.

For your table, for now with no rowspan, if it were me I would just leave out the cells that span multiple columns/rows and usee a caption. This would work:

BEGIN_PGML
[@
DataTable(
[
['',['Business', headerrow => 1],'Science','Liberal Arts','Other','Total'],
['Female',10,9,9,7,35],
['Male',12,11,10,7,40],
['Total',22,20,19,14,75],
],
horizontalrules => 1,
rowheaders => 1,
booktabs => 0,
texalignment => '|c|c|c|c|c|c|',
caption => 'Academic Major by Gender',
captioncss => 'text-align: center;',
);
@]*
END_PGML
However in my testing just now I see that the vertical rules between columns from the texalignment option are not appearing in the hardcopy. I'll look into that too.



In reply to Alex Jordan

Re: Documentation for Tables in PGML

by Alex Jordan -

Just following up that I do not think I will try to implement rowspan at this time. The short version is that it really complicates things way more than you might expect. And in different ways for HTML output versus LaTeX output.

The long version: here was the markup you suggested before:

BEGIN_PGML
[@ 
  DataTable(
    [
      ['','',['Academic Major', colspan => 5,headerrow => 1]],
      ['','','Business','Science','Liberal Arts','Other','Total'],
      [['Gender', rowspan => 3, writing-mode => 'vertical-rl'],'Female',10,9,9,7,35],
      ['','Male',12,11,10,7,40],
      ['','Total',22,20,19,14,75],
    ],
    tablecss => 'border: 1px solid black;',
    midrules => 1,
    align => '| c | c | c | c | c | c | c |',
    empty-cells => 'hide',
  );
@]*
END_PGML

With this particular suggestion, the empty string cell that precedes 'Male' and 'Total' is meant to be 'written over' by the "Gender" cell. OK, so to prevent an HTML cell from being created there, we would need to scan "above" in the table to see if a rowpan cell is overwriting this. And what does "above" mean? Remember that some cells in previous rows might be using colspan. And this isn't even looking at LaTeX complications yet.

The next idea would be to do like HTML does, and not even have those empty cells. So like:

BEGIN_PGML
[@ 
  DataTable(
    [
      ['','',['Academic Major', colspan => 5,headerrow => 1]],
      ['','','Business','Science','Liberal Arts','Other','Total'],
      [['Gender', rowspan => 3, writing-mode => 'vertical-rl'],'Female',10,9,9,7,35],
      ['Male',12,11,10,7,40],
      ['Total',22,20,19,14,75],
    ],
    tablecss => 'border: 1px solid black;',
    midrules => 1,
    align => '| c | c | c | c | c | c | c |',
    empty-cells => 'hide',
  );
@]*
END_PGML
OK but now consider that "11" cell just for example. It is really in the 4th column, but you cannot discern this just from looking at the markup of the 3rd row. You would have to make it understand the influence of the "Gender" cell from the 2nd row shifting column indices. We need to know which column it is really in to apply things like borders and other styling as defined in the texalignment string and elsewhere.

So to do this, it would be possible, but I think it would need the code to start out by doing a once-over where it is purely analyzing the impact of colspan and rowspan and inserting "dead" cells that can (a) be ignored for printing to output but (b) be relied upon to accurately locate a given cell's visual position. I don't think I have time to do this before 2.18 comes out.



In reply to Alex Jordan

Re: Documentation for Tables in PGML

by Gustavo Felisberto-Valente -
Thank you for all these suggestions. I think I am going to simplify the concept a little and place Gender on the header. So I don't need a "first column" only to output the second variable.
[@ 
DataTable(
[
[['Gender',header => 1],['Academic Major', colspan => 5,headerrow => 1]],
['','Business','Science','Liberal Arts','Other','Total'],
['Female',$data[0][0],$data[0][1],$data[0][2],$data[0][3],$data[0][0]+$data[0][1]+$data[0][2]+$data[0][3]],
['Male',$data[1][0],$data[1][1],$data[1][2],$data[1][3],$data[1][0]+$data[1][1]+$data[1][2]+$data[1][3]],
['Total',$data[0][0]+$data[1][0],$data[0][1]+$data[1][1],$data[0][2]+$data[1][2],$data[0][3]+$data[1][3],$total],
],
tablecss => 'border: 1px solid black;',
midrules => 1,
align => '| c | c | c | c | c | c | c |',
);
@]*

Please ignore the "data[i][j]" there, they are part of a randomization process I defined in the preamble. The only changes I made were: removed the first column, wrote Gender in the first cell (as a header)