## WeBWorK Main Forum

### How make Webwork display *MULTILINE* string answers properly when student wants Webwork to "Show correct answers" ?

by Christian Seberino -
Number of replies: 15

HOWEVER, when I try to set the correct answer to a multi-line string, that multi-line string doesn't get displayed correctly by Webwork when students select the box to "Show correct answers".

Is there a simple tweak I can do that will make Webwork display mult-line string answers WITHOUT any formatting like centering, special treatment of newlines, etc?

Thanks,

Chris

### Re: How make Webwork display *MULTILINE* string answers properly when student wants Webwork to "Show correct answers" ?

by Davide Cervone -
It turns out that there are several factors that contribute to this.

First, MathObjects are targeted around single-line answers, so there isn't much support for multi-line answers, as you have found. The ArbitraryString context that you have been using wasn't intended for multi-line input, and so it fails in severals ways when these are used.

Second, the correct answer used to be displayed as a string, but now it is the result of a TeX expression. That is a fairly recent change, and it was added on top of the MathObjects library in what was perhaps not he bet possible way (I am going to need to look into straightening that out).

In any case, the contextArbitraryString.pl file can be modified to do what you are looking for. I have made the modifications, and attach the changed file. Put it in your course's templates/macros file (or in the system pg/macros directory), and then you can use something like:

DOCUMENT();

"PGstandard.pl",
"contextArbitraryString.pl",
);

TEXT(beginproblem());

Context("ArbitraryString");

$a = Compute("Line 1~~nLine 2"); BEGIN_TEXT Enter your code:$BR
\{ANS_BOX(0,5,20)\}
END_TEXT

ANS($a->cmp); ENDDOCUMENT(); should do what you want. The ArbitraryString context will now properly handle multi-line input and correct answers. It renders the answers via an array (so that it can be rendered by TeX). The problemPreserveAnswers.pl file is loaded above in order to allow student answers to include characters like$ and @ that would not be properly saved by WeBWorK otherwise.

Note that the linebreaks that are returned as part of the answer can vary from platform to platform, so Mac browsers may give you carriage returns, and Windows may give you carriage return-linefeed pairs. This is taken care of in the ArbitraryString comparison routine, but if you do your one custom answer checker, you may need to normalize the line breaks yourself.

Davide

### Re: How make Webwork display *MULTILINE* string answers properly when student wants Webwork to "Show correct answers" ?

by Christian Seberino -
Davide

Thanks so much! It worked... even with PGML!

Webwork can handle more than just math!.....science and now programming classes.

cs

### Re: How make Webwork display *MULTILINE* string answers properly when student wants Webwork to "Show correct answers" ?

by Christian Seberino -
How make Webwork keep/display whitespace?
I noticed if I tried to indent a line in my answer, that it didn't display correctly.
The closest I got to adding spaces was with backslashes....

e.g.

print "hello";
\ \ \ \ # This is an indented comment.
print "there";

When the answer is displayed the backslashes appear.
I also tried double backslashes and "~~ ".

Sincerely,

Chris

### Re: How make Webwork display *MULTILINE* string answers properly when student wants Webwork to "Show correct answers" ?

by Davide Cervone -
It turns out that the display mode makes a difference, here. Image mode works as expected (whitespace is retained), but MathJax has a bug that prevents it. That is easily fixed, but you would need to modify your copy of MathJax, and you may not have access to that.

Instead, I have modified the contextArbitraryString.pl file again so that it will (optionally) display student and correct answers using HTML rather than TeX. To do this, add

Context()->flags->set(noLaTeXresults => 1);
right after setting the Context to ArbitraryString.

This version also fixes a couple of other issues, so see if that works better for you. In particular, it now handles the line-terminator conversion (from Mac and Windows linebreaks to unix line breaks) automatically.

Davide

### Re: How make Webwork display *MULTILINE* string answers properly when student wants Webwork to "Show correct answers" ?

by Christian Seberino -
Davide

Thanks!

This version converts multiple spaces into a single space and
multiple indented lines seem to have different amounts of spaces each.

For example...

aaaaaa
XXXXXXXXbbbbb
XXXXXXXXccccc
XXXXXXXXddddd

becomes

aaaaaa
Xbbbbb
XXccccc
XXXddddd

for some reason where X represents a blank space.

chris

### Re: How make Webwork display *MULTILINE* string answers properly when student wants Webwork to "Show correct answers" ?

by Davide Cervone -
What display mode are you using (MathJax, Image, etc.)?

### Re: How make Webwork display *MULTILINE* string answers properly when student wants Webwork to "Show correct answers" ?

by Davide Cervone -
I am not able to reproduce your results. It works as expected for me.

Can you provide a complete problem where you see this behavior?

Have you used the noLaTeXresults setting that I suggested in my earlier message?

Davide

### Re: How make Webwork display *MULTILINE* string answers properly when student wants Webwork to "Show correct answers" ?

by Christian Seberino -
Oops my fault. I got so excited from your email with the new file that
I forgot to add the noLaTeXresults setting. Now that I did that all
is working perfectly.

### Re: How make Webwork display *MULTILINE* string answers properly when student wants Webwork to "Show correct answers" ?

by Davide Cervone -
OK, great! Glad it is working correctly, now.

Davide

### Re: How make Webwork display *MULTILINE* string answers properly when student wants Webwork to "Show correct answers" ?

by Christian Seberino -
Davide

I found a subtle bug that isn't a show stopper but I wanted to mention it.

correctly IF STUDENTS ENTER AN ANSWER.

If the answer field is left blank by a student and they check
Subsequent lines have the indentation messed up as shown in the
example problem I've attached FYI.

(To recreate the problem, don't type an answer. Just leave the field blank.
If you type anything as an answer, the correct answer will suddenly
be formatted correctly!)

Sincerely,

Chris

### Re: How make Webwork display *MULTILINE* string answers properly when student wants Webwork to "Show correct answers" ?

by Davide Cervone -
You never responded about which display mode you are using. I'm suspecting it is MathJax (or perhaps the older jsMath). Note that I mentioned that MathJax has a bug in its implementation of \verb which is used in contextArbitraryString.pl. If you used image mode, it would display correctly.

Because of the way that the LaTeXed correct answer was added to the MathObjects answer checker, it is difficult to get all these situations to work properly in a reasonable way. I will be correcting that, but haven't had the opportunity yet.

In the meantime, here is a new version that should work better for you. The original works as though noLaTeXresults is false in the case where the answer is blank. This one works as through it is true in that case (which is the value you are using).

Hope that does what you need.

Davide

### Re: How make Webwork display *MULTILINE* string answers properly when student wants Webwork to "Show correct answers" ?

by Christian Seberino -
Thanks that worked!

cs

### Re: How make Webwork display *MULTILINE* string answers properly when student wants Webwork to "Show correct answers" ?

by Davide Cervone -

PS, there are a couple of issues with the code that you attached to your previous message.

1. You don't need to include the pattern for strings any longer as it is now part of the contextArbitraryStrings.pl file.

2. Your @answers[0] should be $answers[0], since you want the scalar value that is the 0-th entry of the array, not the array consisting of just the first entry of the @answers array. Davide In reply to Davide Cervone ### Re: How make Webwork display *MULTILINE* string answers properly when student wants Webwork to "Show correct answers" ? by Christian Seberino - I noticed that @answers[0] seems to work even though it should be$answers[0] as you said.

I don't know why as I'm a Python guy myself. Maybe it was just dumb luck on my part?! :)