3

In ConTeXt, is it possible to use modes to extract only parts of a document, say only the exercises?

My naive try was

\defineenumeration[exercise][
before={\startmode[Quest]\blank[line]},
after={\blank[line]\stopmode},
]

\starttext
\input knuth
\startexercise
\input tufte
\stopexercise
\input knuth
\stoptext

but that does not even compile. What I would ultimately like to have:

Running context file.tex I get the full document.

Running context --mode=Quest file.tex I get only the exercises (that are defined in the enumeration exercise).

I would also like, if possible, not have to type \startmode and \stopmode at every instance where the exercises appear.

7
  • What about puting the definition inside \startmode?
    – Manuel
    Commented Nov 6, 2016 at 8:27
  • Thank you for your comment, but I dont see how that helps me to get what I'm after. Maybe I just dont see how modes work.
    – mickep
    Commented Nov 6, 2016 at 8:32
  • 2
    Here you have an answer from Henri Menke.
    – Manuel
    Commented Nov 6, 2016 at 9:05
  • Thank you for the link. Now at last I know that it is not possible to do it with before and after. Also, this shows how to hide the exercises. I dont see how to hide everything but the exeecises. But it is getting closer.
    – mickep
    Commented Nov 6, 2016 at 9:22
  • I don't think ConTeXt (or any other variant of TeX) can do what you want. A mode can show or hide parts of a document. Your requirements amount to filtering part of your document when a mode is enabled (hiding all the rest). It's relatively straightforward to come up with a solution in xml, processed by ConTeXt. But if you want to write your document in ConTeXt syntax, I don't see how this is possible.
    – Thomas
    Commented Nov 6, 2016 at 10:57

1 Answer 1

4

If you don't mind modifying your source file a little bit, here is a solution using the translate module.

\defineenumeration[exercise]
\usemodule[translate]
\translateinput[startexercise][stopnotmode\startexercise]
\translateinput[stopexercise][{stopexercise\startnotmode[question]}]
\enableinputtranslation


\starttext\startnotmode[question]
Random text
\startexercise
  Content of exercise 1
\stopexercise
More random text
\startexercise
  Content of exercise 2
\stopexercise
More random text
\stopnotmode\stoptext

Note that I have replaced \starttext with \starttext\startnotmode[question] and \stoptext with \stopnotmode\stoptext.

When you compile the file using context test.tex, then the output is:

enter image description here

When you compile using context --mode=question test.tex, then the output is:

enter image description here

1
  • Sorry for being slow. This is a very nice trick! When I first saw your example I did not think it would work with references or titles (of the exercise), but it does! This is an acceptable solution for me (I will not have to change a lot of code, but only some parts of the "pramble"), so I will accept your answer.
    – mickep
    Commented Nov 18, 2016 at 6:28

You must log in to answer this question.

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