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.

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}
Find the radius ...
\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 ...
  2. Find the radius ...

Trigonometry:

  1. The angle ...
share|improve this question
3  
Some special usage of BibTeX or Makeindex may do the stuff, I guess. –  Leo Liu Jul 31 '12 at 7:19
add comment

3 Answers 3

up vote 17 down vote accepted

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.

enter image description here

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}
    Find the radius ...
    \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}
share|improve this answer
add comment

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}
Find the radius ...
\end{pro}

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

\end{document}
share|improve this answer
add comment

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}
\usepackage[load-headings]{exsheets}
\SetupExSheets{
  % only place a number in front of the exercises instead of creating a title:
  headings=runin-nr,
  % default headings would be bold, so:
  headings-format=,
  % 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]
 Find the radius ...
\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}

enter image description here

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).

share|improve this answer
add comment

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.