Sign up ×
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.

Suppose I have a long file of a pile of test questions. I want to select a subset and make a test out of them. Of course I could copy/paste. But is there a package that helps to sort and display text? This is something like what bibtex does but I want to just insert text/questions by something like an "include", as in "include 95,22,43 from algebra.tex" to insert problems 95,22,43 from that file and enumerate them.

The closest items I found is this Automatically generate workbook from a sort of database and the answers package.

share|improve this question
exsheets does it. – Harish Kumar Sep 11 '13 at 8:10
@HarishKumar exsheets does not sort question but it allows to selectively include them from an external file. They're included in the order they're written in the external file, though. – clemens Sep 11 '13 at 8:48
@cgnieder Oho My bad. That is what I meant. Thanks for both (clarification and package) – Harish Kumar Sep 11 '13 at 10:08
I don't know if this is of any use to you, but my solution was to use a C program to fill in content and define TeX commands to do the formatting. See – John Kormylo Sep 12 '13 at 3:33

2 Answers 2

up vote 3 down vote accepted

This question is old but made me curious. This is probably not at all safe. That is, you would need to know what you were feeding the file.

This is certainly fragile and, since it uses external writes, inherently insecure.

Caveat emptor...

If \jobname.tex is the .tex file, \jobname.dat should contain the questions. Each should be preceded by a line PRef:<label>. The label must not contain weird characters, including spaces. \princlude{} should contain a list of questions in the required order, separated by spaces. So if you want questions 98, 1 and 33 in that order, \princlude{98 1 33}. Other files are created on-the-fly either by direct writing or via bashful.

If a chimpanzee has 5 bananas, 6 oranges and 2 coconuts, how many tomatoes does the nearest gorilla have?
Answers should be correct to 5 decimal places.
Is she right?
Why is there nothing rather than something?
Prove the Bishop wrong.
If the Bishop objects, consider the Queen's advice to Alice or take the train instead.

  \immediate\write\writeme{#1 }%



\princlude{2 5 1}

for i in $(cat my.prefs)
sed -n "/PRef:${i}/,/PRef:/p" prawf3.dat | sed -e "s/PRef:${i}/\\\\item /" -e '/^PRef:.*$/d'


selected questions

share|improve this answer

You can do this with a scripting language. I'm not aware of all the options for scripting languages usable with LaTeX, but I'm aware of PerlTeX, which allows you to create LaTeX macros and environments that run Perl code. You include \usepackage{perltex} in the preamble, then compile using at the command line.

So, for example, you could have a function that reads from a file of questions and randomly select one. You could start with the Perl code given here and modify it for LaTeX so that it typesets the line in place with your document. If you didn't want questions to be repeated, you would need to include checks for repeats or modify the code to find the first question, skip a random number to get the next question, and so on.

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.