WeBWorK Problems

Custom ijk symbols don't seem to be working with functional vectors

Custom ijk symbols don't seem to be working with functional vectors

by Paul Seeburger -
Number of replies: 2

I have been customizing the ijk symbols to place a hat above them as shown in my code below. Although this works well with numerical vectors, I am having trouble getting my custom ijk sybols to show with gradient vectors involving x and y variables. Is this a bug?

See my code below. If I use the number form of $gr it displays my custom i and j, but with the variable vector $gr it does not. Instead all I see is the default script bold i and j.

Code:


Context("Vector2D");
Context()->variables->add( dx=>'Real', dy=>'Real' );
Context()->flags->set( ijk=>1 );

$i = "\boldsymbol{\hat{\imath}}";
$j = MODES(
HTML_MathJax => "\boldsymbol{\hat{\jmath}}",
HTML => "\boldsymbol{\hat{\bf{\jmath}}}",
TeX => "\hat{j}");
$k = "\boldsymbol{\hat{k}}";

Context()->constants->set(
i => {TeX => $i},
j => {TeX => $j},
k => {TeX => $k});

$showPartialCorrectAnswers = 1;

$a = random(2,5,1);
$b = random(2,6,1);
$r = random(-1,1,2);

$fx = Compute( "$a*x + $b*e^($r*y)" )->reduce();
$fy = Compute( "$b*x*e^($r*y)" )->reduce();

$gr = Vector( $fx, $fy );
# $gr = Vector(3, 6);
$df = Compute( "$fx*dx + $fy*dy" );

Context()->texStrings;
TEXT(beginproblem());

$grad = MODES(
HTML_MathJax => "{\overset{\scriptstyle\rightharpoonup}{\nabla}}",
HTML => "{\overset{\rightharpoonup}{\vphantom{a}\smash{\nabla}}}",
TeX => "{\overset{\rightharpoonup}{\vphantom{a}\smash{\nabla}}}",
);

BEGIN_TEXT

Find the total differential \( dz \) of the function \(f\)
given its gradient is:
\[ $grad f(x,y) = $gr. \]

$PAR
\( dz = \) \{ ans_rule(35) \}

END_TEXT
Context()->normalStrings;

ANS($df->cmp() );

Context()->texStrings;
SOLUTION(EV3(<<'END_SOLUTION'));
$PAR SOLUTION $PAR

Since \($grad f(x,y) = f_x(x,y) $i + f_y(x,y) $j\) and
\( dz = f_x(x,y)\, dx + f_y(x,y)\,dy\), we have
\[ dz = $df. \]

END_SOLUTION
Context()->normalStrings;

Thanks!

Paul

In reply to Paul Seeburger

Re: Custom ijk symbols don't seem to be working with functional vectors

by Davide Cervone -
It turns out that formulas handle vectors differently, and don't end up referring to the constants in the context. This should be changed, but for now you can override the parser's Vector class with one that does what you want. Add the following
    Context()->lists->set(
      Vector => {class => "myVector"},
    );
    
    package myVector;
    our @ISA = ('Parser::List::Vector');
    
    sub TeX {
      my $self = shift;
      if ($self->{ijk} || $self->{equation}{ijk} || $self->{equation}{context}->flag("ijk"))
        {return $self->ijk("TeX",[$i,$j,$k,'\boldsymbol{\hat{0}}'])}
      return $self->SUPER::TeX;
    }
    
    package main;
to your code just before the $showPartialCorrectAnswers line.

Also, I'm not sure why you are needing MODES for your $j, as both \boldsymbol{\hat{\jmath}} and \boldsymbol{\hat{\bf{\jmath}}} should be identical in both MathJax and LaTeX, and I don't see why you don't want \boldsymbol for the TeX output.

Davide

In reply to Davide Cervone

Re: Custom ijk symbols don't seem to be working with functional vectors

by Paul Seeburger -

Thanks, Davide!

I will give this a try!

The reason I used Modes for the $j was that the text output to PDF was not displaying correctly without changing the way it was displayed, but I wanted the other two modes to display correctly on the computer.  It has been awhile since I did this, so I would need to take another look as to why I left out the \boldsymbol command.  Perhaps it changed the display in PDF format, but I will need to double check.

I do remember that the \jmath did NOT display correctly in TeX (PDF) mode.  It was giving me an empty box for that character.  So I just went with the hat over a regular bold j.

Thanks again, Davide!

Paul