WeBWorK Problems

options for display of vectors (and answers)

options for display of vectors (and answers)

by Dick Lane -
Number of replies: 3
There are a variety of text notations used for vectors (\mathbf, overline, \vec) and for norms (|v| or ||v||).  When writing stuff (in plain TeX) for classes, I have coped with my own macros plus a style file to produce notation fitting my current text.  What do we have now in WeBWorK to be similarly polymorphic (i.e., write lots of problems with consistent markup and have eventual cosmetics depend on a single auxiliary file, or a fragment in PGcourse.pl) ??

I have attached a polyglot example which uses several options.  Even if I prefer the {\mathbf vector} variant, it is not very striking even on a high-resolution screen.  Do we have another mode or font, known by jsMath and MathJax, which could make the vector context more visually striking?

FWIW, my example also demonstrates one of my failed attempts to have the "Correct Answer" display structure rather than mere numbers.  (More than 7 variants have achieved only the trivial improvement of showing answer components as one decimal divided by another despite my attempts to display <a/norm,b/norm> with norm shown as sqrt(a^2+b^2) or sqrt(A2B2) where A2B2 = a^2 + b^2.)
In reply to Dick Lane

Re: options for display of vectors (and answers)

by Michael Gage -
On a site by site basis this can already be done if you have access to Constants.pm on the server.

In WeBWorK/Constants.pm is a code defining $WeBWorK::PG::ImageGenerator::TeXPreamble = ..
where you could define:
\def\myVec#1{\vec{#1}}
% \def\myVec#1{ {\bf #1 }} % alternate version

You will already see things such as

\def\gt{>}

in this space.

The real task will be to make this more configurable. I think there is a chance
that the TeXPreamble could be defined in global.conf instead (and therefore also in course.conf) which would make it possible to configure this course by course.

(This might take a little bit of work -- there was probably a reason the original designers placed this configuration in Constants.pm instead of global.conf. There is a spot in CourseEnvironment.pm where "evil things can be done to the course environment". I believe you could pull TeX configuration information out of the global.conf file at this point and use it to assign the $WeBWorK::PG::ImageGenerator::TeXPreamble variable. )

(I suspect there is a better way: There are several places where ImageGenerators are created. All of these have access to the course environment so TeX header information could be passed to the ImageGenerator object. However I think some of these calls are redundant so a little refactoring would be in order while trying to add a configuration feature.)

There is a lot of tweaking that could be done to the TeX "style" files that are
stored in webwork2/conf/snippets. For example -- perhaps you could change the output for hardcopy so that it uses a single column and puts white space after questions instead of using the double column format. (This seems to be similar to what Sam Hathaway is looking for.) Once one has some idea of the range of tweaking that would be desirable we can figure out a way to make the changes easier to make by an instructor who does not have direct access to the server.

-- Mike



In reply to Dick Lane

Re: options for display of vectors (and answers)

by Michael Gage -
I have created a few macros that make customization of TeX display a bit easier.  It would be nice to simplify it more.

First -- to have equations display vectors in a particular style place this 
in your PGcourse.pl file (and place "PGcourse.pl" in the loadMacros() queueue).

addToTeXPreamble("\\newcommand{\\myVec}[1]{\\vec{#1}} ");

If you were to place this command in a single question (a .pg file) then you use single
backslashes instead of double backslashes.  You should place the command towards the beginning of the question before any equations are rendered.

This affects only image ("dvipng") mode.  It does not affect jsMath or MathJax
mode -- that will have to come later.

You can now modify this preamble command in PGcourse.pl to obtain different styles for denoting vectors.

To make sure that you can see the changes (equation images are cached!)
place   

refreshEquations(1);

in a given question or in the PGcourse.pl file while testing.  This forces equation images to be recreated every time.  

To set the style for the hardcopy for a given homework set, place the command

\newcommand{\myVec}[1]{\vec{#1}} 

in the header file used for the homework set in the section which is 
read when creating hardcopy. (Or if you use separate header files for 
screen display and for image display place it in the hardcopy header file.

There are ways to change the TexPreamble for an entire site -- 
see the WeBWorK/Constants.pm file for images TexPreamble and 
the webwork2/conf/snippets directory for the locations of the files to change the hardcopy version.

jsMath and MathJax headers are defined in the webwork2/html/ directory.

It would be nice to be able to change all of the different presentation modes simultaneously with one command -- but that is not possible yet.

-- Mike

In reply to Dick Lane

Re: options for display of vectors (and answers)

by Michael Gage -
As a last resort -- here is a method to directly modify what you want displayed as the correct answer:

ANS( $ev -> cmp()->withPostFilter( 
 sub { my $ans_hash=shift; 
 $ans_hash->{correct_ans_latex_string}=
 "\left<\frac{$a}{$norm}, \frac{$b}{\sqrt{$a^2+$b^2}}\right>";
 $ans_hash;
 }
 )
);

The post filter explicitly changes the TeX for the correct answer, after it
has been computed during the comparing operation. (If you change the
{correct_ans_latex_string} before the comparison it just gets overwritten when the comparison occurs.)

One more gotcha -- no comparison occurs if the answer blank is empty -- so in that case no latex string is calculated and only the raw correct answer occurs. In other words to review your correct answer you must enter SOMETHING into the answer blank or you will never see the lovely tex message you have created!

This bug (which I helped create :-) ) kept me busy for several hours today. :-(.

------------------------
A change to MathObjects which creates the {correct_ans_latex_string} when the object is created would, I think, fix this. It would also simplify
the evaluation of answers in Problem.pm (This is a technical note -- so we
can remember what could be improved.)
---------------------------
If you do this often you could write the following subroutine in your problem
or in PGcourse.pl

sub fixCorrectAnswer {
 my $latex_string = shift;
 sub {
  my $ans_hash = shift;
  $ans_hash->{correct_ans_latex_string}=$latex_string;
 $ans_hash
 };
}


and call it with

ANS( $ev -> cmp()->withPostFilter( 
 fixCorrectAnswer(
 "\left<\frac{$a}{$norm},\frac{$b}{\sqrt{$a^2+$b^2}}\right>"
 )
 )
);