TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

My document consists (among other things) of blocks that look like this:




I would like to define the Solution environment so that, upon setting a switch, the contents of the block is included as is (with LaTeX processing, of course), or is omitted completely if the last digit (the "3" above) is even. In addition, the inserted text, if it is inserted, should be labeled with the full string ("1.2.3") in the above example. I don't know TeX well enough to even know where to start. Anyone willing to help?

share|improve this question
What is the format of the argument to the Solution environment? Does it always contain just a single comma? What do the numbers represent? The more information you give, the easier it is to help. – TH. Sep 13 '10 at 10:14
Yes, at most one comma. {x.y.z,a.b.c} means that the exercise was numbered x.y.z in the previous edition but is now numbered a.b.c. If x.y.z is not present (comma only), then a.b.c is a new exercise in this edition. – rogerl Sep 13 '10 at 12:30

The solution to your question probably involves a number of three smaller subproblems:

  1. Scanning the input argument to find the last number.
  2. Testing whether a number is even or not.
  3. Making (La)TeX ignore a block of code.

For the first one you probably need some TeX hackery, maybe something like this

\def\scan#1.#2.{\ifx#2\relax\def\next{#1}\else\def\next{\scan #2.}\fi\next}

For the second task the ifthen package provides an \iffodd which might do the trick.

For the final and third task take a look at the verbatim package which, among other things, defines a comment environment to ignore blocks of LaTeX code.

share|improve this answer

As for the 3rd sub-problem mentioned by Juan: Making (La)TeX ignore a block of code.

Having TeX ignore a block of code isn't all that difficult, somehow causing it to be executed anyway might proof more difficult. I guess that one way to accomplish it might be to scan ahead to the \end{Solution} directive and put all tokens you encounter along the way in a macro, which optionally gets expanded later on. I doubt that's a very easy endeavour though.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.