如何自定义练习样式以使用自动编号书中的问题答案

如何自定义练习样式以使用自动编号书中的问题答案

我是 LaTeX 新手,正在尝试为一本书编写几个问题和答案。我会在某个部分之后或根据文档中的任意位置选择使用问题答案。

因此,我希望问题和答案能够自动编号。一旦我在文档的其他部分使用另一组练习,编号就会重置。我的示例设计如图所示 这是我将在章节内的各个部分使用的一个选项 我将在本章末尾使用此选项

MWE 可能如下

%文本的颜色和字体大小应可自定义,字体颜色可选为红色% %每个问题的问题和答案都分组在一起,并且有各种类型的问题%

%请注意,我已经有用于标记和引用的 tcolourbox,因此不需要标题练习 1.1 等。我只列出这个问题和答案,以便我能够根据需要使用它们。无需链接到任何部分或章节 % 因为我已经有了 colorbox 代码,我会将他的代码放在里面。%%

\documentclass[hidelinks,11pt]{book}
\usepackage{graphicx}
\usepackage{titlesec}
\usepackage{titletoc}
\usepackage{exercise}
\usepackage{xsim}
\usepackage{blindtext}
\usepackage[english]{babel}

\begin{document}



{\noindent \textbf \LARGE  This is one option of one by one question answer}\bigskip

{\noindent \textbf {Question 1.}} Which if the following is a secondary memory Devices.?\hfill \break  
    (a) keyboard    (b) Monitor (c) Hard Disk   (d) SMPS \hfill \break          
{\textbf {Answer 1.}} (c) Hard Disk.     \vspace{2\baselineskip} %used for skiping line for next question visibility

{\noindent \textbf {Question 2.}} Recently deleted files are stored in\hfill \break           
    (a) Recycle bin (b) Taskbar (c) My computer (d) desktop \hfill \break       
{\textbf {Answer 2.}} ( a) Recycle bin.      \vspace{2\baselineskip}

{ \noindent \textbf {Question 3.}} What is RAM  \hfill \break             
{\noindent \textbf {Answer 3.}} RAM is Random Acess Memory.  \vspace{2\baselineskip}

 {\noindent \textbf {Question 4.}} What is ROM  \hfill \break             
  {\noindent \textbf {Answer 4.}} ROM is Read Only Memory.  \vspace{2\baselineskip} \hfill \break

 {\noindent \textbf \LARGE This is 2nd  option of grouping question and snwer seperately } \bigskip

{\textbf \LARGE {Questions}} \par

{\noindent \textbf {\underline {A. MCQ}}}\hfill \break

{\noindent \textbf {Question 1.}} Which if the following is a secondary memory Devices.?\hfill \break  
    (a) keyboard    (b) Monitor (c) Hard Disk       (d) SMPS   \hfill \break    

{\noindent \textbf {Question 2.}} Recently deleted files are stored in\hfill \break           
    (a) Recycle bin (b) Taskbar (c) My computer (d) desktop     \hfill \break

{\noindent \textbf {\underline{B. Very Short Questions}}} \hfill \break
{\noindent \textbf {Question 3.}} What is RAM \hfill \break \par
{\noindent \textbf {\underline{C. Short Questions}}}\hfill \break
{\noindent \textbf {Question 4.}} What is ROM and its function?  \hfill \break \par
{\noindent \textbf \LARGE Answers}\par  \bigskip \par
{\noindent \textbf  {\underline{A. MCQ}}}\par
{\noindent \textbf {Answer 1.}} (c) Hard Disk.\hfill \break
{\noindent \textbf {Answer 2.}} ( a) Recycle bin.\hfill \break \par
{\noindent \textbf {\underline{ B. Very Short Questions}}}\par
{\noindent \textbf {Answer 3.}} RAM is Random Acess Memory.\hfill \break \par
{\noindent \textbf  {\underline{C. Short Questions}}}  \par
{\noindent \textbf {Answer 4.}} ROM is Read Only Memory.\hfill \break
\blindtext 

\end{document}

答案1

由于您明确要求xsim解决方案在评论中

有人可以指导我使用 xsim 包吗?

下面是一个简单示例,展示了 MWE 的基本设置:

\documentclass{article}
\usepackage{xsim}
\usepackage{tasks}

\DeclareExerciseProperty{mc}
\newcommand*\correct[1]{%
  \XSIMexpandcode{\SetExerciseProperty{mc}{(\alph{task}) \unexpanded{#1}}}%
  #1%
}
\newcommand*\mcanswer{\GetExerciseProperty{mc}}

\settasks{label=(\alph*),label-width=14pt}

\xsimsetup{
  % uncomment if you want the answer directly after the questions:
  % solution/print = true ,
  exercise/name = Question ,
  solution/name = Answer ,
  exercise/template = runin ,
  solution/template = runin ,
  print-solutions/headings-template = custom
}
\renewcommand*\theexercise{\arabic{exercise}.}
\loadxsimstyle{layouts}

\DeclareExerciseHeadingTemplate{custom}{\section*{Answers}}


\begin{document}

\begin{exercise}
  Which if the following is a secondary memory Devices?
  \begin{tasks}(4)
     \task keyboard \task Monitor \task \correct{Hard Disk} \task SMPS
  \end{tasks}
\end{exercise}
\begin{solution}
  \mcanswer.
\end{solution}

\begin{exercise}
  Recently deleted files are stored in
  \begin{tasks}(4)
    \task \correct{Recycle bin} \task Taskbar \task My computer \task desktop
  \end{tasks}
\end{exercise}
\begin{solution}
  \mcanswer.
\end{solution}

\begin{exercise}
  What is RAM?
\end{exercise}
\begin{solution}
  RAM is Random Acess Memory.
\end{solution}

\begin{exercise}
  What is ROM?
\end{exercise}
\begin{solution}
  ROM is Read Only Memory.
\end{solution}

% uncomment if you want to print a list of all solutions:
% \printsolutions

\end{document}

一个输出:

在此处输入图片描述

另一个输出是:

在此处输入图片描述

相关内容