WeBWorK Problems

Correct syntax for PGML display math in variables

Correct syntax for PGML display math in variables

by Sean Fitzpatrick -
Number of replies: 1

Not sure the subject captures exactly the issue, so I'll try to elaborate.

A few summers ago we hired some students to help write some problems for us.

Some of those problems look OK when accessed by a student, but don't render properly in other areas, such as the homework sets editor. An example is below.

The issue seems to be that in the setup (prior to BEGIN_PGML) there are some variables defined to contain some LaTeX bits. The goal seems to be to render a system of equations with a large left brace at the front.

Obvious first question: why the hell does a system of equations need a big ol' brace at the front anyway? That was my thought. I was about to just delete it and move on, but decided I should learn the right way in case it comes up later.

Here is sample code: 

## DESCRIPTION
##      Solving a system of equations where the resulting matrix
##      is the identity matrix
## ENDDESCRIPTION

## DBsubject(Linear algebra)
## DBchapter(Matrices)
## DBsection(Row operations)
## Date(2018-06-12)
## Institution(University of Lethbridge)
## Author(Mitchell Sulz-Martin)
## Static(1)
## MO(1)
## Level(3)
## KEYWORDS('ULETH-1410', 'Gaussian Elimination', 'Row Operations', 'System of equations')


DOCUMENT();

loadMacros(
  "PGstandard.pl",
  "MathObjects.pl",
  # Provides greater control over the layout of the problem.
  "PGML.pl",
  # Used for course-specific initializations.
  "PGcourse.pl",
  "AnswerFormatHelp.pl",
);

TEXT(beginproblem());


###################################
#  Setup

#-ULETH-#
Context("Matrix");

sub disSYS{
    $M = shift;
    @dim = $M->dimensions;
    @d = @_;
    $data = "\[ \left\lbrace\begin{array}{";
    @var = ("x", "y", "z");

    for (my $i = 0; $i < $dim[1]; $i++) { $data = $data."r"; }
    $data = $data."}";
   
    for(my $i = 0; $i < $dim[0]; $i++){  
        my $nonzero = 0;
        for (my $j = 0; $j < $dim[1]-1; $j++){
            $m = $M->element($i+1,$j+1);
            if($j == 0){
                if ($m == 1) {$data = $data."$var[$j]"; $nonzero = 1;}
                elsif ($m == -1) {$data = $data." - $var[$j]"; $nonzero = 1;}
                elsif ($m != 0) {$data = $data."$m $var[$j]"; $nonzero = 1;}
                else {$data = $data." & ";}}
            else{
                $sign = ($m > 0) ? "+" : "-";
                $sign = ($m > 0 && $nonzero == 0)? " ":$sign;
                if ($m == 0) {$data = $data." & & ";}
                elsif (abs($m) == 1 && $nonzero) {$data = $data."& $sign &  $var[$j]";}
                elsif ($nonzero) {$a = abs($m); $data = $data."& $sign & $a $var[$j]";}
                else {$data = $data."& $m $var[$j]";}
                $nonzero = 1;
                if ($j == $dim[1] - 2 && $i != $dim[0] - 1) {$data = $data." & = & ".$M->element($i+1,$j+2)." \\";}
                elsif ($j == $dim[1] - 2 && $i == $dim[0] - 1) {$data = $data." & = & ".$M->element($i+1,$j+2);}}}}
    $data = $data."\end{array}\right. \]
";
    return $data;}
   
do { $L = non_zero_random(-4, 4); $J = non_zero_random(-3, 5); $K = non_zero_random(-3, 5);
     $I = non_zero_random( 0, 5); $H = non_zero_random( 0, 5); $E = non_zero_random( 0, 3);
     $D = non_zero_random( 0, 2); $B = non_zero_random( 0, 5); $C = non_zero_random(-3, 0);
     $F = non_zero_random(-8, 8); $G = non_zero_random(0, 3); $A = non_zero_random( 0, 5);}
until($L > $J && $J > $K && $H > $I && ($A % ($E*$C)) == 0 && $L != 1 && $L != -1 && $I != 1 && $G != 1 );


$alpha = $B + ($D*$C);
$beta = $E + ($G*$D);
$gamma = $A + ($E*$C);

$x1 = $L;
$y1 = $L * $G;
$z1 = $L * $beta;
$s1 = $L * ($gamma + ($G * $alpha));

$x2 = $J;
$y2 = $I + ($J * $G);
$z2 = ($I * $D) + ($J * $beta);
$s2 = ($I * $alpha) + ($J * ($gamma + ($G * $alpha)));

$x3 = $K;
$y3 = $H + ($K * $G);
$z3 = $F + ($D * $H) + ($K * $beta);
$s3 = ($F * $C) + ($H * $alpha) + ($K * ($gamma + ($G * $alpha)));

$M = Matrix([[$x1, $y1, $z1, $s1,],[$x2, $y2, $z2, $s2,],[$x3, $y3, $z3, $s3,],]);
$ds = disSYS($M);

#-ENDULETH-#

###################################
#  Main Text

#-ULETH-#
$M = Matrix([[1, 0, 0, $A,],[0, 1, 0, $B,],[0, 0, 1, $C,],]);
BEGIN_PGML
###Solve the following system of equations.
[$ds]
   
>>[`x = `][____]{$A}<< 
>>[`y = `][____]{$B}<< 
>>[`z = `][____]{$C}<< 
   
END_PGML

#-ENDULETH-#



COMMENT("Uses PGML.
Randomization provides many different possible versions of this question.<BR>
    Does NOT include a solution set.<BR>
    Recommended Settings:<BR>
    - Weight: 2<BR>
    - Max attempts: Unlimited<BR>
    - Show me another: -2<BR>
    - Rerandomize after: Default<BR>
    Made from a ULETH template.<BR>");
#-ENDULETH-#

ENDDOCUMENT();

In reply to Sean Fitzpatrick

Re: Correct syntax for PGML display math in variables

by Danny Glin -

When I tried to reproduce your issue I discovered that I only had problems if the display mode was set to images.  Further it appears that even in images mode PGML generates MathJax for its equations, but that's a bug for another thread.

It looks like the problem is that PGML isn't designed to handle \ [ and \ ] as delimiters for LaTeX (spaces added so the Moodle forum doesn't parse this as math).  Here's what worked for me:

Remove \ [ and \ ] from the string being generated by disSYS, then use the following in the BEGIN_PGML block:

[``` [$ds] ```]

Though when I view this in images mode in the Homework Sets Editor it breaks each equation into two lines.  The three backticks indicate to use LaTeX display mode in PGML.

It's also worth noting that if the disSYS subroutine is used in multiple problems it should probably be put into a macro file.