嵌套列表 - 数学书的问题和答案

嵌套列表 - 数学书的问题和答案

我正在尝试在 latex 中设置一本数学书。书中有练习,我想将问题标记为 1 (a) (b) (c) (i) (ii) 形式。它的设置方式是将答案作为第二个参数输入,然后将第二个参数打印在解决方案文件中。答案将位于书的末尾

在示例代码中,Q2 应如下所示 Q2 (a) (b) (c) (d) (i) (ii) 我知道代码有问题。有人能帮我吗?

\documentclass{book}

\usepackage{newfile}
\usepackage{amsmath}
\usepackage{xifthen}
\usepackage{enumitem}
\usepackage{multicol}
\usepackage{lipsum}

\setlength{\parindent}{0pt}

\newoutputstream{solutions}
\openoutputfile{\jobname.solutions}{solutions}

\newcounter{exblock}
\newcounter{exquestion}
\newcounter{exsubquestion}
\newcounter{exsubpart}

\newlist{questionlist}{enumerate}{3}
\newlist{solutionlist}{enumerate}{3}
\newlist{subquestionlist}{enumerate*}{2}
\newlist{subsolutionlist}{enumerate*}{2}
\newlist{subpartlist}{enumerate*}{1}
\newlist{subpartsolutionlist}{enumerate*}{1}

\setlist[questionlist,solutionlist]{label=\arabic*.}
\setlist[subquestionlist,subsolutionlist]{label=(\alph*),itemjoin={},before=~}
\setlist[subpartlist,subpartsolutionlist]{label=(\roman*),itemjoin={},before=~}


\newcommand{\exheader}{%
    \ifthenelse{\value{exblock}>0}{\addtostream{solutions}{\noexpand\end{solutionlist}}}{}%
\refstepcounter{exblock}%
\setcounter{exquestion}{0}%
\section{Exercises}%
\label{ex.\arabic{exblock}}%
\addtostream{solutions}{\noexpand\paragraph{Exercises \noexpand\ref{ex.\arabic{exblock}}, page \noexpand\pageref{ex.\arabic{exblock}}}}%
\addtostream{solutions}{\noexpand\begin{solutionlist}}%
}

\newcommand{\printsolutions}{%
    \ifthenelse{\value{exblock}>0}{\addtostream{solutions}{\noexpand\end{solutionlist}}}{}%
\closeoutputstream{solutions}%
\input{\jobname.solutions}%
}

\newenvironment{exercises}[1][]{%
\begin{questionlist}[start=\value{exquestion}+1, #1]%
}{%
\end{questionlist}%
}

\newenvironment{exercisescol}[2][]{%
\begin{multicols}{#2}%
    \begin{questionlist}[start=\value{exquestion}+1, #1]%
    }{%
    \end{questionlist}%
\end{multicols}%
}



\newcommand{\question}[2]{%
\refstepcounter{exquestion}%
\label{ex.\arabic{exblock}.\arabic{exquestion}}%
\item #1%
\addtostream{solutions}{\noexpand\item[\noexpand\ref{ex.\arabic{exblock}.\arabic{exquestion}}.] #2}%
}

\newenvironment{subquestions}[1][]{%
\refstepcounter{exquestion}%
\label{ex.\arabic{exblock}.\arabic{exquestion}}%
\setcounter{exsubquestion}{0}%
\addtostream{solutions}{\noexpand\item[\noexpand\ref{ex.\arabic{exblock}.\arabic{exquestion}}.]\noexpand\begin{subsolutionlist}}%
    \item #1%
    \begin{subquestionlist}%
    }{%
    \end{subquestionlist}%
    \addtostream{solutions}{\noexpand\end{subsolutionlist}}
    }
    
    
\newcommand{\subquestion}[2]{%
\refstepcounter{exsubquestion}%
\item #1%
\addtostream{solutions}{\noexpand\item #2}%
}

\newenvironment{subparts}[1][]{%
    \refstepcounter{exsubpart}%
    \label{ex.\arabic{exblock}.\roman{exsubpart}}
       
    \addtostream{solutions}{\noexpand\item[\noexpand\ref{ex.\arabic{exblock}.\roman{exsubpart}}.]\noexpand\begin{subpartsolutionlist}}%
        \item #1%
        \begin{subpartlist}%
        }{%
        \end{subpartlist}%
        \addtostream{solutions}{\noexpand\end{subpartsolutionlist}}
}

\newcommand{\subpart}[2]{%
    \refstepcounter{exsubquestion}%
    \item #1%
    \addtostream{solutions}
}

    \setlist*[questionlist]{font=\bfseries}
    \setlist*[solutionlist]{noitemsep,font=\bfseries}
    \setlist*[subquestionlist]{font=\bfseries}
    \setlist*[subsolutionlist]{noitemsep,font=\bfseries,itemjoin={\\},before={}}
    \setlist*[subpartlist]{font=\bfseries}
    \setlist*[subpartsolutionlist]{noitemsep,font=\bfseries,itemjoin={\\},before={}}
    
\begin{document}


\chapter{First Chapter}

\exheader
\begin{exercises}
    
        \question{This is a test question}{solution 1}
        
            \begin{subquestions}
            \subquestion{testing1a}{testing answer1} \\
            \subquestion{testing1b}{testing answer2} \\ 
            \subquestion{testing1c}{testing answer3} \\
            
            
            \begin{subparts}
                \subpart{First Sub Part}{First Subpart answer} \\
                \subpart{Second Sub Part}{Second Subpart answer} \\ 
                \subpart{Third Sub Part}{Third Subpart answer} \\
                \end{subparts}

            \subquestion{testing1e}{testing answer3} \\
        \end{subquestions}
        
    
    \begin{subquestions}[Find the range of values of $x$ for which.\\] 
        \subquestion{$3x^2+2x-1\geq0$}{$x\leq-1, \ x\geq\dfrac{1}{3}$} \\
        \subquestion{$x^2+8x-33<0$}{$-11 < x < 3$} \\
        \subquestion{$2x^2-5x+3\le0$}{$1\leq x\leq\dfrac{3}{2}$} \\
    \end{subquestions} \\
    
    \begin{subquestions}[Find the range of values of $x$ for which.\\] 
        \subquestion{$2x^2-3x-2\geq0$}{$x\leq-\dfrac{1}{2}, \ x\geq 2$} \\
        \subquestion{$x^2-6x+8<0$}{$2 < x < 4$} \\
        \subquestion{$x^2+5x+6\le0$}{$-3\leq x\leq -2$} \\
    \end{subquestions}  
    
    \end{exercises}


\chapter{Appendix}

\section{Solutions}

\printsolutions

\end{document}

答案1

我不知道您的代码到底想做什么,但它似乎比必要的要复杂得多。您实际上是在毫无理由地重复 LaTeX 的计数器步进,尽管您名义上是在使用enumitem,但实际上您也是在手动完成其工作。

您发布的代码由于分组问题无法编译。我尝试追踪这个问题,但最终放弃了。创建一个更简单的实现更容易,让enumitemLaTeX 的列表处理完成大部分繁重的工作。

对于您发布的示例,我们只需要一个枚举环境,exercises具有三个级别。您的代码定义了 3,但使用的级别从未超过 1。但是,如果您希望解决方案采用不同的格式,我创建了第二个嵌套的三个列表,其solutions格式与 完全相同exercises

我们可以同时或单独配置这些列表的每个级别的格式。例如,我们可能有

\newlist{exercises}{enumerate}{3}
\setlist[exercises]{font=\bfseries}

这将创建 3 个级别的列表,并将标签格式设置为使用粗体。示例中的所有 3 个级别都使用相同的字体,因此将其设置在一个位置是合理的。

% \setlist[exercises,1]{label=\arabic*.}% format in MWE

此行将第一级标签的格式设置为“1.”、“2.”等。由于我们不想要这样,我已将其注释掉,并将其替换为

\setlist[exercises,1]{label=Q\arabic*}% requested format

这会将格式设置为“Q1”、“Q2”等。

\setlist[exercises,2]{label=(\alph*),noitemsep}

对于下一个级别,我们将标签的格式设置为“(a)”、“(b)”,并消除项目之间的分隔。在您的 MWE 中,您指示enumitem创建一个内联列表,然后在项目之间强制换行\\永远不要用来\\结束一行除非您处于tabulararray类似的特殊环境中。

\setlist[exercises,3]{label=(\roman*),noitemsep}

此行以类似的方式格式化列表的第三级。

这消除了许多出错的可能性。我们不再需要\refstepcounter手动管理等,而且我们有一组易于记忆的计数器名称,因为enumitem名称计数器位于列表之后。因此,当我们创建列表时exercisesenumitem为我们创建了三个计数器:exercisesiexercisesiiexercisesiii。(它对 做了同样的事情solutions,但我们不关心这些,因为我们无论如何都不打算使用它们。

这意味着我们可以\question用更简单的方式定义

\newcommand{\question}[2]{%
  \item \label{ex.\arabic{exblock}.\arabic{exercisesi}} #1%
  \addtostream{solutions}{\noexpand\item[\noexpand\ref{ex.\arabic{exblock}.\arabic{exercisesi}}] #2}%
}

这里唯一的陷阱是我们需要记住\addtostream \item在某些情况下,这意味着稍微改变顺序。

请注意,虽然我在这里没有这样做,但exblock如果它与节计数器基本相同,您可能也应该消除它。但是,我不确定您的实际文档中是否也是如此,所以我没有触及配置的这个元素。

排版练习,自定义编号

不确定这里的章节编号中的“2”是否是故意的,或者您是否想从之前的部分编号中选取,但附录通常会获得不同的编号,因此无论如何这可能不是在这里需要确定的事情。

使用自定义编号排版解决方案

完整代码:

\documentclass{book}
% ateb https://tex.stackexchange.com/a/699706/ i gwestiwn akasteve: https://tex.stackexchange.com/q/699689/

\usepackage{newfile}
\usepackage{amsmath}
\usepackage{xifthen}
\usepackage{enumitem}
\usepackage{multicol}
% \usepackage{lipsum}% you cannot need this in your real document and don't use it here

\setlength{\parindent}{0pt}

\newoutputstream{solutions}
\openoutputfile{\jobname.solutions}{solutions}

\newcounter{exblock}

\newlist{exercises}{enumerate}{3}
\setlist[exercises]{font=\bfseries}
% \setlist[exercises,1]{label=\arabic*.}% format in MWE
\setlist[exercises,1]{label=Q\arabic*}% requested format
\setlist[exercises,2]{label=(\alph*),noitemsep}
\setlist[exercises,3]{label=(\roman*),noitemsep}

\newlist{solutions}{enumerate}{3}
\setlist[solutions]{font=\bfseries}
% \setlist[solutions,1]{label=\arabic*.}% format in MWE
\setlist[solutions,1]{label=Q\arabic*}% requested format
\setlist[solutions,2]{label=(\alph*),noitemsep}
\setlist[solutions,3]{label=(\roman*),noitemsep}

\newcommand{\exheader}{%
  \ifthenelse{\value{exblock}>0}{%
    \addtostream{solutions}{%
      \noexpand\end{solutions}%
    }%
  }{}%
  \refstepcounter{exblock}%
  \section{Exercises}%
  \label{ex.\arabic{exblock}}%
  \addtostream{solutions}{%
    \noexpand\paragraph{Exercises \noexpand\ref{ex.\arabic{exblock}}, page \noexpand\pageref{ex.\arabic{exblock}}}%
  }
  \addtostream{solutions}{\noexpand\begin{solutions}}%
}

\newcommand{\printsolutions}{%
  \ifthenelse{\value{exblock}>0}{%
    \addtostream{solutions}{\noexpand\end{solutions}}%
  }{}%
  \closeoutputstream{solutions}%
  \input{\jobname.solutions}%
}

\newcommand{\question}[2]{%
  \item \label{ex.\arabic{exblock}.\arabic{exercisesi}} #1%
  \addtostream{solutions}{\noexpand\item[\noexpand\ref{ex.\arabic{exblock}.\arabic{exercisesi}}] #2}%
}

\newenvironment{subquestions}[1][]{%
  \item \label{ex.\arabic{exblock}.\arabic{exercisesi}} #1%
  \addtostream{solutions}{%
    \noexpand\item[\noexpand\ref{ex.\arabic{exblock}.\arabic{exercisesi}}]\noexpand\begin{solutions}%
  }%
  \begin{exercises}%
}{%
  \end{exercises}%
  \addtostream{solutions}{\noexpand\end{solutions}}%
}
    
    
\newcommand{\subquestion}[2]{%
  \item\label{ex.\arabic{exblock}.\arabic{exercisesi}.\arabic{exercisesii}} #1
  \addtostream{solutions}{\noexpand\item[\noexpand\ref{ex.\arabic{exblock}.\arabic{exercisesi}.\arabic{exercisesii}}] #2}%
}

\newenvironment{subparts}[1][]{%
  \item \label{ex.\arabic{exblock}.\arabic{exercisesi}.\arabic{exercisesii}} #1%
  \addtostream{solutions}{%
    \noexpand\item[\noexpand\ref{ex.\arabic{exblock}.\arabic{exercisesi}.\arabic{exercisesii}}]\noexpand\begin{solutions}%
  }%
  \begin{exercises}%
}{%
  \end{exercises}%
  \addtostream{solutions}{\noexpand\end{solutions}}%
}
    
    
\newcommand{\subpart}[2]{%
  \item\label{ex.\arabic{exblock}.\arabic{exercisesi}.\arabic{exercisesii}.\arabic{exercisesiii}} #1
  \addtostream{solutions}{\noexpand\item[\noexpand\ref{ex.\arabic{exblock}.\arabic{exercisesi}.\arabic{exercisesii}.\arabic{exercisesiii}}] #2}%
}

    
\begin{document}


\chapter{First Chapter}

\exheader
\begin{exercises}
  \question{This is a test question}{solution 1}  
  \begin{subquestions}
    \subquestion{testing1a}{testing answer1} 
    \subquestion{testing1b}{testing answer2}  
    \subquestion{testing1c}{testing answer3}     
    \begin{subparts}
      \subpart{First Sub Part}{First Subpart answer} 
      \subpart{Second Sub Part}{Second Subpart answer}  
      \subpart{Third Sub Part}{Third Subpart answer} 
    \end{subparts}    
    \subquestion{testing1e}{testing answer3} 
  \end{subquestions}
  \begin{subquestions}[Find the range of values of $x$ for which.] 
    \subquestion{$3x^2+2x-1\geq0$}{$x\leq-1, \ x\geq\dfrac{1}{3}$} 
    \subquestion{$x^2+8x-33<0$}{$-11 < x < 3$} 
    \subquestion{$2x^2-5x+3\le0$}{$1\leq x\leq\dfrac{3}{2}$} 
  \end{subquestions}   
  \begin{subquestions}[Find the range of values of $x$ for which.] 
    \subquestion{$2x^2-3x-2\geq0$}{$x\leq-\dfrac{1}{2}, \ x\geq 2$} 
    \subquestion{$x^2-6x+8<0$}{$2 < x < 4$} 
    \subquestion{$x^2+5x+6\le0$}{$-3\leq x\leq -2$} 
  \end{subquestions}  
\end{exercises}

\chapter{Appendix}

\section{Solutions}

\printsolutions

\end{document}

编辑

你不能直接添加 Ti例如, Z 图片到解决方案,因为它们充满了不稳健的东西。对于简单的情况,您可以使用\noexpand。例如,

\subquestion{$x^2+5x+6\le0$}{$-3\leq x\leq -2$\noexpand\par
  \noexpand\begin{tikzpicture}
    \noexpand\draw [blue, top color=blue, bottom color=blue,middle color=blue!5] circle (50pt);
  \noexpand\end{tikzpicture}} 

解决方案中的阴影圆圈

对于更复杂的事情,我建议将图片或图形保存到一个框中,然后简单地将该框插入到解决方案中。

例如,在序言中,

\newsavebox\mygreencirclebox

在文档、章节或其他内容的开头,

\sbox\mygreencirclebox{%
  \begin{tikzpicture}
    \draw [blue, top color=green, bottom color=green,middle color=green!5] circle (50pt);
  \end{tikzpicture}%
}

进而

    \subquestion{$x^2+5x+6\le0$}{$-3\leq x\leq -2$\noexpand\par
      \noexpand\begin{tikzpicture}
        \noexpand\draw [blue, top color=blue, bottom color=blue,middle color=blue!5] circle (50pt);
      \noexpand\end{tikzpicture}%
    \usebox{\mygreencirclebox}} 

使用不同的方法将绿色和蓝色阴影圆圈插入溶液中

盒子方法更加强大,更不容易出错,并且可以避免污染您的代码\noexpand

相关内容