WeBWorK Main Forum

multirow in tabular?

multirow in tabular?

by Andrew Dabrowski -
Number of replies: 11
I'm working with PGML and trying to write a macro for displaying matrices in table form. I see that I can use the \multicolumn command within the tabular environment, but the \multirow command does not seem to be recognized. Is there a pl file I can load to add the functionality, or should I just do without it?
In reply to Andrew Dabrowski

Re: multirow in tabular?

by Paul Pearson -
Hi Andrew,

In general, it would be nice if your post was more descriptive and also attached some code that you've been working on.

Is the example below what you had in mind?

Best regards,

Paul Pearson

######### begin pg code ##############

DOCUMENT();
loadMacros(
"PGstandard.pl",
"MathObjects.pl",
"PGML.pl",
"unionTables.pl",
);

TEXT(beginproblem());
$showPartialCorrectAnswers = 1;

Context("Numeric");

$A = Matrix([[1,2,3],[4,5,6]]);

Context()->texStrings;
$table = BeginTable().
AlignedRow([" ", "Column label"]).
AlignedRow(["Row label", "\( $A \)"], separation=>5).
TableSpace(10,25). # vertical separation in pixels, points
EndTable();
Context()->normalStrings;

BEGIN_PGML
[` [$A] `]

[$table]***
END_PGML

Context()->texStrings;
BEGIN_TEXT
\( $A \)

$table
END_TEXT
Context()->normalStrings;

ENDDOCUMENT();
In reply to Paul Pearson

Re: multirow in tabular?

by Andrew Dabrowski -
Thanks, I had been looking at nicetables.pl and somehow missed uniontables.

In reply to Andrew Dabrowski

Re: multirow in tabular?

by Andrew Dabrowski -
Now that I look more closely at
http://webwork.maa.org/pod/pg_TRUNK/macros/unionTables.pl.html
I don't know if the two-column restriction is workable.

My goal is to do game theory payoff matrices along the lines of the attached png.

I got pretty good results with a perl macro that produces raw latex output, like the following.

" \\begin{tabular}{ll|$cstring}
&& \\multicolumn{$numcols}{l}{\\bf $colname} \\\\
$colstratheaders \\\\ \\hline
\\multirow{$numrows}{*}{\\bf $rowname}& $label1 & $payoffs1 \\\\
&
$label2 & $payoffs2 \\\\
\\end{tabular} "

This output is then displayed inside [`` ``].

But although the \multicolumn command is expressed correctly, the \multirow is ignored.

My question is very specific: can the \multirow command be made to work in this context?
Attachment payoffmatrix.png
In reply to Andrew Dabrowski

Re: multirow in tabular?

by Paul Pearson -
Hi Andrew,

I think MathJax focuses on displaying TeX math in html mode, so you'll probably want to use array instead of tabular.  You can nest arrays to get the effect you want without using multicolumn or multirow.  It's not perfect (nor is it turned into a subroutine or a macro), but is renders OK in html mode and very well in pdf hardcopy mode.

Best regards,

Paul Pearson


######## begin pg code ##############

DOCUMENT();
loadMacros(
"PGstandard.pl",
"MathObjects.pl",
"PGML.pl",
);

TEXT(beginproblem());
$showPartialCorrectAnswers = 1;

Context("Numeric");

@a = map{ random(-5,5,1); } (1..8);

$array = qq(
\begin{array}{cc}
& \text{Firm 2} \\
\text{Firm 1} &
\begin{array}{l|c|c|}
& \text{Product A} & \text{Product B} \\ \hline
\text{Product A} & $a[0], $a[1] & $a[2], $a[3] \\ \hline
\text{Product B} & $a[4], $a[5] & $a[6], $a[7] \\ \hline
\end{array}
\end{array}
);

BEGIN_PGML
>> [``  [$array] ``] <<
END_PGML

ENDDOCUMENT();
In reply to Andrew Dabrowski

Re: multirow in tabular?

by Alex Jordan -
niceTables.pl doesn't support cells that span multiple columns or rows. I think that with some effort, the capacity could be added. It doesn't help right now, but I'll put this on my long term to-do list.

Other solutions like using a lot of math mode arrays will break accessibility standards. With your example png, for that table to appear in a web accessible way in the online version of the problem, "Firm 1", "Firm 2", the two instances of "Product A", and the two instances of "Product B" should all be th elements with the appropriate scopes, and there should be colgroups and rowgroups involved. Otherwise you might have something that looks fine for sighted users, but either can't be read or is disorienting to a screen reader user.

In reply to Alex Jordan

Re: multirow in tabular?

by Andrew Dabrowski -
Yes, after trying out Paul's example I see that the Firm labels are not properly aligned.

No biggy though, I just thought I would see how nicely it could be done currently, which is at least nice enough.
In reply to Alex Jordan

Re: multirow in tabular?

by Alex Jordan -
I added colspan=>integer capacity to niceTables.pl. As a byproduct, you can also declare halign=>string for a cell. halign is perhaps not the best name for what it does, but it's consistent with align=>string for the whole table (which is also a bad name, but anyway).

So you can do things like
DataTable([[1, 2, 3],
[4, [5,colspan=>2]]]);

And the halign string can be something simple like "r" or something complicated like "|>{\color{blue}\cellcolor{green}}r|". If you use colspan in a cell, you should probably also use halign for that cell and get specific.

In the same way that align for the whole table works, the bits of halign for a cell will be interpreted and applied for both hardcopy and screen output. Pipes, the actual alignment character (r, c, l, p{width}, or X), coloring, boldface, italics, and monospace all get applied to both output modes.

Right now this is sitting in a pull request to the develop branch of pg. If anyone is in more of a hurry, you can make a local macros copy of the file by following the pull request: https://github.com/openwebwork/pg/pull/233.

It remains to implement rowspan.
In reply to Andrew Dabrowski

Re: multirow in tabular?

by Davide Cervone -
Personally, I think it is a mistake to use math mode for this at all. The table should be an HTML table in screen mode, and so I'd recommend Alex's niceTables.pl macros (except for the math in the cells itself). Here is one possibility:
loadMacros("niceTables.pl");

$SPACER = MODES(
  HTML=>'<span style="visibility:hidden">&nbsp; Product A &nbsp;</span>',
  TeX=>'\phantom{Product A}'
);

BEGIN_TEXT
\{
DataTable([
["",["${SPACER}Firm 1",header=>"TH"]],
[["${BR}Firm 2",header=>"TH"],DataTable([
   ["",["Product A",header=>"CH"],["Product B",header=>"CH"]],
   [["Product A",header=>"RH"],"\(-2, -2\)","\(2, 2\)"],
   [["Product B",header=>"RH"],"\(2, 2\)","\(1, 1\)"],
  ], midrules=>1,align=>"|c|c|c|")]
])
\}
END_TEXT
This probably doesn't meet Alex's approval for screen readers, but perhaps it is closer than a table in math notation.
In reply to Davide Cervone

Re: multirow in tabular?

by Paul Pearson -
Hi Davide,

Nice solution!  I have not previously used niceTables.pl.  In the definition of $SPACER, you used "\phantom{Product A}" in TeX mode, but "Product A" in HTML mode.  As a result, the first row of the table reads "Product A Firm 1" in HTML mode, when you probably wanted it to read "          Firm 1".  Would using $SPACE$SPACE... to push "Firm 1" to the right in the HTML mode be a good idea in the definition of $SPACER?

Thanks!

Paul Pearson

############# begin pg code ################


DOCUMENT();

loadMacros(
"PGstandard.pl",
"MathObjects.pl",
"PGML.pl",
"niceTables.pl",
"PGcourse.pl",
);

TEXT(beginproblem());

$SPACER = MODES(
  HTML=>'  $SPACE$SPACE$SPACE$SPACE$SPACE$SPACE$SPACE$SPACE$SPACE$SPACE  ',
  TeX=>'\phantom{Product A}'
);

BEGIN_TEXT
\{
DataTable([
["",["${SPACER}Firm 1",header=>"TH"]],
[["${BR}Firm 2",header=>"TH"],DataTable([
   ["",["Product A",header=>"CH"],["Product B",header=>"CH"]],
   [["Product A",header=>"RH"],"\(-2, -2\)","\(2, 2\)"],
   [["Product B",header=>"RH"],"\(2, 2\)","\(1, 1\)"],
  ], midrules=>1,align=>"|c|c|c|")]
])
\}
END_TEXT

ENDDOCUMENT();

In reply to Paul Pearson

Re: multirow in tabular?

by Davide Cervone -
Actually, there was supposed to be a <span> element there with visibility:hidden, but I forgot to escape the angle brackets, so Moodle used them as part of the HTML of the page rather than showing them. I have edited my response above so they now show (the visibility:hidden caused them to not be visible, naturally, but it was picked up by your copy and paste, but without the enclosing <span> that made it invisible). I prefer this to the $SPACE approach, since the amount of space you get in that case depends on the font you are using, whereas the invisible Product A is more likely to be the right size.