# Is there a package that can automatically sort items based on tags?

For example, I write a problem booklet with a ton of problems. I don't want to use a database. In the input file, I write as follows.

\pro{geometry}
Find the area of ...
\endpro

\pro{trigonometry}
The angle ...
\endpro

\pro{algebra}
Prove that $x^2+1=0$ has no real solution.
\endpro

\pro{geometry}
\endpro


But the render output in PDF, the problems are grouped based on the tags. For example,

Algebra:

1. Prove that $x^2+1=0$ has no real solution.

Geometry:

1. Find the area of ...

Trigonometry:

1. The angle ...
-
Some special usage of BibTeX or Makeindex may do the stuff, I guess. –  Leo Liu Jul 31 '12 at 7:19

This doesn't "sort" based on tags, but does achieve the desired result.

The problems are placed in an external file and are input, once for each subject, and only the environments that match the current subject are printed.

## Notes:

• There must be at least one question for each subject.
• I made the titles and the subject the same to simplify things, but this code can be easily adapted to the case where, for example the subject is trig, and the title is Trigonometry.

## Code:

\documentclass{article}
\usepackage{pgffor}
\usepackage{xstring}
\usepackage{environ}

\usepackage{filecontents}
\begin{filecontents*}{foo.tex}
\begin{pro}{Geometry}
Find the area of ...
\end{pro}

\begin{pro}{Trigonometry}
The angle ...
\end{pro}

\begin{pro}{Algebra}
Prove that $x^2+1=0$ has no real solution.
\end{pro}

\begin{pro}{Geometry}
\end{pro}
\end{filecontents*}

\NewEnviron{pro}[1]{%
\IfStrEq{#1}{\CurrentSubject}{\item \BODY}{}
}%

\newcommand*{\CurrentSubject}{}%

\begin{document}
\foreach \Title in {Algebra, Geometry, Trigonometry} {%
\renewcommand*{\CurrentSubject}{\Title}%
\section*{\CurrentSubject}
\begin{enumerate}
\input{foo}
\end{enumerate}
}%
\end{document}

-

You can accumulate the exercises in various token registers:

\documentclass{article}
\usepackage{environ}
\makeatletter
\newcommand{\check@pro}[1]{%
\@ifundefined{pro@#1}{\create@pro{#1}}{}%
}
\newcommand\create@pro[1]{%
\expandafter\newtoks\csname pro@#1\endcsname
}
\NewEnviron{pro}[1]{%
\check@pro{#1}%
\global\csname pro@#1\endcsname=\expandafter{\the\csname pro@#1\expandafter\endcsname
\expandafter\item\BODY}%
}
\makeatother
\newcommand{\printpro}[1]{%
\section*{#1}
\begin{enumerate}
\the\csname pro@#1\endcsname
\end{enumerate}
}

\begin{document}

\begin{pro}{Geometry}
Find the area of ...
\end{pro}

\begin{pro}{Trigonometry}
The angle ...
\end{pro}

\begin{pro}{Algebra}
Prove that $x^2+1=0$ has no real solution.
\end{pro}

\begin{pro}{Geometry}
\end{pro}

\printpro{Algebra}
\printpro{Geometry}
\printpro{Trigonometry}

\end{document}

-

The following answer probably comes too late for your actual use case but maybe it is of interest to others.

The exsheets package can be used here. The syntax would be slightly different, though. The package let's you assign topics to questions and you can then choose which topic should be used.

\SetupExSheets{use-topics=<some topic>}
\begin{question}[topic=<some topic>]
Some question...
\end{question}
\begin{question}[topic=<some other topic>]
Some other question...
\end{question}


Here only the first question would be typeset.

When the questions are placed in a separate file this can be used to achieve the desired result. The exercises won't be sorted, though, but included in the order given in the file.

exsheets has a command \includequestions that includes questions/exercises from an external file which in the example below isn't stricly necessary, though, -- a simple \include would suffice. With unnumbered section titles one would need to reset the question counter manually. In any case one could easily define some wrapper macro for creating a heading, possibly resetting the counter, and including the questions according to a topic.

\documentclass{scrartcl}
\SetupExSheets{
% only place a number in front of the exercises instead of creating a title:
% default headings would be bold, so:
% reset question' counter with a new section:
counter-within=section
}

% the external file:
\usepackage{filecontents}
\begin{filecontents}{\jobname-exercises.tex}
\begin{question}[topic=geometry]
Find the area of ...
\end{question}
\begin{question}[topic=trigonometry]
The angle ...
\end{question}
\begin{question}[topic=algebra]
Prove that $x^2+1=0$ has no real solution.
\end{question}
\begin{question}[topic=geometry]
\end{question}
\end{filecontents}

\begin{document}

\section{Algebra}
\SetupExSheets{use-topics=algebra}
\includequestions{\jobname-exercises}

\section{Geometry}
\SetupExSheets{use-topics=geometry}
\includequestions{\jobname-exercises}

\section{Trigonometry}
\SetupExSheets{use-topics=trigonometry}
\includequestions{\jobname-exercises}

\end{document}
`

It would be relatively easy to add solutions to the exercises and print them in a different section afterwards (in which case a less ambiguous question numbering would be favourable, though).

-