1

I'm trying to create some exercises for students. So the idea was, to use a command which just makes the solution white, if the solution counter is set to 0 and which shows the solution if the counter is set to 1.

(In addition I created a box which shows squared paper (for calculations) if counter is set to 0 and a framed box with white background and the solution if counter set to 1; but I want to keep it simple for the moment, so just focus on the command)

So here is a minimal working example of the command:

\documentclass[12pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage{xcolor}

\newcounter{wf}
\setcounter{wf}{1}

\newcommand{\whf}[1]{%
\ifnum\value{wf}=0%
    \textit{#1}%
\else%
    \textcolor{white}{\itshape #1}%
\fi%
}

\begin{document}
The following word is white sometimes: \whf{Test}, hope it works.
\end{document}

It works quite well and was fine, but:

Now I want to create an overview over all solutions. So I just want to show the solutions without showing the exercises. Best I can hope for is something like: "Show nothing except the things in the \whf command" or so.

I have created tons of exercises right now and I hope there's a solution where I

  1. don't have to work on every single exercise again and

  2. have the solutions compact together and not spread over a lot of pages

  3. (bonus) the number of the exercises (another counter) are shown together with the solution according to the exercise

So I hope my english is not too bad, and sorry for the long description. I hope you understand my problem and may offer me a solution.

Thank you.

  • 2
    Is there anything in your text that indicated what is the solution part? If not, then of course you will have to do some work on every exercise. LaTeX cannot guess what part is the exercise and what part is the solution. Please give as a minimal working example (MWE). – Piet van Oostrum Nov 19 '16 at 10:41
  • The solution part is inside a command. But the exercise text not. So the question was if you can extract the parts inside the commands. You can see this in the MWE I already posted above. There the solution is in the \whf{} command – buecki Nov 20 '16 at 18:49
3

If the solutions are just words, not involving TeX code, you can write the solutions to a separate file. To open the file, use

\newwrite\solfile
\immediate\openout\solfile\jobname Solutions.tex

\jobname holds the name of the current main TeX file, so the file with the solutions will be named XXXSolutions.tex, if the main file is called XXX.tex.

To write to the file, use

\immediate\write\solfile{...}

This will write the stuff between the braces to \solfile. The stuff is expanded, so you have to take precautions that the right tokens end up in the file.

The code below, put into a file test.tex, will generate the file testSolutions.tex containing the single line

\solution {Test}

You can format the answers by defining the macro \solution.

Your code with the file commands:

\documentclass[12pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage{xcolor}

\newcounter{wf}
\setcounter{wf}{1}

\newwrite\solfile                               % <<<<<<<<<<<<<<<<<<<<<<
\immediate\openout\solfile\jobname Solutions.tex% <<<<<<<<<<<<<<<<<<<<<<
\newcommand{\whf}[1]{%
\ifnum\value{wf}=0%
    \textit{#1}%
\else%
    \textcolor{white}{\itshape #1}%
\fi%
\immediate\write\solfile{\noexpand\solution{#1}}% <<<<<<<<<<<<<<<<<<<<<<
}

\begin{document}
The following word is white sometimes: \whf{Test}, hope it works.
\end{document}
  • Thank you, that seems to be a good approach. I will take some further research on this, to see if this can fit my other criteria. – buecki Nov 20 '16 at 18:54

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.