按奇数/偶数分割命令输出

按奇数/偶数分割命令输出

我有一个如下所示的文档,其中有一个自定义问题命令:

\documentclass[11pt]{article}

\newcommand\question[2]{\setcounter{qnum}{#1}\vspace{.25in}\hrule\textbf{#1:} \textit{#2}\vspace{.5em}\hrule\vspace{.10in}}
\newcounter{qnum}

\begin{document}
\question{1}{Prompt 1}

Answer 1

\question{2}{Prompt 2}

Answer 2

\question{3}{Prompt 3}

Answer 3

\end{document}

这可能超出了 Latex 的范围,但是否可以将奇数问题及其答案输出到一页(问题 1、3、5、...),然后分页后跟偶数问题和答案(问题 2、4、6、...)?我知道我可以手动执行此操作,但问题具有一定的连续性(2 可帮助您回答 3),按顺序输入它们对我有帮助。我在其他地方找不到类似的问题。

答案1

这个想法是这样的:你将答案与问题一起输入,将其保存在可以随时传送的令牌寄存器中。

\documentclass[11pt]{article}

\newcommand\question[3]{%
  \par\vspace{.25in}\hrule\nopagebreak\vspace{.5em}
  \textbf{#1:} \textit{#2}\par\nopagebreak\vspace{.5em}
  \hrule\vspace{.10in}%
  \ifodd#1\relax
    \global\oddanswers=\expandafter{\the\oddanswers\answer{#1}{#3}}%
  \else
    \global\evenanswers=\expandafter{\the\evenanswers\answer{#1}{#3}}%
  \fi
}
\newtoks\oddanswers
\newtoks\evenanswers
\newcommand{\answer}[2]{%
  \par\noindent Answer to #1: #2\par
}
\newcommand{\printoddanswers}{\the\oddanswers}
\newcommand{\printevenanswers}{\the\evenanswers}

\begin{document}

\question{1}{Compute $1+1$}{$2$}
\question{2}{Compute $1+2$}{$3$}
\question{3}{Compute $1+3$}{$4$}
\question{4}{Compute $1+4$}{$5$}
\question{5}{Compute $1+5$}{$6$}

\section{Answers to odd numbered exercises}
\printoddanswers

\section{Answers to even numbered exercises}
\printevenanswers


\end{document}

enter image description here

请求的变体:

\documentclass[11pt]{article}

\newcommand\question[3]{%
  \ifodd#1\relax
    \global\oddquestions=\expandafter{%
      \the\oddquestions
      \problem{#1}{#2}%
      \answer{#1}{#3}%
    }%
  \else
    \global\evenquestions=\expandafter{%
      \the\evenquestions
      \problem{#1}{#2}%
      \answer{#1}{#3}%
    }%
  \fi
}
\newtoks\oddquestions
\newtoks\evenquestions

\newcommand{\problem}[2]{%
  \par\vspace{.25in}\hrule\nopagebreak\vspace{.5em}
  \textbf{#1:} \textit{#2}\par\nopagebreak\vspace{.5em}
  \hrule\vspace{.10in}%
}
\newcommand{\answer}[2]{%
  \par\noindent #2\par
}
\newcommand{\printoddquestions}{\the\oddquestions}
\newcommand{\printevenquestions}{\the\evenquestions}

\begin{document}

\question{1}{Compute $1+1$}{$2$}
\question{2}{Compute $1+2$}{$3$}
\question{3}{Compute $1+3$}{$4$}
\question{4}{Compute $1+4$}{$5$}
\question{5}{Compute $1+5$}{$6$}

\section{Odd numbered exercises}
\printoddquestions

\section{Even numbered exercises}
\printevenquestions


\end{document}

enter image description here

答案2

这是一个解释3将问题放入“偶数”和“奇数”序列,然后分别使用命令\evenquestions和打印它们\oddquestions。我已将答案放入环境中,因为从 OP 来看,它们可能相对复杂。

输出如下:

enter image description here

当然,您可以将奇数和偶数问题放在不同的页面上,方法是\newpage\oddquestions\evenquestions命令之间放置类似的内容。

以下是代码:

\documentclass{article}
\usepackage{expl3}
\usepackage{environ}

\ExplSyntaxOn
\newcommand\Question[2]{%
   \par\vspace{.25in}\hrule\vspace{0.5em}
   \noindent\textbf{#1:}~\textit{#2}\par
   \vspace{.5em}\hrule\vspace{.10in}
}
\seq_new:N \g_even_questions_seq
\seq_new:N \g_odd_questions_seq
\cs_new:Npn \add_to_sequence #1 #2 #3 #4 {
  \seq_gput_right:Nn #1 { \Question{#2} {#3} }
  \seq_gput_right:No #1 { #4 }
}
\NewEnviron{question}[2]{
  \int_if_odd:nTF {#1}
    { \add_to_sequence \g_odd_questions_seq {#1}{#2}{\BODY}}
    { \add_to_sequence \g_even_questions_seq {#1}{#2}{\BODY}}
}

\newcommand\oddquestions{
  \section{Odd~numbered~questions}
  \seq_map_inline:Nn \g_odd_questions_seq {##1}
}

\newcommand\evenquestions{
  \section{Even~numbered~questions}
  \seq_map_inline:Nn \g_even_questions_seq {##1}
}

\ExplSyntaxOff

\begin{document}

  \begin{question}{1}{Prompt 1}
    Answer 1
  \end{question}

  \begin{question}{2}{Prompt 2}
    Answer 2
  \end{question}

  \begin{question}{3}{Prompt 3}
    Answer 3
  \end{question}

  % now print the odd and even questions

  \oddquestions

  %\newpage

  \evenquestions

\end{document}

如果所有问题编号都会出现(即如果问题 k+1 出现则问题 k>0 也会出现),那么我会省略question环境的第一个参数,而是使用计数器自动插入它。

编辑

我意识到,使用标记列表比使用序列更好。以下代码就是按照这种方式执行的,输出相同。为了保险起见,我已将问题编号自动化:

\documentclass{article}
\usepackage{expl3}
\usepackage{environ}

\ExplSyntaxOn
\newcommand\Question[2]{%
   \par\vspace{.25in}\hrule\vspace{0.5em}
   \noindent\textbf{#1:}~\textit{#2}\par
   \vspace{.5em}\hrule\vspace{.10in}
}
\int_new:N \g_question_int
\tl_new:N \g_even_questions_tl
\tl_new:N \g_odd_questions_tl
\cs_new:Npn \add_to_questions #1 #2 #3 {
  \tl_gput_right:Nn #1 { \Question{\int_eval:n {\g_question_int}} {#2} }
  \tl_gput_right:No #1 { #3 }
}
\cs_generate_variant:Nn \int_if_odd:nTF {NTF}
\NewEnviron{question}[1]{
  \int_gincr:N \g_question_int
  \int_if_odd:NTF \g_question_int
    { \add_to_questions \g_odd_questions_tl  {#1} {\BODY} }
    { \add_to_questions \g_even_questions_tl {#1} {\BODY} }
}

\newcommand\oddquestions{
  \section{Odd~numbered~questions}
  \tl_use:N \g_odd_questions_tl
}

\newcommand\evenquestions{
  \section{Even~numbered~questions}
  \tl_use:N \g_even_questions_tl
}

\ExplSyntaxOff

\begin{document}

  \begin{question}{Prompt 1}
    Answer 1
  \end{question}

  \begin{question}{Prompt 2}
    Answer 2
  \end{question}

  \begin{question}{Prompt 3}
    Answer 3
  \end{question}

  % now print the odd and even questions

  \oddquestions

  %\newpage

  \evenquestions

\end{document}

相关内容