TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

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

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:
\item Part one \solution{Part 1 answer}
\item Part two \solution{Part 2 answer}

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

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
 First question.
  \item Part one
  \item Part two
 The answer to the first question.
  \item Part one
  \item Part two

% ID=2
 Second question.
 The answer to the second question.

% ID=3
 Third question.
 The answer to the third question.

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







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. – clemens 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. – clemens 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! – clemens Oct 24 '12 at 7:50

I usually do



question ?
the answer is...

next (sub) question ?
the answer is...


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


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.