# How to sort text or how to make a tex-based test generator

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.

-
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 elfsoft2000.com/worksheets/index.htm – John Kormylo Sep 12 '13 at 3:33

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.

\documentclass{article}
\usepackage{bashful}
\begin{filecontents}{\jobname.dat}
PRef:1
$2+2=?$
PRef:2
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.
PRef:3
Is she right?
PRef:4
Why is there nothing rather than something?
PRef:5
Prove the Bishop wrong.
If the Bishop objects, consider the Queen's advice to Alice or take the train instead.
\end{filecontents}

\newwrite\writeme
\newcommand\princlude[1]{%
\immediate\openout\writeme=my.prefs%
\immediate\write\writeme{#1 }%
\immediate\closeout\writeme}

\AtEndDocument{%
\begin{enumerate}
\input{\jobname-pref.tex}
\end{enumerate}}

\begin{document}

\princlude{2 5 1}

\bash[stdoutFile=\jobname-pref.tex]
for i in $(cat my.prefs) do sed -n "/PRef:${i}/,/PRef:/p" prawf3.dat | sed -e "s/PRef:${i}/\\\\item /" -e '/^PRef:.*$/d'
done
\END

\end{document}


-

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 perltex.pl 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.

-