WeBWorK Main Forum

display of a two value function

display of a two value function

by Clinton Ferreira -
Number of replies: 1
Hi, I need to display something in webwork but I'm not sure how. I'd like to give the students one function with two different values for different ranges of x. It needs to look someting like

f(x) = large-left-curly-bracket with one answer above another.

Doing this in latex is very easy using something like

\[f(x) = \left\{
\begin{array}{l l}
 A & \quad \mbox{-B \leq x \leq B}\\
 0 & \quad \mbox{|x| > B}\\
\end{array} \right. \]
But this doesn't work in webwork. So, I tried using a display matrix like

\{ mbox(

display_matrix(
[
[ '\(A\)', '\(-B\leq x \leq B\)'],
[ '\(0\)', '\(|x|>B\)' ],
],

align=>"r", left=>"{", right=>".")

)\}

But this only gives the right hand side and I'm not sure of how to include the f(x)= for the center of the left hand side.

Assuming my explanation makes sense, do you know of a way to display this equation correctly?

Thanks
In reply to Clinton Ferreira

Re: display of a two value function

by Davide Cervone -
There are a number of problems with your example array. The main problem with this in WeBWorK is that \{ is used by WeBWork to delimit commands to be performed whose output is to be included in the output text. These commands are performed before mathematics is processed, so WeBWorK tries to use part of your mathematics as a command to execute. To prevent that, you need to use $LBRACE in place of \{.

On the other hand, you should really be using \begin{cases}...\end{cases} rather than an array environment. That will obviate the need for the left brace in the first place, and also the \quad will no longer bee needed either.

Also, the \mbox{...} is not needed (and is in fact an error, though WeBWorK's math processor will ignore it). An \mbox will put TeX into text mode rather than math mode, so -B \leq x \eq B would not be processed properly.

So the final version should be more like

    \[f(x) = \begin{cases}
         A & -B \leq x \leq B\\
         0 & |x| > B\\
       \end{cases}\]

This should produce [dmath] f(x) = \begin{cases} A & -B \leq x \leq B\\ 0 & |x| > B\\ \end{cases} [/dmath]

It is also possible to use the piecewiseFunctions.pl macro file from the Union macro library. (Read the documentation at the top of the file). This is a non-TeX specific way of creating the piecewise function, and it will work correctly in more display modes than the raw TeX code above. This is older code, but still works.

Finally, the MathObjects contextPiecewiseFunction.pl context can be used to create multi-branched functions that display properly, but also can be entered by students, evaluated, differentiated, and so. But it is restricted to using actual constants as the limits of the branches, not symbolic constants, so it won't be able to do the [math]-B \le x \le B[/math] that you have above. I've looked into changing this, but it is not as easy to modify as I had hoped.

Davide