I'm working on a simple layout in LyX 2.2 for the exam class. Many things work very well, especially with the LatexType Item_Environment style in LyX layouts. However, I'm stuck at how to deal with the multiple-choice environment, because


are essentially two different \item possibilities within a LatexType Item_Environment (for LyX layouts). It doesn't look possible to have two different ItemCommand definitions in LyX. Arguably, \CorrectChoice is just a special case of \choice, but it's a lot to change the Exam class...

I realized it would be much easier if \CorrectChoice were just a \choice with an optional argument set to true (or 1), e.g., \choice[1]. Perhaps there's a way to hack it back to \CorrectChoice after LyX generates the LaTeX and before the LaTeX is compiled?

I have seen some hacking things done like this with the etoolbox package, but I couldn't find anything obvious. I admit I'm not much of a TeX hacker. It would be great to know if this is possible.


Turns out to be basic LaTeX hacking (rewriting a macro). Here's how I accomplished it, based on advice from this answer:

  • In LyX, set the ItemCommand in the Item_Environment to \mychoice
  • Add an optional argument for the items, as follows. Here's an example in the exam.layout file I'm developing:

    Style Multiple_Choice_Answers_(checkboxes)
      CopyStyle        Enumerate
      Category         Answers
      LatexType        Item_Environment
      LatexName        checkboxes
      ItemCommand      mychoice
      LabelType        Enumerate
      LabelSep         xx
      Argument item:1
        MenuString     "Mark correct"
        LabelString    "✓"
        Decoration     Classic

    In this case, multiple-choice answers (with checkboxes) have an optional "Mark correct" inset (that can contain anything). It will generate the \mychoice[] equivalent of an item.

  • In the document preamble, there need to be some commands to rewrite \mychoice according to the presence of the optional parameter (indicating the correct choice). As pointed out in the comments, we should use the Preamble ... EndPreamble construct in the .layout file, as this keeps this change also with the layout. Also, the makeat... is best left to LyX to manage (one of the many reasons I adore LyX is it manages well the complexity of LaTeX).

        % hack to rewrite \mychoice or \mychoice[] to \choice or \CorrectChoice respectively

In LyX, the multiple choices look like this: enter image description here

LyX generates this code:

\mychoice Here's an answer
\mychoice Here's another
\mychoice[] This is the correct answer.
\mychoice This is not correct.

Here's the output in PDF (with answers shown):

enter image description here

  • Excellent! Note that the LyX preamble is already inside \makeatletter…\makeatother so by writing \makeatother in the preamble you might mess things up. Also you can use the InPreamble layout tag to move your preamble code to the layout. All together this would make a nice LyX module. – G.M. Jul 15 '16 at 13:31
  • @G.M. I'm assuming you meant Preamble ... PreambleEnd? I'm making those changes (testing) and will update my answer. I'm going to put the layout on github. – Fuhrmanator Jul 15 '16 at 14:42
  • You are right, Preamble…PreambleEnd as per Customization 5.3.6. – G.M. Jul 15 '16 at 15:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.