我是一名教师,我使用 准备家庭作业\documentclass{exam}
。我知道当我编译为 PDF 时,我可以使用\documentclass[answers]{exam}
它来生成包含解决方案的编译 PDF。
最近有一位学生向我索要一个 TeX 模板,他们可以用它来写入他们的解决方案。
当然,我不想提供所有需要的 TeX——因为它已经有了我的解决方案,如果他们已经有了答案,那么这个任务就不是什么大问题了!
相反,我想将我的 TeX“编译”成学生模板 TeX 文件,他们可以编辑、编译成自己的 PDF,然后提交。
是否有一个简单的工具,可以获取用考试文档类准备的 TeX 文档并生成另一个仅包含问题的 TeX 文件(即源代码)?
答案1
这将创建一个 LaTeX 文件来添加问题的答案,使用相同的文件名并附加答案(例如 test.tex 和 testAnswer.tex)。
您需要\AnswerSpace
在每个问题后添加,并为答案保留空间。可惜的是,您无法使用,\vfill
因为在页面完成之前距离不可用。
\documentclass{exam}
%%%%%%%%%%%%%%%%%%%%%%%%%%
\AtBeginDocument{\newwrite\ExamAnswer
\immediate\openout\ExamAnswer{\jobname Answer.tex}%
\immediate\write\ExamAnswer{\string\documentclass{exam}}%
\immediate\write\ExamAnswer{\string\usepackage{graphicx}}%
\immediate\write\ExamAnswer{\string\AddToHook{shipout/background}%
{\string\put(0pt,-\the\paperheight){\string\includegraphics[page=\string\thepage]{\jobname.pdf}}}}%
\immediate\write\ExamAnswer{\string\begin{document}}%
}
\AtEndDocument{\immediate\write\ExamAnswer{\string\end{document}}%
\closeout\ExamAnswer
}
\newcommand\AnswerSpace{\par\immediate\write\ExamAnswer{\string\par
\string\ifnum\string\value{page}<\thepage\string\relax \string\newpage\string\fi
\string\vspace*{\string\dimexpr\the\pagetotal-\string\pagetotal}Answer \thequestion\string\par}}
%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
\begin{questions}
\question
How high is up?
\AnswerSpace
\vspace{1in}
\question
Where have all the flowers gone?
\AnswerSpace
\end{questions}
\end{document}
答案2
好的,这是我选定的路线。它使用 vim 的多行正则表达式匹配来删除我在 each\begin{solution}
和之间写的所有内容\end{solution}
。
生成文件:
TEX=pdflatex -halt-on-error -interaction=nonstopmode
example-%.pdf: example-%.tex
$(TEX) example-$*
$(TEX) example-$*
$(TEX) example-$*
template-%.pdf: template-%.tex
$(TEX) example-$*
$(TEX) example-$*
$(TEX) example-$*
template-%.tex: example-%.tex
cat example-$*.tex | vim -c ":%s/\\\\begin{solution}\_.\{-}\\\\end{solution}/\\\\begin{solution}\\\\end{solution}/g" -c "w! template-$*.tex" -c ":q!" -
然后,例如-01.tex 我可以放
\documentclass[answers]{exam}
\usepackage{amsmath,amssymb}
\usepackage{bm}
\usepackage[
colorlinks=true,
allcolors=blue
]{hyperref}
\begin{document}
\title{Homework 01 Example for Template}
\author{\href{https://tex.stackexchange.com/users/27873/evanb}{evanb}}
\date{Due Date Goes Here}
\maketitle
\begin{questions}
\question Recall Euler's identity, $e^{i\theta} = \cos\theta + i \sin \theta$.
\begin{parts}
\part Express $\cos\theta$ in terms of $e^{+i\theta}$ and $e^{-i\theta}$.
\begin{solution}
Cosine is even and sine is odd.
Add the two exponentials together to get
\begin{align*}
e^{+i\theta} + e^{-i\theta}
&= (\cos \theta + i \sin \theta) + (\cos (-\theta) + i\sin(-\theta)) \\
&= (\cos \theta + i \sin \theta) + (+\cos \theta - i\sin\theta) \\
&= 2 \cos\theta + 0 i \sin \theta
\end{align*}
Divide both sides to find
\begin{equation*}
\cos\theta = \frac{e^{+i\theta} - e^{-i\theta}}{2}
\end{equation*}
\end{solution}
\part Express $\sin\theta$ in terms of $e^{+i\theta}$ and $e^{-i\theta}$.
\begin{solution}Just as in the previous part, but subtract.
\begin{equation*}
\sin\theta = \frac{e^{+i\theta} - e^{-i\theta}}{2i}
\end{equation*}
One of the most common careless mistakes is to forget the $i$ downstairs!
\end{solution}
\part Using the expressions you found above, show $\cos(\alpha+\beta) = \cos(\alpha)\cos(\beta) - \sin(\alpha)\sin(\beta)$.
\begin{solution}
Just expand! The left hand side is
\begin{equation*}\cos(\alpha+\beta) = \frac{e^{+i(\alpha+\beta)} + e^{-i(\alpha+\beta)}}{2}\end{equation*}
The right-hand side is
\begin{align*}
\cos\alpha \cos\beta - \sin\alpha\sin\beta
&= \left(\frac{e^{+i\alpha}+e^{-i\alpha}}{2}\right)
\left(\frac{e^{+i\beta}+e^{-i\beta}}{2}\right)
- \left(\frac{e^{+i\alpha}-e^{-i\alpha}}{2i}\right)
\left(\frac{e^{+i\beta}-e^{-i\beta}}{2i} \right)\\
&= \frac{e^{+i(\alpha+\beta)} + e^{+i(\alpha-\beta)} + e^{-i(\alpha-\beta)} + e^{-i(\alpha+\beta)}}{4} \\
&\phantom{=} -\frac{e^{+i(\alpha+\beta)} - e^{+i(\alpha-\beta)} - e^{-i(\alpha-\beta)} + e^{-i(\alpha+\beta)}}{4i^2} \\
&= \frac{e^{+i(\alpha+\beta)} + e^{-i(\alpha+\beta)}}{2}
\end{align*}
after a lot of cancellation and simplification (don't forget the $i^2$ in the basement of the sine term!). I know personally I can never remember the angle-addition formulas. So here is a way to derive them!
\end{solution}
\end{parts}
\question Solve these simple differential equations. Specify what parts of your solutions are arbitrary constants. Explain where this differential equation might be useful and give the constants a meaning if you can.
\begin{parts}
\part $y' = m$ with $m$ constant and $x$ the independent variable.
\begin{solution}
It's a line.
$y(x) = mx+b$ with $b$ that can be set by the $y$ intercept of the line (for example).
\end{solution}
\part $\ddot{y} = -g$ with $g$ constant and $t$ the independent variable.
\begin{solution}
$y(t) = y_0 + v_0 t - \frac{1}{2}gt^2$ where $v_0$ and $y_0$ are arbitrary constants. They are fixed by the initial conditions. This equation arises when something falls under the influence of constant gravity. Then, $y_0$ could be the initial height and $v_0$ the initial velocity.
To show that this is the solution, you can integrate it. Or, you can `guess' it and just plug it in!
\end{solution}
\end{parts}
\end{questions}
\end{document}
可以使用 将其编译为 PDF,make example-01.pdf
作为任务或键(带或不带[answers]
)documentclass
。也可以使用make template-01.tex
来获取新文件(template-01.tex
),
\documentclass[answers]{exam}
\usepackage{amsmath,amssymb}
\usepackage{bm}
\usepackage[
colorlinks=true,
allcolors=blue
]{hyperref}
\begin{document}
\title{Homework 01 Example for Template}
\author{\href{https://tex.stackexchange.com/users/27873/evanb}{evanb}}
\date{Due Date Goes Here}
\maketitle
\begin{questions}
\question Recall Euler's identity, $e^{i\theta} = \cos\theta + i \sin \theta$.
\begin{parts}
\part Express $\cos\theta$ in terms of $e^{+i\theta}$ and $e^{-i\theta}$.
\begin{solution}\end{solution}
\part Express $\sin\theta$ in terms of $e^{+i\theta}$ and $e^{-i\theta}$.
\begin{solution}\end{solution}
\part Using the expressions you found above, show $\cos(\alpha+\beta) = \cos(\alpha)\cos(\beta) - \sin(\alpha)\sin(\beta)$.
\begin{solution}\end{solution}
\end{parts}
\question Solve these simple differential equations. Specify what parts of your solutions are arbitrary constants. Explain where this differential equation might be useful and give the constants a meaning if you can.
\begin{parts}
\part $y' = m$ with $m$ constant and $x$ the independent variable.
\begin{solution}\end{solution}
\part $\ddot{y} = -g$ with $g$ constant and $t$ the independent variable.
\begin{solution}\end{solution}
\end{parts}
\end{questions}
\end{document}
包含所有解决方案的空间(带有\begin{solution}\end{solution}
),但没有我准备的任何解决方案。该文档可以通过以下方式同样很好地编译make template-01.pdf
因此,我可以将其分发给template-01.tex
学生,以便他们可以编写自己的答案的 PDF。