家庭作业生成

家庭作业生成

可能重复:
条件排版/构建

我有一个 LaTeX 家庭作业问题数据库,如下所示:

question1

question2

question3

etc

是否可以创建一个标志来选择问题?例如:

flag question1

question2

flag question2

flag question3

答案1

目前还不清楚您想如何使用它,但这里有一个建议:创建一个\Question环境,在其中用唯一的名称命名每个问题:

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

然后在命令中提供您想要打印的问题列表,例如:

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

笔记:

  • 每个问题名称\ListOfQuestions 必须以 结束,并且需要,尾随。%

下面我创建了一个单独的foo.tex文件,其中包含所有问题的列表。此文件可以单独编译以生成一个文件全部问题:

在此处输入图片描述

此文件中问题的顺序决定了问题的输出顺序。定义为\ListOfQuestions

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

输出为:

在此处输入图片描述

代码:

\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}

相关内容