TransWikia.com

Custom itemize with extra parameters for items

TeX - LaTeX Asked by Timon P on February 8, 2021

I am currently working on a document class for creating worksheets for school in which I want to include a multiplechoice environment. For this I basically used the itemize environment and changed the default bullet to a big square drawn with tikz. I also want to be able to easily create solutions for these worksheets and so I introduced a boolean solution which determines if the correct answers are ticked or not.

Now my problem is how to define which ansers are correct and which ones are not. In the end I want this to look something like this:

begin{multiplechoice}
    item[correct] Correct answer
    item Incorrect answer
end{multiplechoice}

But I am not sure how to add this parameter to the items. So far, this is what the multiplechoice environment looks like:

newenvironment{multiplechoice}{
    renewcommand{labelitemi}{
        tikz[baseline=-0.3em]{
            draw[black, thick] (-0.2, -0.2) rectangle (0.2, 0.2);
            ifsolution
                draw[black, thick] (-0.2, -0.2) -- (0.2, 0.2);
                draw[black, thick] (-0.2, 0.2) -- (0.2, -0.2);
            fi
        }
    }

    itemize
    bgroup
}{
    egroup
    enditemize
}

Currently, every anser will be ticked if solution is true.

One Answer

Here is a solution using item as requested, with only a tiny change from the question: Instead of [correct], use [correct]. That change makes the solution much easier, since it matches the regular optional parameter that item always takes.

documentclass[12pt]{article}
usepackage{tikz}

newififsolution

newcommand{choicebox}{%
    tikz[baseline=-0.3em]{%
       draw[black, thick] (-0.2, -0.2) rectangle (0.2, 0.2);
    }%
}
newcommand{correct}{%
    ifsolution
        tikz[baseline=-0.3em]{%
            draw[black, thick] (-0.2, -0.2) rectangle (0.2, 0.2);
            draw[black, thick] (-0.2, -0.2) -- (0.2, 0.2);
            draw[black, thick] (-0.2, 0.2) -- (0.2, -0.2);
        }%
    else
        choicebox
    fi
}

newenvironment{multiplechoice}{%
    renewcommand{labelitemi}{choicebox}%
    itemize
}{%
    enditemize
}
% The previous bgroup and egroup were bad.

begin{document}

begin{multiplechoice}
    item[correct] Correct answer
    item Incorrect answer
end{multiplechoice}

solutiontrue


begin{multiplechoice}
    item[correct] Correct answer
    item Incorrect answer
end{multiplechoice}

end{document}

I would probably prefer to use an input syntax of choice and choice*, and a definition of choice could be layered over the solution with item.

makeatletter
newcommandchoice{@ifstar{item[correct]}{item}}
makeatother

Answered by Donald Arseneau on February 8, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP