我需要用 LaTeX 编写解决方案,以便我们的讲师在整个课程中给出问题。为了做到这一点,我创建了一个名为prob
(见下文)的环境。基本上,它是一个单词“问题”和相应的数字(所有问题都已编号)。问题是我想将所有解决方案保存在同一个文件中,但我每周只需要提交所需的问题子集(例如,第一周提交问题 1-5,第二周提交问题 6-8,等等)。
因此,问题是是否有可能以某种方式仅显示具有给定数字的问题的解决方案?换句话说,我需要一个命令showprob
来showprob{1}
显示(打印)问题 1 的解决方案。也许,需要修改环境(或使用另一种技术)。
当然,人们可以为不同的作业(问题子集)创建单独的文件,然后只使用命令\input
,但我想避免这种情况。一个(可能不太令人信服的)原因是我希望能够展示任何解决方案的子集(比如,我的朋友只需要问题 1、5 和 12 的解决方案,那么只向他/她发送这些解决方案会很方便,而不是发送一堆包含问题 1-16 的解决方案的文件)。
\documentclass[12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{amsthm,amssymb,amsmath}
\newcounter{probnum}
\newtheoremstyle{problemstyle}% name of the style to be used
{\topsep}% measure of space to leave above the theorem. E.g.: 3pt
{\topsep}% measure of space to leave below the theorem. E.g.: 3pt
{}% name of font to use in the body of the theorem
{0pt}% measure of space to indent
{\bfseries}% name of head font
{.}% punctuation between head and body
{ }% space after theorem head; " " = normal interword space
{\thmname{#1}\thmnumber{ #2}\textnormal{\thmnote{ (#3)}}}
\theoremstyle{problemstyle}
\newtheorem{prob}[probnum]{Problem}
\begin{document}
\prob{Solution.}
\end{document}
任何想法,将不胜感激。
答案1
首先,即使它与你的问题无关,使用
\prob{Solution}
在很多方面都是错误的。正确的语法和给定的代码是
\begin{prob}
Solution
\end{prob}
如果不能正确使用\begin{prob}
,\end{prob}
最终会导致本质上随机的间距。也没有必要定义计数器,因为计数器\newtheorem
会为您完成。
现在,假设你的问题是按顺序编号的,比如从 1 到 8,并且你想要类似
\documentclass{article}
\newtheorem{prob}{Problem}
\begin{document}
\showproblems{1-3,4,6}
\begin{prob}
Solution for problem 1
\end{prob}
\begin{prob}
Solution for problem 2
\end{prob}
\begin{prob}
Solution for problem 3
\end{prob}
\begin{prob}
Solution for problem 4
\end{prob}
\begin{prob}
Solution for problem 5
\end{prob}
\begin{prob}
Solution for problem 6
\end{prob}
\begin{prob}
Solution for problem 7
\end{prob}
\begin{prob}
Solution for problem 8
\end{prob}
\end{document}
并且您希望只打印所述问题,即 1、2、3、4、6。我省略了样式声明,因为它与解决方案无关。
第一步是改变类定理环境的名称并定义prob
使用给定的列表进行检查。
\documentclass{article}
\usepackage{amsthm}
\newtheoremstyle{problemstyle}% name of the style to be used
{\topsep}% measure of space to leave above the theorem. E.g.: 3pt
{\topsep}% measure of space to leave below the theorem. E.g.: 3pt
{}% name of font to use in the body of the theorem
{0pt}% measure of space to indent
{\bfseries}% name of head font
{.}% punctuation between head and body
{ }% space after theorem head; " " = normal interword space
{\thmname{#1}\thmnumber{ #2}\textnormal{\thmnote{ (#3)}}}
\theoremstyle{problemstyle}
\newtheorem*{probinner}{Problem \problemnumber}
\newcommand{\problemnumber}{}% initialize
\ExplSyntaxOn
\NewDocumentEnvironment{prob}{+b}
{
\richrow_prob_body:n { #1 }
}
{}
\NewDocumentCommand{\showproblems}{m}
{
\richrow_prob_list:n { #1 }
}
\seq_new:N \g_richrow_prob_list_seq
\seq_new:N \l__richrow_prob_temp_seq
\int_new:N \g_richrow_prob_count_int
% populate the sequence containing the problems to show
\cs_new_protected:Nn \richrow_prob_list:n
{
\clist_map_function:nN { #1 } \__richrow_prob_add:n
}
\cs_new_protected:Nn \__richrow_prob_add:n
{
\seq_set_split:Nnn \l__richrow_prob_temp_seq { - } { #1 }
\int_compare:nTF { \seq_count:N \l__richrow_prob_temp_seq == 1 }
{% single number
\seq_gput_right:Nn \g_richrow_prob_list_seq { #1 }
}
{% range
\int_step_inline:nnn
{ \seq_item:Nn \l__richrow_prob_temp_seq {1} } % start
{ \seq_item:Nn \l__richrow_prob_temp_seq {2} } % end
{ \seq_gput_right:Nn \g_richrow_prob_list_seq { ##1 } }
}
}
% define the code for prob
\cs_new_protected:Nn \richrow_prob_body:n
{
% increment the counter
\int_gincr:N \g_richrow_prob_count_int
% define the problem number
\cs_set:Npx \problemnumber { \int_eval:n { \g_richrow_prob_count_int } }
% check against the list
\seq_if_in:NxT \g_richrow_prob_list_seq { \problemnumber }
{% the problem is in the list
\probinner #1 \endprobinner
}
}
\ExplSyntaxOff
\begin{document}
\showproblems{1-3,4,6}
\begin{prob}
Solution for problem 1
\end{prob}
\begin{prob}
Solution for problem 2
\end{prob}
\begin{prob}
Solution for problem 3
\end{prob}
\begin{prob}
Solution for problem 4
\end{prob}
\begin{prob}
Solution for problem 5
\end{prob}
\begin{prob}[Difficult]
Solution for problem 6
\end{prob}
\begin{prob}
Solution for problem 7
\end{prob}
\begin{prob}
Solution for problem 8
\end{prob}
\end{document}