如何在枚举中交替显示偶数和奇数条目的不同名称

如何在枚举中交替显示偶数和奇数条目的不同名称

我正在制作一个问题列表,其中答案在问题下方给出。目前要求答案应与问题使用相同的缩进,并自动编号(与问题编号相同)。例如,问题 1 后面应跟着答案 1,依此类推。

MWE 是,其输出如下所示。

\documentclass{article}
\usepackage{amsmath}
\usepackage{enumitem}


\begin{document}
\begin{enumerate}[label={Problem \arabic*},leftmargin=2cm]
\item Find the least term of each sequence:

\begin{enumerate}[label=(\alph*)]
\item $y_{n} = n^{2} -5n + 1 $
\item $y_{n} = n + \dfrac{100}{n}$ 
\item $y_{n} = n + 5 \sin\dfrac{\pi n}{2}$ 
\end{enumerate}
 Answer 1 % this is manual now, should be auto with indent as \item
\begin{enumerate}[label=(\alph*)]
\item $  y_{2} = y_{3}= -5$
\item $ y_{10} = 20 $
\item $ y_{3} = -2$
\end{enumerate}

\item Find the largest term of each sequence:

\begin{enumerate}[label=(\alph*)]
\item $y_{n} = \dfrac{90n}{n^{2} + 9} $
\item $y_{n} = \dfrac{10^{n}}{n!}$ 
\end{enumerate}

 Answer 2 % this is manual now, should be auto with indent as \item
\begin{enumerate}[label=(\alph*)]
\item $  y_{3}= 15$
\item $ y_{9} = y_{10} = \dfrac{10^{9}}{9!} $
\end{enumerate}

\end{enumerate}
\end{document}

这将生成以下输出: 在此处输入图片描述

预期结果如下所示。 在此处输入图片描述

答案1

这是一个自动化过程:

\documentclass{article}
\usepackage{amsmath}
\usepackage{enumitem}


\newcommand{\Answer}{\let\oldtheenumi=\theenumi\def\theenumi{\arabic{enumi}}\item[Answer \theenumi] \leavevmode\let\theenumi\oldtheenumi}

\begin{document}
\begin{enumerate}[label={Problem \arabic*},leftmargin=2cm]
\item Find the least term of each sequence:

\begin{enumerate}[label=(\alph*)]
\item $y_{n} = n^{2} -5n + 1 $
\item $y_{n} = n + \dfrac{100}{n}$ 
\item $y_{n} = n + 5 \sin\dfrac{\pi n}{2}$ 
\end{enumerate}

\Answer% this is automated now

\begin{enumerate}[label=(\alph*)]
\item $  y_{2} = y_{3}= -5$
\item $ y_{10} = 20 $
\item $ y_{3} = -2$
\end{enumerate}

\item Find the largest term of each sequence:

\begin{enumerate}[label=(\alph*)]
\item $y_{n} = \dfrac{90n}{n^{2} + 9} $
\item $y_{n} = \dfrac{10^{n}}{n!}$ 
\end{enumerate}

\Answer % this is automated now
\begin{enumerate}[label=(\alph*)]
\item $  y_{3}= 15$
\item $ y_{9} = y_{10} = \dfrac{10^{9}}{9!} $
\end{enumerate}

\end{enumerate}
\end{document}

结果和您要求的图像完全一样。

答案2

我建议更改语法以使代码更清晰。定义一个problemandanswer环境:

在此处输入图片描述

\documentclass{article}

\usepackage{amsmath,enumitem,needspace}

\newcounter{problem}
\newenvironment{problem}[1]
  {\par
   \needspace{3\baselineskip}% Need at least 3 lines of text left on page
   \refstepcounter{problem}% Mark counter increment
   \noindent Problem \theproblem\quad #1% Set problem heading
   \begin{enumerate}[label=(\alph*)]}
  {\end{enumerate}}
\newenvironment{answer}
  {\par
   \needspace{3\baselineskip}% Need at least 3 lines of text left on page
   \noindent Answer \theproblem% Set answer heading
   \begin{enumerate}[label=(\alph*)]}
  {\end{enumerate}}

\begin{document}

\begin{problem}
  {Find the least term of each sequence:}
  \item $y_{n} = n^2 - 5n + 1$
  \item $y_{n} = n + \dfrac{ 100 }{ n }$
  \item $y_{n} = n + 5 \sin \dfrac{ \pi n }{ 2 }$
\end{problem}

\begin{answer}
  \item $ y_2 = y_3 = -5$
  \item $ y_{10} = 20$
  \item $ y_3 = -2$
\end{answer}

\begin{problem}
  {Find the largest term of each sequence:}
  \item $y_n = \dfrac{ 90n }{ n^2 + 9 }$
  \item $y_n = \dfrac{ 10^n }{ n! }$
\end{problem}

\begin{answer}
  \item $y_3 = 15$
  \item $y_9 = y_{10} = \dfrac{ 10^9 }{ 9! }$
\end{answer}

\end{document}

相关内容