[Tex/LaTex] Any way to set a variable externally

conditionalsmacros

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.

Best Answer

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.

\doifsomething{\env{from}}{%%
  \dostepwiserecurse{\env{from}}{\env{to}}{1}{%%
    \enablemode [ex-\recurselevel]}}

\starttext

\startmode [ex-1]
  A very easy exercise

  \startmode [clues]
    Clue to exercise 1
  \stopmode
\stopmode

\startmode [ex-2]
  An easy exercise
\stopmode

\startmode [ex-3]
  A fairly easy exercise

  \startmode [clues]
    Clue to exercise 3
  \stopmode
\stopmode

\startmode [ex-4]
  An moderate exercise
\stopmode

%% […]

\startmode [ex-10]
  A very hard exercise

  \startmode [clues]
    Clue to exercise 10
  \stopmode
\stopmode

\stoptext

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:

sc1

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

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

It renders as:

sc2

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.

Related Question