1

There are different packages out there to add answers to exercises to a document, like answers and exercise, however they're all a bit overkill, e.g. they re-format and re-number the list of exercises in my document.

What I want to achieve is juist this: I have a list of exercises, which are formatted in a standard enumerate-environment, and I want to put the solutions just underneath, but in the beginning of my document I want to be able to define some kind of 'flag'. If the flag is 'on', then the document should print out my lecture notes with solutions, if it's 'off', it should just skip the solutions and print out the document as if the solutions were never there (so also not leave blank space or something similar.

Ideally, something like this:

List of exercises:

\begin{enumerate}
\item 2+2
  \begin{solution}
  4
  \end{solution}
\item 3-1
  \begin{solution}
  2
  \end{solution}
\item 5+2
  \begin{solution}
  7
  \end{solution}
\end{enumerate}

If the flag is off, all solutions should be downright skipped.

Is there a way to do this, preferably as simple as possible?

  • Take a look at the comment package, it should suit your needs. – Alexis Nov 6 '16 at 9:28
1

Without an environment, simply \solution:

\documentclass{article}
\newif\ifsolution
\def\solution#1{\ifsolution #1 \else \relax\fi}

\begin{document}

\solutiontrue

\begin{enumerate}

\item 2+2

\solution{4}
\item 3-1

\solution{2}
\item 5+2

\solution{7}
\end{enumerate}

\solutionfalse

\begin{enumerate}
\item 2+2

\solution{4}
\item 3-1

\solution{2}
\item 5+2

\solution{7}
\end{enumerate}

\end{document}

enter image description here

| improve this answer | |
  • Yes, that's what I'm looking for! Thanks a lot! :) – Werner Nov 6 '16 at 14:52

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.