6

I am creating a random quiz in ConTeXt, using itemizations. I need to only show the first 10 items, and discard the rest, never showing them.

\starttext
    \startitemize[n]
        \item This is the first question.
        \item This is the second question.
        \item ...
    \stopitemize
\stopttext

I tried using \doboundtext and \limitatetext but those don't seem designed for this specific form of cropping.

Is there a way to have ConTeXt only show the first n number of items in an itemization?

  • Limiting the number of entries is easy when you use \startitem ... \stopitem for each entry but it requires a few changes in the itemize code. To add this function to itemize you should ask for it in the ConTeXt mailing list. – Wolfgang Schuster Jun 5 at 18:30
9

Something like this could be easily done in Lua. The code below not only prints only the first five items from the list but also shuffles the items so that the order of the questions is randomized.

\starttext

\startluacode
-- https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle
local function shuffle(list)
    for i = #list,2,-1 do
        local j = math.random(i)
        list[i], list[j] = list[j], list[i]
    end
end

local items = {
    "This is the first question",
    "This is the second question",
    "This is the third question",
    "This is the fourth question",
    "This is the fifth question",
    "This is the sixth question",
    "This is the seventh question",
    "This is the eighth question",
    "This is the nineth question",
    "This is the tenth question",
}

shuffle(items)

context.startitemize{ "n", "packed" }
for i = 1, 5 do
    context.item(items[i])
end
context.stopitemize()
\stopluacode

\stoptext

enter image description here

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.