我刚开始学习 probsoln 包,以便可以创建问题集。我有一个可以运行的基本示例:
\documentclass{article}
\usepackage{probsoln}
\showanswers
\begin{document}
\ifthenelse{\boolean{showanswers}}{\textbf{Solution Sheet}}{\textbf{Homework problems}}
\loadallproblems{HW_Descriptive.rnw}
\useproblem{comparing_rates1}
\end{document}
问题集 HW_Descriptive.rnw 包含
\begin{defproblem}{comparing_rates1}
If 136,670,000 persons were wage and salary workers and 10,544,000 persons were self-employed, what is the fatal injury rate per 100,000 workers for each group?
\begin{onlysolution}%
\begin{solution}
Wage and salary workers have a fatal injury rate per 100,000 workers of 136670000/100000 = 1366.7
Self-employed workers have a fatal injury rate per 100,000 workers of 10544000/100000 = 105.44
\end{solution}
\end{onlysolution}
\end{defproblem}
我希望将 R 代码纳入问题中(有时在问题中,最常见的是在解决方案中)。不幸的是,最直接的方法是
\begin{defproblem}{comparing_rates2}
If 136,670,000 persons were wage and salary workers and 10,544,000 persons were self-employed, what is the fatal injury rate per 100,000 workers for each group?
\begin{onlysolution}%
\begin{solution}
<<>>=
136670000/100000
10544000/100000
@
\end{solution}
\end{onlysolution}
\end{defproblem}
没有工作,输出看起来像
解决方案:!!??=136670000/100000 10544000/100000 @
有人能指出我一个 Sweave + probsol 的实际示例吗?
答案1
这不是问题probsoln
。如果您尝试以下操作:
\documentclass{article}
\begin{document}
<<>>=
136670000/100000
10544000/100000
@
\end{document}
你得到
这只是 LaTeX 使用默认字体的默认行为。
如果您使用以下选项,则可以在问题定义中逐字使用fragile
:
\documentclass{article}
\usepackage{probsoln}
\showanswers
\begin{defproblem}{comparing_rates2}[fragile]
If 136,670,000 persons were wage and salary workers and 10,544,000 persons were self-employed, what is the fatal injury rate per 100,000 workers for each group?
\begin{onlysolution}[fragile]%
\begin{solution}
\begin{verbatim}
<<>>=
136670000/100000
10544000/100000
@
\end{verbatim}
\end{solution}
\end{onlysolution}
\end{defproblem}
\begin{document}
\ifthenelse{\boolean{showanswers}}{\textbf{Solution Sheet}}{\textbf{Homework problems}}
\useproblem{comparing_rates2}
\end{document}
得出的结果为:
或者,您可以尝试使用listings
包裹。