我想为学生排版每周练习表。通常,一张练习表包含 2-5 个练习。此外,还应为导师提供一个包含解决方案的版本。
由于每年的练习内容大体相同,我们希望创建一个练习集,然后可以按照自定义顺序将它们放在一起。因此,应该有一个包含序言和所有内容的主文档,然后包含或输入练习。
我对 KOMA-Script 类 scrartcl 非常满意,我唯一想要的就是对练习和解决方案进行自动编号。
一种简单(但可能并不优雅)的按我希望的方式对练习进行编号的方法如下:
\documentclass{scrartcl}
\usepackage{titlesec}
\titleformat{\subsection}{\large\bfseries\sffamily}{}{0pt}{Exercise \thesubsection:\quad}
\newcommand{\exercise}[1]{\subsection{#1}}
\newcommand{\solution}[1]{\subsection{#1}}
\begin{document}
\setcounter{section}{1}\setcounter{subsection}{0}
\exercise{This is the Name of the first Exercise}
This is the problem given for the first Exercise.
\exercise{This is the Name of the second Exercise}
This is the problem given for the second Exercise.
\addtocounter{section}{1}\setcounter{subsection}{0}
\exercise{This is the Name of the third Exercise}
This is the problem given for the third Exercise.
\solution{This is the solution for the third exercis.}
However, it should be titled "Solution 2.1" and not "Exercise 2.2".
\end{document}
但是,我仍然需要另一个命令\solution
,它给出“解决方案 XY:...”,其中 XY 只是最后一个练习的编号。
我知道有很多软件包,比如考试、答案等,但还没有一个能满足我的要求(不过可能是我自己的问题)。在练习包中,我无法像通常那样包含图表或小页面,这一点我非常不喜欢。
顺便说一句,我不介意手动注释不同版本的解决方案。但是,如果有自动化版本只包含练习或练习和解决方案,那就更好了!
有人能很好地解决我的问题吗?
答案1
正如评论中提到的exsheets
包可用于此目的。可以使用question
环境subtitle
选项将练习的标题/名称作为副标题。要实际打印副标题,必须使用标题实例对其进行排版。预定义实例block-subtitle
几乎与您自己定义的标题相同,因此可以使用。
这意味着我们设置
\SetupExSheets{
headings = block-subtitle ,
headings-format = \large\bfseries\sffamily ,
% needs v0.16 2014/09/14 to work:
subtitle-format = \large\bfseries\sffamily
}
它模仿了你问题中的定义。
接下来我们需要的显然是一些mainquestion
计数器,它可以用来打印问题编号,如 1.1、1.2、2.1 等。像你一样,我也使用计数器section
来完成这项任务。然后我们需要
\SetupExSheets{
counter-within = section ,
counter-format = se.qu\IfQuestionSubtitleT{:} ,
}
\IfQuestionSubtitleT{:}
确保只有给出副标题时才排版冒号。
为了管理练习,可以在外部文件中定义它们myexercises.tex
。例如,在下面的例子中,我使用以下内容保存了它:
\begin{question}[subtitle=This is the Name of the first Exercise,ID=Q1]
This is the problem given for the first Exercise.
\end{question}
\begin{solution}
This is the first solution.
\end{solution}
\begin{question}[subtitle=This is the Name of the second Exercise,ID=Q2]
This is the problem given for the second Exercise.
\end{question}
\begin{solution}
This is the second solution.
\end{solution}
\begin{question}[subtitle=This is the Name of the third Exercise,ID=Q3]
This is the problem given for the third Exercise.
\end{question}
\begin{solution}
This is the third solution.
\end{solution}
如果我们将所有内容放在一起,我们会得到:
\documentclass{scrartcl}
\usepackage{exsheets}[2014/09/14] % v0.16 or newer
\SetupExSheets{
headings = block-subtitle ,
headings-format = \large\bfseries\sffamily ,
subtitle-format = \large\bfseries\sffamily ,
counter-within = section ,
counter-format = se.qu\IfQuestionSubtitleT{:} ,
% solution/print = true % uncomment for tutors
}
% needed in earlier versions of exsheets:
% \DeclareInstance{exsheets-heading}{block-subtitle}{default}{
% subtitle-format = \large\bfseries\sffamily ,
% join = {
% title[r,B]number[l,B](.333em,0pt) ;
% title[r,B]subtitle[l,B](1em,0pt)
% } ,
% attach = {
% main[l,vc]title[l,vc](0pt,0pt) ;
% main[r,vc]points[l,vc](\marginparsep,0pt)
% }
% }
\begin{document}
\stepcounter{section}
\includequestions[IDs={Q1,Q2}]{myexercises}
\stepcounter{section}
\includequestions[IDs=Q3]{myexercises}
\end{document}
如果你取消注释该行
% solution/print = true
在上面的例子中,你会得到
如果你在最后添加,\printsolutions
你会得到
不幸的是,目前无法为解决方案添加字幕。
答案2
这是我的包的简化版本,尚未准备好发布(更新:正在进展,但仍然不可用!)。
此代码定义了一个命令
\Exercise[2][]
其中强制参数包含练习文本,可选参数包含值solution={....}
,即选修的。
{}
应在分组中指定解决方案。
然后将解决方案内容写入文件。
(很简单)示例:
\Exercise[solution={2}]{What is 1 + 1}
所有练习都可以放在外部文件中,也可以直接放在序言中,或者放在文档正文中。
但是,只要没有声明列表,就不会打印它们,从而可以选择特定的练习。
\DeclareExerciseList{1,3,4,5}
将选择练习 1、3、4 和 5,忽略练习 2 和任何其他练习。不存在的练习编号将被忽略!
驱动程序 *.tex/package 代码
\documentclass{scrartcl}
\usepackage{tcolorbox}%
\usepackage{xkeyval}%
\usepackage{blindtext}%
\usepackage{etoolbox}%
\usepackage{assoccnt}%
\newcounter{realexercisecounter}%
\newcounter{exercise}%
\newcounter{solution}%
\DeclareAssociatedCounters{exercise}{realexercisecounter}%
% Some definitions
\newcommand{\SolutionName}{Solution}
\newcommand{\SolutionPageName}{Solutions}
\newcommand{\ExerciseName}{Exercise}%
\AtBeginDocument{%
\immediate\openout\exerfile=\jobname.solutions
\immediate\write\exerfile{%
\string\setcounter{solution}{0}%
}%
\immediate\write\exerfile{%
\string\clearpage
\string\section*{\SolutionPageName}%
}%
}%
\newwrite\exerfile%
\makeatletter
\define@key{exerkeys}{exercise}{%
\def\exerkeys@@exercise{#1}%
}%
\define@key{exerkeys}{solution}{%
\def\exerkeys@@solution{#1}%
}%
\newcommand{\writesolutiontofile}[1]{%
\immediate\write\exerfile{%
\string\begin{Solution}^^J%
\unexpanded\expandafter{#1}^^J%
\string\end{Solution}^^J%
}%
}%
\newcommand{\Exercise}[2][]{%
\setlength{\parindent}{0pt}%
\refstepcounter{exercise}%
\xifinlist{\number\value{realexercisecounter}}{\exerciselist}{%
\setkeys{exerkeys}{#1}%
\begin{tcolorbox}[width=\textwidth,colbacktitle=red,coltitle=black,title={\ExerciseName~\theexercise}]
#2%
\ifdef{\exerkeys@@solution}{%
\xifinlist{\number\value{realexercisecounter}}{\exerciselist}{%
\immediate\write\exerfile{%
\string\setcounter{solution}{\number\value{exercise}}^^J%
\string\addtocounter{solution}{-1}%
}%
}{}%
\writesolutiontofile{\exerkeys@@solution}%
}{}%
\end{tcolorbox}%
}{}%
\undef\exerkeys@@solution% Must be outside/at the end!
}%
\newenvironment{Solution}{%
\setlength{\parindent}{0pt}%
\refstepcounter{solution}
\begin{tcolorbox}[width=\textwidth,colbacktitle=green,coltitle=black,
title={\SolutionName~\thesolution}]
}{\end{tcolorbox}}%
\makeatother%
\AtEndDocument{%
\immediate\closeout\exerfile%
\cleardoublepage%
\InputIfFileExists{\jobname.solutions}{}{}%
}%
\newcommand{\DeclareExerciseList}[1]{%
\undef{\exerciselist}{}%
\listgadd{\exerciselist}{}%
\forcsvlist{\listgadd{\exerciselist}}{#1}%
}%
\begin{document}
\DeclareExerciseList{1,3,4,5}%
\InputIfFileExists{exercises}{}{Ooopps!}
\end{document}
练习.tex
\Exercise[solution={Yes, it's a question}]{First Question}
\Exercise[solution={They are thin at one end, thick in the middle and thin on the end again}]{%
Question: What is special about Brontosaurs?
}%
\Exercise[solution={to be done}]{%
Proof
\begin{equation}
a^2 + b^2 = c^2
\end{equation}
}%
\Exercise[solution={\textcolor{blue}{\blindtext}}]{%
Cite a Latin Text
}%
\Exercise[]{%
\textcolor{red}{\textbf{Cite a Latin text again}} % Won't be shown as solution
}%
答案3
为了解决您的问题,我会在一个文档(tex 文件)中提供每个练习表,如下所示:
--sheet1.tex:
\documentclass{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage[ngerman]{babel}
% generate blindtext
\usepackage{blindtext}
% add dot after section number
\makeatletter
\g@addto@macro\thesection.
\makeatother
% define command for exercise and solution
\newcommand{\problem}[1]{\section{Exercise}\subsection*{Problem}{#1}}
\newcommand{\solution}[1]{\subsection*{Solution for Problem \arabic{section}}{#1}}
\begin{document}
\title{Sheet 1}
\maketitle
% testing without own commands
\section{Exercise}
\subsection*{Problem}
\blindtext
\subsection*{Solution for Problem \arabic{section}}
\blindtext
% using own commands defined above
\problem{insert your exercise, task, problem here}
\solution{insert your solution here}
\end{document}
将第二张表保存在与第一张表类似的文件中(在此示例中,它只是“sheet1.tex”的重命名副本):
--sheet2.tex:
\documentclass{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage[ngerman]{babel}
% generate blindtext
\usepackage{blindtext}
% add dot after section number
\makeatletter
\g@addto@macro\thesection.
\makeatother
% define command for exercise and solution
\newcommand{\problem}[1]{\section{Exercise}\subsection*{Problem}{#1}}
\newcommand{\solution}[1]{\subsection*{Solution for Problem \arabic{section}}{#1}}
\begin{document}
\title{Sheet 2}
\maketitle
% testing without own commands
\section{Exercise}
\subsection*{Problem}
\blindtext
\subsection*{Solution for Problem \arabic{section}}
\blindtext
% using own commands defined above
\problem{insert your exercise, task, problem here}
\solution{insert your solution here}
\end{document}
要生成包含所有曾经分发过的练习表的“总体文档”,您可以使用该standalone
包在使用时忽略外部文件的前言input
或include
导入该外部文件。因此,对于“总体文档”,您只需要编译“allsheets.tex”:
-- 所有表格.tex:
\documentclass{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage[ngerman]{babel}
% input external file but ignore the preamble
\usepackage{standalone}
% generate blindtext
\usepackage{blindtext}
% add dot after section number
\makeatletter
\g@addto@macro\thesection.
\makeatother
% define command for exercise and solution
\newcommand{\problem}[1]{\section{Exercise}\subsection*{Problem}{#1}}
\newcommand{\solution}[1]{\subsection*{Solution for Problem \arabic{section}}{#1}}
\begin{document}
\title{All Sheets}
\maketitle
\input{sheet1.tex}
\input{sheet2.tex}
\end{document}
这种方法将在学期/课程期间提供单个练习表,并在学期/课程结束时提供“总体文档”。