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.

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
share|improve this question

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

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

3  
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
    
@Werner, Sorry. My database is a regular LaTeX file. –  Papiro Aug 9 '12 at 23:53
    
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

1 Answer 1

up vote 3 down vote accepted

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:

enter image description here

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

\newcommand*{\ListOfQuestions}{%
    Algebra,%
    Radius of Circle,% 
    %Trig Identity,%
}%

the output is:

enter image description here

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
       \par\addvspace{\bigskipamount}%
        \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}
        % ------------
        \begin{Question}{Radius of Circle}
            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,}{%
        \par\addvspace{\bigskipamount}%
        \noindent\textbf{Q: #1}%
        \par\medskip%
        \BODY%
    }{}%
}%

\begin{document}
    \include{foo}
\end{document}
share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.