用于创建新 .tex 文件的宏

用于创建新 .tex 文件的宏

我借用了将指定环境中的文本内容提取到新文件中一些代码可以从文件中提取行.tex并将其放入新文件中。代码运行良好,但我想创建一个循环来执行此操作以解决一系列测试问题。在下面的示例中,用户将新环境命名为:test,宏提取文本并将其放入新文件中。我希望能够定义多个环境并使用宏为每个环境生成不同的文件。

\documentclass{article}
\usepackage{environ}

\newwrite\myexportone
\newwrite\myexporttwo

\makeatletter
\NewEnviron{test1}{%
\toks@=\expandafter{\BODY}%
\immediate\write\myexportone{\the\toks@}}
\makeatother

\makeatletter
\NewEnviron{test2}{%
\toks@=\expandafter{\BODY}%
\immediate\write\myexporttwo{\the\toks@}}
\makeatother

\begin{document}
\immediate\openout\myexportone=export1.tex
\immediate\openout\myexporttwo=export2.tex

\begin{test1}

\newproblem{

\FPsetpar{a}{-10}{10}[{-1,1}]
\FPsetpar{b}{-10}{10}[{-1,1}]

\item  If 

\begin{equation*}  \a (x \dsign{\b} \b)=\a x+m \end{equation*} what is the value of $m$?

\vspace{10 mm}
  \begin{answers}{2}\bChoices[random]
    \Ans1 \FPsv{a*b} \eAns
    \Ans0 \FPsv{-a*b} \eAns   \eFreeze
    \Ans0 \a \eAns  \eFreeze
    \Ans0 \b \eAns
    \eChoices\end{answers}
}

\end{test1}

\begin{test2}

\newproblem{
\FPsetpar{a}{1}{10}

\item  What is the solution set for $x^2-\FPsv{\a^2}=0$?

\vspace{10 mm}
  \begin{answers}{2}\bChoices[random]
    \Ans0 $\FPsv{a}i$ \eAns
    \Ans0 $\pm\FPsv{a}i$ \eAns
    \Ans1 $\pm\FPsv{a}$ \eAns
    \Ans0 $\FPsv{a}$ \eAns
    \eChoices\end{answers}
}

\end{test2}

\end{document}

答案1

目前尚不完全清楚您想要什么,但也许是这个。

您不需要单独的输出流,您可以对所有文件重复使用同一个输出流。

这定义了一个采用逗号分隔的环境名称列表的命令。

\documentclass{article}
\usepackage{environ}

\newwrite\myexport



\makeatletter
\def\myenvs#1{%
\@for\mytmp:=#1\do{%
\show\mytmp
\expandafter\myenv\expandafter{\mytmp}%
}}

\def\myenv#1{\NewEnviron{#1}{%
\toks@=\expandafter{\BODY}%
\immediate\openout\myexport=\jobname-export-#1.tex %
\immediate\write\myexport{\the\toks@}}%
\immediate\closeout\myexport}%

\makeatother

\myenvs{test1,test2}

\begin{document}


\begin{test1}

\newproblem{

\FPsetpar{a}{-10}{10}[{-1,1}]
\FPsetpar{b}{-10}{10}[{-1,1}]

\item  If 

\begin{equation*}  \a (x \dsign{\b} \b)=\a x+m \end{equation*} what is the value of $m$?

\vspace{10 mm}
  \begin{answers}{2}\bChoices[random]
    \Ans1 \FPsv{a*b} \eAns
    \Ans0 \FPsv{-a*b} \eAns   \eFreeze
    \Ans0 \a \eAns  \eFreeze
    \Ans0 \b \eAns
    \eChoices\end{answers}
}

\end{test1}

\begin{test2}

\newproblem{
\FPsetpar{a}{1}{10}

\item  What is the solution set for $x^2-\FPsv{\a^2}=0$?

\vspace{10 mm}
  \begin{answers}{2}\bChoices[random]
    \Ans0 $\FPsv{a}i$ \eAns
    \Ans0 $\pm\FPsv{a}i$ \eAns
    \Ans1 $\pm\FPsv{a}$ \eAns
    \Ans0 $\FPsv{a}$ \eAns
    \eChoices\end{answers}
}

\end{test2}

\end{document}

相关内容