创建问题解决能力

创建问题解决能力

我正在尝试修改一个前段时间得到精彩回答的问题解决功能这里。每道题后,可以自由使用\solution将解决方案直接与题文放在一起。解决方案通过命令存储在 .sol 文件中\collectSolutions,可以通过 在文档中访问\printSolutions。下面的代码感谢 David Carlisle,运行良好(也许应该是一个包)。这个问题是关于微调的。

下面的代码将返回解决方案的编号:1:2a:b:c:3ai:ii:3b:4:

我希望它返回:1:2a:b:c:3ai:ii:b:4:

\documentclass{article}

\makeatletter
\newwrite\solutions@file
\newcommand{\collectSolutions}{\immediate\openout\solutions@file=\jobname.sol}
\newcommand{\sol@enumi}{{\theenumi}}
\newcommand{\sol@enumii}{{\theenumi.}\theenumii}
\newcommand{\sol@enumiii}{{\theenumi.\theenumii.}\theenumiii}
\newcommand{\solution}[1]{%
  \immediate\write\solutions@file{%
    \noexpand\solsep\csname sol@\@enumctr\endcsname: \unexpanded{#1\ignorespaces}%
  }%
}

\def\solsep{\afterassignment\@solsep\def\@tempa}

\def\@solsep{%
\ifx\@tempa\sol@lastsec,
\else
\global\let\sol@lastsec\@tempa
\sol@stop\gdef\sol@stop{. }%
\textbf{\@tempa}
\fi
}

\newcommand{\printSolutions}{%
  \let\sol@stop\@empty
  \gdef\sol@lastsec{0.}%
  \immediate\closeout\solutions@file
  \noindent\input{\jobname.sol}\sol@stop
}
\makeatother
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
\collectSolutions

\begin{enumerate}
\item Question text \solution{Ans}
\item Question text 
\begin{enumerate}
  \item Part Q \solution{Ans}
  \item Part Q \solution{Ans}
  \item Part Q \solution{Ans}
\end{enumerate}
\item Another Q
\begin{enumerate}
  \item Part Q
    \begin{enumerate}
        \item Part Q \solution{Ans}
        \item Part Q \solution{Ans}
    \end{enumerate}
  \item Part Q \solution{Ans}
\end{enumerate}
\item Question text \solution{Ans}
\end{enumerate}

\printSolutions
\end{document}

答案1

解决方案是重写宏\sol@enumiii以将两个较高的前缀分开分组。所以

\newcommand{\sol@enumiii}{{\theenumi.}{\theenumii.}\theenumiii}

使用两个内部组而不是一个组{\theenumi.\theeunmii.}。下面的代码还修复了:MWE 输出中的多余的空格。

示例输出

\documentclass{article}

\makeatletter
\newwrite\solutions@file
\newcommand{\collectSolutions}{\immediate\openout\solutions@file=\jobname.sol}
\newcommand{\sol@enumi}{{\theenumi}}
\newcommand{\sol@enumii}{{\theenumi.}\theenumii}
\newcommand{\sol@enumiii}{{\theenumi.}{\theenumii.}\theenumiii}
\newcommand{\solution}[1]{%
  \immediate\write\solutions@file{%
    \noexpand\solsep\csname sol@\@enumctr\endcsname: \unexpanded{#1\ignorespaces}%
  }%
}

\def\solsep{\afterassignment\@solsep\def\@tempa}

\def\@solsep{%
\ifx\@tempa\sol@lastsec,
\else
\global\let\sol@lastsec\@tempa
\sol@stop\gdef\sol@stop{. }%
\textbf{\@tempa}%
\fi
}

\newcommand{\printSolutions}{%
  \let\sol@stop\@empty
  \gdef\sol@lastsec{0.}%
  \immediate\closeout\solutions@file
  \noindent\input{\jobname.sol}\sol@stop
}
\makeatother
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
\collectSolutions

\begin{enumerate}
\item Question text \solution{Ans one}
\item Question text
\begin{enumerate}
  \item Part Q \solution{Ans two A}
  \item Part Q \solution{Ans two B}
  \item Part Q \solution{Ans two C}
\end{enumerate}
\item Another Q
\begin{enumerate}
  \item Part Q
    \begin{enumerate}
        \item Part Q \solution{Ans three A one}
        \item Part Q \solution{Ans three A two}
    \end{enumerate}
  \item Part Q \solution{Ans three B}
  \item Part Q
    \begin{enumerate}
    \item Part Q \solution{Ans three C one}
    \item Part Q \solution{Ans three C two}
    \end{enumerate}
\end{enumerate}
\item Question text \solution{Ans four}
\end{enumerate}

\printSolutions
\end{document}

相关内容