WeBWorK Problems

Displaying Radio Options Horizontally instead of Vertically?

Displaying Radio Options Horizontally instead of Vertically?

by Paul Seeburger -
Number of replies: 6
I have seen a questionnaire in WeBWorK that displays radio buttons horizontally, but all example code for regular problems I can find seems to always display them vertically, even when the options are short, e.g., 0, 1, 2, 3, 4, etc.

Is there an easy way to display radio button options horizontally that I am not seeing?  

Can I use the method shown in the questionnaire of using:

@button_string = ('%0' =>0,1=>1,2=>2,3=>3,4=>4,5=>5);


and then using this later to display (and evaluate?) it:


ans_radio_buttons(@button_string) 


If so, how do I select the correct answer in this case?

Thanks!

Paul
In reply to Paul Seeburger

Re: Displaying Radio Options Horizontally instead of Vertically?

by Paul Pearson -
Hi Paul,

Use parserRadioButtons.pl with the option 

separator => ","

to replace the default separator $BR with a comma.  For instance,


$radio = RadioButtons(["Incorrect item 1","Correct item","Incorrect item 2"], "Correct item", separator=>",");
BEGIN_TEXT
Question text (choose one): \{ $radio->buttons \}
END_TEXT
ANS($radio->cmp);

Best regards,

Paul Pearson
In reply to Paul Pearson

Re: Displaying Radio Options Horizontally instead of Vertically?

by Paul Seeburger -
Thanks, Paul!

But when I try this in WeBWorK version 2.12, I still get the options on separate lines vertically, but with a comma between them.

Here is my problem code.  Note that I currently still have a regular multiple choice question first.  The RadioButtons element is in the second part of the question.

DOCUMENT();
loadMacros(
  "PGstandard.pl",
  "MathObjects.pl",
  "PGunion.pl",  
  "parserMultiAnswer.pl",
 ## "PGcourse.pl",
  "PGchoicemacros.pl",
  "parserRadioButtons.pl"
);
TEXT(beginproblem());
$showPartialCorrectAnswers = 1;

######################
#              SETUP              #
$diagram = Image('imageP3-6Main.gif',size=>[400,400],tex_size=>400);

$diagrama = Image('imageP3-6a.gif',size=>[188, 251],tex_size=>300);
$diagramb = Image('imageP3-6b.gif',size=>[188, 251],tex_size=>300);
$diagramc = Image('imageP3-6c.gif',size=>[188, 251],tex_size=>300);
$diagramd = Image('imageP3-6d.gif',size=>[188, 251],tex_size=>300);

   $mc = new_multiple_choice();
   $mc -> qa (
      "a. The contour plot of a function \(f(x, y)\) is shown below along with a blue constraint curve. Which of the four 3D graphs shows a surface plot of this function \(f(x, y)\) and the correct projection of the constraint curve onto the surface?",
     "D");

   $mc -> makeLast(
                          "A",
                          "B",
                          "C",
                          "D",
                          "None of the 3D graphs represent \(f(x, y)\) and this constraint correctly.");
 
$radio = RadioButtons(["0","1","2","3","4","5","6","More than 6."], "6", separator=>"");

#          END SETUP          #
######################

######################
#          MAIN TEXT           #

Context()->texStrings;
   BEGIN_TEXT
\{$mc -> print_q()\}
$PAR
\{ColumnTable($mc -> print_a().$BR,
$diagram, indent=>10, separation=>30, valign=>"TOP")\}

    \{
      BeginTable(spacing=>5).
      AlignedRow(["A. ".$diagrama, "B. ".$diagramb, "C. ".$diagramc, "D. ".$diagramd]).
      EndTable()
    \}
$PAR
b. For the function \(f(x, y)\) in the previous question, how many local extrema does \(f(x, y)\) have if you restrict the input points of \(f\) to points on the given ellipse constraint curve?
$PAR
\{$radio->buttons\}
   END_TEXT
   Context()->normalStrings;

#      MAIN TEXT  END     #
######################

#############################
#       ANSWER EVALUATION        #

      $showPartialCorrectAnswers = 0;
      ANS( radio_cmp( $mc->correct_ans() ) );
      ANS($radio->cmp());

#   ANSWER EVALUATION END   #
#############################

ENDDOCUMENT();
In reply to Paul Seeburger

Re: Displaying Radio Options Horizontally instead of Vertically?

by Robin Cruz -
Defining a new separator worked for me (I put in spaces,) but only if the problem was in a regular homework set. It goes back to a vertical display if the same problem template (file) is used in a gateway quiz format. Here is how I defined the radio button:

@list = ("Sample","Statistic","Parameter","Population");
($a, $b, $c, $d) = NchooseK(4,4); #Scramble the choices.
$correctb = "Parameter";
$radiob = RadioButtons([$list[$a],$list[$b],$list[$c],$list[$d]], $correctb, separator=>" $SPACE $SPACE $SPACE $SPACE $SPACE");

It shows up vertically on the homework set, but horizontally in the gateway quiz. By the way, I'm on ww_version: WeBWorK-2.13.
Thanks -- rac
In reply to Robin Cruz

Re: Displaying Radio Options Horizontally instead of Vertically?

by Glenn Rice -
Here is a quick hack that will make this work in a gateway quiz. Add

TEXT(MODES(HTML => "<script>jQuery(function(){jQuery('.gwProblem').addClass('problem-main-form form-inline');});</script>", TeX => ""));

to the text of the problem. This will not affect anything in regular homework problems.

The reason horizontal radio buttons work in regular homework assignments is because the math4.js file adds the css class "form-inline" to the main form that has id "problemMainForm", and then the bootstrap.css style file makes the labels display inline-block, which allows the labels to stay on the same line.

The reason this doesn't work (without adding the above hack) is because there is no form with the id "problemMainForm" in a gateway quiz. Instead there are divs that have the class "gwProblem" for each problem, but the math4.js file does not add the "form-inline" class to that, and the bootstrap.css style file makes labels without that class display block, which means the labels will be on different lines.

There are a few other style differences needed to make things look right as well, but the above hack takes care of them all.

I should add that I have not tested this thoroughly, and the hack may mess up the appearance of other things in the gwProblem div. Although in my quick test things looked fine.
In reply to Glenn Rice

Re: Displaying Radio Options Horizontally instead of Vertically?

by Robin Cruz -
Thanks, Glenn,
Finally had several classes take gateway quizzes using the code you sent. It seems to be working without any other issues arising.
--rac