2

I am writing a question and answer problem book.

I would like the questions and their answers to be proximal in the source, but, upon compilation, for the questions to all clump together in one section of the output and the answers to all clump together in a different section of the document.

For instance, the input might be:

Question 1
Answer 1
Question 2
Answer 2
Question 3
Answer 3

whereas the output might be:

Questions About The Calculus of Puppies
Answers About The Calculus of Puppies
Questions About The Algebra of Cats
Answers About The Algebra of Cats

Off-handedly, I think this could be achieved using floating environments and flushes, perhaps similar to what the endfloat package does. But perhaps there are better ways?

4
  • 1
    answers package is do for this ctan.org/tex-archive/macros/latex/contrib/answers
    – touhami
    Mar 29, 2015 at 20:37
  • 1
    I am not sure, but answers or exsheets, probsoln could be of help
    – user31729
    Mar 29, 2015 at 20:39
  • I don't think you want a float character of answers and questions, as the varying order might confuse readers ;-)
    – user31729
    Mar 29, 2015 at 21:04
  • @SoundsOfSilence, exsheets seems pretty well-suited to this task. I think I'll give it a try.
    – Richard
    Mar 29, 2015 at 21:30

1 Answer 1

4

This is a very, very small version of a quick - and dirty question/solution approach. The tcolorbox stuff is rather eye-candy then really needed. (Apart from advancing counters)

Each \Question has a mandatory argument (for the question) and a 3rd. optional argument for the solution content. The 1st. opt. argument is for options (not used here, due lack of time).

The solution is written to an external file named \jobname.sol, the question file is (again) not used here.

(Admittedly, I grabbed from a non-working package of mine, which is not ready to be published :-()

\documentclass{article}


\usepackage[many]{tcolorbox}
\usepackage{morewrites}
\usepackage{xparse}


\newcounter{QuestionBox}[section]

\newcounter{AnswerBox}[section]

\newtcolorbox[use counter=QuestionBox,number within=section]{QuestionBox}{title={Question~\theQuestionBox},colbacktitle={green},coltitle=black}

\newtcolorbox[use counter=AnswerBox,number within=section]{AnswerBox}{title={Answer~\theAnswerBox},colbacktitle={yellow},coltitle=black}


\newwrite\questionfile
\newwrite\solutionfile

\NewDocumentCommand{\Answer}{+O{}+m}{%
  \begin{AnswerBox}
    #2
  \end{AnswerBox}
}



\NewDocumentCommand{\Question}{+O{}+m+O{}}{%
\immediate\write\solutionfile{%
  \string\setcounter{section}{\number\value{section}}
  \string\setcounter{AnswerBox}{\number\value{QuestionBox}}
}
\begin{QuestionBox}
#2
\end{QuestionBox}

\IfValueTF{#3}{%
  \immediate\write\solutionfile{%
    \string\Answer{\unexpanded{#3}}
  }
}{}


}

\AtBeginDocument{%
  \immediate\openout\questionfile=\jobname.quest
  \immediate\openout\solutionfile=\jobname.sol
}


\NewDocumentCommand{\CollectSolutions}{+O{}}{%
  \clearpage
  \immediate\closeout\questionfile%
  \immediate\closeout\solutionfile%
  \setcounter{QuestionBox}{0}
  \setcounter{AnswerBox}{0}
  \section*{Solutions}
  \InputIfFileExists{\jobname.sol}{}{}
}

\AtEndDocument{%
  \immediate\closeout\questionfile%
}%

\begin{document}

\section{Relativity}

\Question{What's the meaning of \(E=mc^2\)}[It's the total relativistic energy (of a particle)]

\Question{Proof that \textcolor{red}{\[\gamma = \dfrac{1}{\sqrt{1-\dfrac{v^2}{c^2}}}\]}}[\textbf{It's completely straightforward}]

\section{Other stuff}
\Question{Find the anti-derivative of \[e^{-x^2}\]}[Ooops...]



\CollectSolutions

\end{document}

enter image description here

1
  • My solution is of course not as sophisticated as exsheets and other packages.
    – user31729
    Mar 29, 2015 at 21:52

You must log in to answer this question.

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