Take the 2-minute tour ×
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.

I am trying to create a database of maths questions and answers in TeX form. The idea is that a simple command such as \getfromDB{101} will pull the text from the database of the question numbered 101. The solutions should be included in the text source with the question. Hence a typical entry in the database is:

What is the answer? \solution{This is it!}

A user can simply put the \getfromDB{101} text in their document as they like: say, within a list/enumerate/theorem, numbered whichever way they want. Now if all questions were like this, then I think that there are all sorts of options and packages available that can redefine the \solution macro the way any user would like.

But some questions come in parts:

This question has two parts:
\begin{parts}
\item Part one \solution{Part 1 answer}
\item Part two \solution{Part 2 answer}
\end{parts}

Again the idea is that the user can define the parts environment the way they like, but so far I haven't found a suitable package that can help with a suitable redefinition of the \solution macro.

I have looked at the following packages and so far I haven't been smart enough to solve my problem: answers and exsheets. Can anyone point me in an appropriate direction?

share|improve this question

3 Answers 3

This is not exactly using a database but exsheets can selectively include questions from an external file. Questions can be given IDs and can be included by ID. Suppose your exernal file looks like this:

% this is file questions-db.tex
% ID=1
\begin{question}[ID=1]
 First question.
 \begin{parts}
  \item Part one
  \item Part two
 \end{parts}
\end{question}
\begin{solution}
 The answer to the first question.
 \begin{parts*}
  \item Part one
  \item Part two
 \end{parts*}
\end{solution}

% ID=2
\begin{question}[ID=2]
 Second question.
\end{question}
\begin{solution}
 The answer to the second question.
\end{solution}

% ID=3
\begin{question}[ID=3]
 Third question.
\end{question}
\begin{solution}
 The answer to the third question.
\end{solution}

Then you could use these questions in a main document as follows:

\documentclass{scrartcl}
\usepackage{exsheets}

\usepackage[inline]{enumitem}
\newlist{parts}{enumerate}{1}
\newlist{parts*}{enumerate*}{1}
\setlist[parts,parts*]{label=(\alph*)}

\begin{document}

\includequestions[IDs={1,3}]{questions-db.tex}

\printsolutions

\end{document}

enter image description here

It is not quite clear where the problem with the different question parts is...

share|improve this answer
    
This doesn't solve my problem. The format of the question as given in the database is such that part questions have the solution attached per part. Your solution has a list of questions followed by a list of solutions, which breaks this link. –  Geoff Oct 24 '12 at 4:33
    
Ah, that wasn't clear from your question. You should edit it, I guess. Do you have these exercises in a real database like in MySQL or how does the format look like? Anyway, this answer of mine might be of intererst for you. –  cgnieder Oct 24 '12 at 7:20
    
The database exists, but is a work in progress, so I can modify it as necessary. My thinking is that the solution goes directly with the question; so if a question has parts, each part has its solution. Exsheets (AFAIK) needs the questions as one list and then the solutions as another. This isn't what is stored in the db. –  Geoff Oct 24 '12 at 7:42
    
You haven't understood what I meant: you need to describe (in your question) how exactly the database looks like. Is it a simple text file? If so, how is its format? A csv list? One needs to know this in order to write a command \getfromDB. –  cgnieder Oct 24 '12 at 7:49
    
exheets needs every solution after the question it belongs to. But my linked answer shows a way around that! –  cgnieder Oct 24 '12 at 7:50

I usually do

\usepackage{versions}
...
\newboolean{ShowSolutions}
\setboolean{ShowSolutions}{false}

\ifthenelse{\boolean{ShowSolutions}}
    {\includeversion{solution}}
    {\excludeversion{solution}}
\begin{document}

question ?
\begin{solution}
the answer is...
\begin{solution}

next (sub) question ?
\begin{solution}
the answer is...
\end{solution}

etc.

Works fine

share|improve this answer
up vote 0 down vote accepted

It seems that I couldn't find a suitable package. One solution would be simply writing the solutions into an external file and then calling that back in at the end. I asked about this at Write the value of enumerate to output file where one can find the appropriate solution.

share|improve this answer

Your Answer

 
discard

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.