I just posted a question and can now generate a conditional section in a text: how to make a conditional document feature

But to extend and generalize the question, first how could I create a document with a multivalued parameter (let's say, a difficulty level). If I were to identify the difficulty level of each exercise with an integer from 1 to 10, suppose I want to define that I print exercises with difficulties from 1 to 5 only.

In addition, whether just a boolean as before or a multi-valued parameter, is there any way to set a value from outside LaTeX? I would like to run a batch job that builds both a teacher edition and a student edition automatically, and I would prefer not to embed that state in the document and continually change it back and forth.

  • 2
    You can do things like latex \def\x{y} \input file.tex from commandline. I'd prefer, however, such a script to generate a short "config" file, which is then \input by your document/package/class.
    – mbork
    Sep 11, 2013 at 19:25
  • Wouldn't it be better to quote (which may be shell-dependent)? So (in bash): latex "\def\x{y}\input{file.tex}"?
    – jon
    Sep 11, 2013 at 19:54

1 Answer 1


Here I present a ConTeXt solution using the --arguments feature and modes. Modes provide a way to conditionally include parts of a document. For instance, when printing a text book one can decide to print only the text or the text including exercises or everything, including exercises and hints to each exercise. Another use case is a single source file for slides and handouts.

It's up to you what you want to mark up. Either mark up each exercise, if you want to be able to include individual exercises, or mark the individual difficulty levels. That way you can include all exercises from a particular difficulty level.

    \enablemode [ex-\recurselevel]}}


\startmode [ex-1]
  A very easy exercise

  \startmode [clues]
    Clue to exercise 1

\startmode [ex-2]
  An easy exercise

\startmode [ex-3]
  A fairly easy exercise

  \startmode [clues]
    Clue to exercise 3

\startmode [ex-4]
  An moderate exercise

%% […]

\startmode [ex-10]
  A very hard exercise

  \startmode [clues]
    Clue to exercise 10


Here I created some example exercises with numeric difficulty levels. Some of the exercises have clues. Now you can include particular exercises on the command line. This includes exercise one and two and no clues.

context --arguments=from=1,to=2 file.tex

It renders as:


To include exercise three to ten and the clues, use:

context --arguments=from=3,to=10 --mode=clues file.tex

It renders as:


As you can see, the values of the arguments can be obtained using \env{key}. It is also possible to directly pass arguments to context:

context --bodyfont=pagella file.tex

The value can be obtained using \getdocumentargument{bodyfont} in this case. But I'd recommend using --argument, since it's safer and does not clash with already defined command line arguments.

  • I was going to post te exact same answer :)
    – Aditya
    Sep 12, 2013 at 2:31
  • 1
    @Aditya “Great minds think alike.” ;)
    – Marco
    Sep 12, 2013 at 5:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .