# Homework generation [duplicate]

Possible Duplicate:
Conditional typesetting / build

I have a database of LaTeX homework questions like this:

question1

question2

question3

etc


Is it possible to create a flag to select questions? Example:

flag question1

question2

flag question2

flag question3

-

## marked as duplicate by Martin Schröder, percusse, Tom Bombadil, Torbjørn T., lockstepSep 2 '12 at 12:41

It would be helpful if you could create a minimal example that shows some LaTeX code of what you're trying to achieve. For example, is your "flag" a symbol or just a boolean variable. Your "database", does that include reading things from a file, or just manually typing (say) question environments: \begin{question} ... \end{question}? –  Werner Aug 9 '12 at 23:52
What do you mean with "flag to select"? Do you want to be able to extract some predefined questions from the database? –  Gonzalo Medina Aug 10 '12 at 0:08
@GonzaloMedina, Yes! –  Papiro Aug 10 '12 at 0:10

It is not really clear how you want to use this, but here is one suggestion: Create a \Question environment where you name each question with a unique name:

\begin{Question}{<Name of Question>}
... Text of question goes here ...
\end{Question}


Then provide the list of questions you want printed in a command such as:

\newcommand*{\ListOfQuestions}{%
<Name of Question>,%
<Name of Another Question>,%
<Name of Some Other Question>,%
}%


## Notes:

• Each question name in the \ListOfQuestions must be terminated with a , and a trailing % is necessary.

Below I have created a separate foo.tex file which contains the list of all questions. This file can be compiled separately to generate a file with all the questions:

The order of the questions in this file determines the order that the questions are output. With the \ListOfQuestions defined as:

\newcommand*{\ListOfQuestions}{%
Algebra,%
%Trig Identity,%
}%


the output is:

## Code:

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

%\usepackage{filecontents}% Commented out for safety: ensure an existing file is not overwritten.
\begin{filecontents*}{foo.tex}
\documentclass{article}
\usepackage{environ}

\NewEnviron{Question}[1]{% #1 = Title for question
\noindent\textbf{Q: #1}%
\par\medskip%
\BODY%
}%

\begin{document}
\begin{Question}{Trig Identity}
Solve for $\theta$: $\sin (3 \theta) = \cos \theta$
\end{Question}
% ------------
\begin{Question}{Algebra}
Solve for $x$: $3x^2 + 4x - 7 = 0$
\end{Question}
% ------------
Compute radius of circle: $13x^2 + 13y^2 - 52 = 0$
\end{Question}
\end{document}
\end{filecontents*}

\newcommand{\IfStrContains}[4]{%
% #1 = main string
% #2 = sub-string to search for
% #3 = code to execute if sub-string is in main string
% #4 = code to execute if sub-string is not in main string
\StrPosition{#1}{#2}[\PositionOfSubString]%
\IfEq{\PositionOfSubString}{0}{#4}{#3}%
}%

\newcommand*{\ListOfQuestions}{%
Algebra,%            These MUST all terminate with a comma and have a
Radius of Circle,%   trailing % characters at the end of the line
%Trig Identity,%
}%

\NewEnviron{Question}[1]{% #1 = Title for question
\IfStrContains{,\ListOfQuestions,}{,#1,}{%