我意识到这是一个相当开放式的问题,所以如果有人认为它不符合 StackOverflow 指南,可以将其删除。
编写数学教科书最繁琐且最容易出错的部分之一是检查练习答案。我想知道是否有人遇到过类似于乳胶单元测试框架的东西来检查练习问题的答案(目前是数字的)。
我想到过一种方法,就是将练习存储在其他地方,让其他软件检查它们,然后在编译 latex 文档时,将练习和解决方案与教科书合并。有人知道这样的事情吗?
答案1
你刚刚描述了sagetex
软件包和文档这里,它使你可以访问 Python 编程以及名为 Sage 的免费开源计算机代数系统 (CAS) 和文档这里。Sage 可以处理代数、微积分、矩阵、向量、多项式、图论等等。
这是我多年前为学习而设计的样本测试sagetex
。问题是根据我的要求随机创建的,并附有答案。
\documentclass[a4paper,addpoints,12point]{exam}%
% -----------------------------The preamble ------------------------------------
\usepackage{xcolor}
\usepackage{fix-cm}% allow for a bigger font
% mathematical typesetting, fonts, and symbols
\usepackage{amsmath, amsfonts, amssymb}
\usepackage[T1]{fontenc}% change the fonts
\usepackage[margin=.5in]{geometry}% sets the margins
\usepackage{sagetex}% use Sage for it's math ability
\usepackage{graphicx}% for including pictures
\usepackage{tikz} %tikz pictures, graph paper.
\pagestyle{empty} % remove the page numbers
\pagestyle{head}
\printanswers % If you want to print answers
%\noprintanswers % If you don't want to print answers
\addpoints % if you want to count the points
% \noaddpoints % if you don't want to count the points
% Specifies the way question are displayed:
%\qformat{\textbf{\thequestion.} \quad(\thepoints)\hfill}
\shadedsolutions % defines the style of the solution environment
% \framedsolutions % defines the style of the solution environment
\definecolor{SolutionColor}{rgb}{0.8,0.9,1} % light blue
% Defines the title of the solution environment:
\renewcommand{\solutiontitle}{\noindent\textbf{Solution:}\par\noindent}
% **************** BEGIN INPUT ASSIGNMENT INFO *******************
\newcommand{\course}{My Math Course}
\newcommand{\assignment}{My Test}
\newcommand{\teacher}{Mr. Ima Putz}
% **************** END INPUT ASSIGNMENT INFO *******************
\runningheadrule
\runningheader{\course}{\assignment}{\teacher}
\setlength{\parskip}{1.2ex} %space between paragraphs
\setlength{\parindent}{1em} %Paragraph indentation
\linespread{1.2} %spacing between lines
\widowpenalty = 10000 %penalty for a broken page
\newcommand\T{\rule{0pt}{2ex}} % \T will create extra space above (used to fix tables)
\newcommand\B{\rule[-1.5ex]{0pt}{0pt}}% \B will create extra space below (used to fix tables)
% -----------------------------End of the preamble ------------------------------
\begin{document}
\fontsize{17pt}{20pt}\selectfont %sets font size and line spacing
\noindent
\begin{tabular}{@{}l p{1.6in}r @{}}
Name: \vrule height 0 pt depth 0.4 pt width 3.0 in & & Period: \vrule height 0 pt depth 0.4 pt width .75 in \\\\
\teacher & &Date: \hrulefill \\
\end{tabular}
\begin{center}
The point value is given for each problem.\\ \vspace{.1in}
{\bf \assignment}
\end{center}
\noindent Show the solution to each problem. Put your answer in the space provided. If there isn't enough space, circle your answer.
% *********** BEGIN DEFINE VARIABLES ****************
\begin{sagesilent}
a = Integer(randint(6,9))
b = Integer(randint(7,10))
c = Integer(randint(2,5))
d = Integer(randint(2,5))
e = Integer(randint(1,4))
f = Integer(randint(1,4))
q1 = expand((x-a)*(x-c))
q2 = expand((x-b)*(x-d))
\end{sagesilent}
% *********** END DEFINE VARIABLES ****************
\begin{questions}
\bracketedpoints
\question[4]
Factor $\sage{q1}$.
\answerline
\vspace{0.85in}
\question[4]
Determine $\int_{0}^{\sage{e}}\sage{q1}\,dx$.
\answerline
\vspace{0.85in}
\question[5]
Factor $\sage{q2}$.
\answerline
\vspace{0.85in}
\question[6]
Find $\frac{d}{dx}\left(\sage{q2}\right)$.
\answerline
\newpage
\question[5]
Factor $\sage{a*b*c}$.
\answerline
\vspace{0.85in}
\question[4]
The table shows the number of voters in Mickey Mouse U. who were polled on their vote for Alex and Bob in the school election. Find the probability that a randomly selected person voted for Alex.
\\\\
\begin{sagesilent}
g=Integer(randint(23,37))
h=Integer(randint(31,42))
i=Integer(randint(19,45))
j=Integer(randint(19,40))
k=Integer(randint(15,36))
l=Integer(randint(24,34))
\end{sagesilent}
\begin{tabular}{c|c|c|c}
& Sophomores & Juniors & Seniors\\
\hline
Alex & $\sage{g}$ & $\sage{h}$ & $\sage{i}$\\
\hline
Bob & $\sage{j}$ & $\sage{k}$ & $\sage{l}$\\
\hline
\end{tabular}
\answerline
\vspace{0.85in}
\question[6]
Find the distance between the points $(\sage{a},\sage{b})$ and $(\sage{c},\sage{e})$.
\newpage
\begin{center}
{\large Answers}
\end{center}
\begin{solution}
$x=\sage{a}$, and $x=\sage{c}$
\end{solution}
\begin{solution}
$\sage{integrate(q1,(x,0,e))}$
\end{solution}
\begin{solution}
$x=\sage{b}$, and $x=\sage{d}$
\end{solution}
\begin{solution}
$\sage{diff(q2)}$
\end{solution}
\begin{solution}
$\sage{factor(a*b*c)}$
\end{solution}
\begin{solution}
$\sage{(g+h+i)/(g+h+i+j+k+l)}$
\end{solution}
\begin{solution}
$\sage{sqrt((a-c)^2+(b-e)^2)}$
\end{solution}
\end{questions}
\end{document}
在Cocalc中运行的结果是:
第三页的答案是:
注意积分问题的答案:
$\sage{integrate(q1,(x,0,e))}$
。Sage 算出了答案,从而避免了错误。它还给出了分数形式的答案,而不是小数。
Sage 不包含在您的 LaTeX 发行版中。有两种主要方式可以访问它。一种是将其下载到您的计算机并使其与 LaTeX 配合使用。第二种方式最简单:获取免费可钙帐户。
搜索此网站以获取sagetex
示例。我对获取矩阵转置的回答这里显示了您可以多么轻松地获得正确答案并据此进行计算。Sage 可以绘制图形,并且可以强制通过 tikz 输出以使其看起来更好:请参阅我对 Riemann Zeta 图的回答这里。
答案2
这是我正在使用的骨架示例。
我在单独的文件中保存了一些通用函数mylib.lua
:
function math.round_int(x)
return x>=0 and math.floor(x+0.5) or math.ceil(x-0.5)
end
function printint(x)
return tex.print(math.round_int(x))
end
function math.round(x, n)
return math.round_int(x*10^n)/10^n
end
function printrd(x, n)
y = math.round(x, n)
return tex.print(string.format("%." .. n .. "f", y))
end
function printe(s, x)
return tex.print(string.format("%" .. s .. "e", x))
end
将函数保存在单独的文件中更加清晰,而且还避免了%
在 Lua 和 LaTeX 中字符具有不同含义的问题。
下面是使用这些函数的示例文件:
\documentclass{article}
\usepackage{mathtools}
\usepackage{luacode}
\directlua{require("mylib.lua")}
\usepackage{siunitx}
\directlua{
eps0 = 8.85e-12
S = 1e-4
e = 0.012
}
\begin{document}
Compute the capacitance of a capacitor with surface $S=\qty{\directlua{printint(S*1e4)}}{mm^2}$ and thickness $e=\qty{\directlua{printrd(e, 3)}}{m}$.
\begin{align}
C
&=\frac{\varepsilon_0S}{e}\\
&=\qty{\directlua{
C = eps0*S/e
printe("1.1e", C)
}}{F}
\end{align}
\end{document}