我使用一个非常简单的自定义环境来创建练习及其解决方案。每个练习都会产生一定数量的点数。点数定义如下:
% First argument: title, Second argument: number of points
\begin{exercise}{First Exercise}{12}
Calculate 2+2!
\begin{solution}
It's $\sqrt{16}$.
\end{solution}
\end{exercise}
我现在想创建一个表格来概述所有练习。其中一列用于显示点数。
\begin{center}
{
\renewcommand{\arraystretch}{1.2}
\newcolumntype{P}[1]{>{\centering\arraybackslash}p{#1}}
\begin{tabular}{|P{2cm}|P{2cm}|P{2cm}|}
\hline
& \multicolumn{2}{c|}{\#points} \\
Exercise & possible & achieved \\ \hline \hline
1 & 12 & \\ \hline
2 & & \\ \hline
3 & & \\ \hline
4 & & \\ \hline
5 & & \\ \hline \hline
Sum & 90 & \\ \hline
\end{tabular}
}
\end{center}
到目前为止,一切都是静态的,这意味着任何练习的改变都需要手动更改表格。例如,如果第一个练习现在产生 20 分而不是 12 分,我需要手动调整表格。
是否可以通过收集所有练习及其分数来自动生成上述表格?表格的布局不会改变。
以下是完整的代码示例:
\documentclass[DIN, xcolor=dvipsnames, pagenumber=false, twoside, fontsize=11pt, parskip=half]{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage{comment}
\usepackage{array}
\newcounter{NumberExercises} % define exercise counter
\def\NumEx{\stepcounter{NumberExercises}\arabic{NumberExercises}}
\newenvironment{exercise}[2]{\Large\textbf{\NumEx. Task: \normalfont #1 (#2 \ifnum #2=1 point\else points\fi)}}{}
% ================================= SOLUTIONS ===================================
\def\ifshow{1} % uncomment to always print solutions
\ifdefined\ifshow
\newenvironment{solution}{\textbf{Solution:}\\}{}
\else
\excludecomment{solution}
\fi
\begin{document}
\begin{exercise}{First Exercise}{12}
Calculate 2+2!
\begin{solution}
It's $\sqrt{16}$.
\end{solution}
\end{exercise}
\begin{center}
{
\renewcommand{\arraystretch}{1.2}
\newcolumntype{P}[1]{>{\centering\arraybackslash}p{#1}}
\begin{tabular}{|P{2cm}|P{2cm}|P{2cm}|}
\hline
& \multicolumn{2}{c|}{\#points} \\
Exercise & possible & achieved \\ \hline \hline
1 & 12 & \\ \hline
2 & & \\ \hline
3 & & \\ \hline
4 & & \\ \hline
5 & & \\ \hline \hline
Sum & 90 & \\ \hline
\end{tabular}
}
\end{center}
\end{document}
答案1
您可以为每个练习定义一个标签,用于存储积分并抓取它们并结束:
\documentclass[DIN, xcolor=dvipsnames, pagenumber=false, twoside, fontsize=11pt, parskip=half]{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage{comment}
\usepackage{array}
\newcounter{NumberExercises} % define exercise counter
\newcounter{PointsExercises} % define points counter
\newcounter{PointsTotal} % define total points counter
\def\NumEx{\stepcounter{NumberExercises}\arabic{NumberExercises}}
\newenvironment{exercise}[2]{\Large\textbf{\NumEx. Task: \normalfont #1 (#2 \ifnum #2=1 point\else points\fi)}%
\setcounter{PointsExercises}{#2}%
\addtocounter{PointsExercises}{-1}%
\refstepcounter{PointsExercises}%
\label{ExercisesPoints-\alph{NumberExercises}}%
}{}
% ================================= SOLUTIONS ===================================
\def\ifshow{1} % uncomment to always print solutions
\ifdefined\ifshow
\newenvironment{solution}{\textbf{Solution:}\\}{}
\else
\excludecomment{solution}
\fi
\makeatletter
\newcommand{\iflabelexists}[3]{\@ifundefined{r@#1}{#3}{#2}}
\makeatother
\begin{document}
\begin{exercise}{First Exercise}{12}
Calculate 2+2!
\begin{solution}
It's $\sqrt{16}$.
\end{solution}
\end{exercise}
\begin{exercise}{Second Exercise}{32}
Calculate 3+1!
\begin{solution}
It's $\sqrt[4]{256}$.
\end{solution}
\end{exercise}
\stepcounter{NumberExercises}
\begin{exercise}{Fourth Exercise}{11}
Calculate 2-1!
\begin{solution}
It's $\cos(2 \pi)$.
\end{solution}
\end{exercise}
\begin{center}
{
\newcounter{rowno}
\setcounter{rowno}{0}
\renewcommand{\arraystretch}{1.2}
\newcolumntype{P}[1]{>{\centering\arraybackslash}p{#1}}
\begin{tabular}{|P{2cm}|>{\stepcounter{rowno}%
\iflabelexists{ExercisesPoints-\alph{rowno}}%
{\ref{ExercisesPoints-\alph{rowno}}%
\addtocounter{PointsTotal}{\ref{ExercisesPoints-\alph{rowno}}}%
}{\relax}}P{2cm}|P{2cm}|}
\hline
& \multicolumn{2}{c|}{\#points} \\
Exercise & \multicolumn{1}{c|}{possible} & achieved \\ \hline \hline
1 & & \\ \hline
2 & & \\ \hline
3 & & \\ \hline
4 & & \\ \hline
5 & & \\ \hline \hline
Sum & \multicolumn{1}{c|}{\arabic{PointsTotal}} & \\ \hline
\end{tabular}
}
\end{center}
\end{document}