3

I have a document which looks like this, with a custom question command:

\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}

This may be beyond the scope of Latex, but is it possible to output the odd questions and their answers on one page (questions 1,3,5,..) and then have a pagebreak followed by the even questions and answers (questions 2,4,6,...)? I realize I could manually do this, but the questions have somewhat of a sequential nature (2 helps you answer 3) and typing them up in order is helpful to me. I wasn't able to find a similar question elsewhere.

Almacomet
  • 361
  • 1
    You could try to use a if clause (like if the pagenumber is even do this if not that this …). – current_user Sep 04 '18 at 17:10
  • @current_user Thanks, using "conditional" helped me search better. I'm looking at this and will get back. – Almacomet Sep 04 '18 at 17:16
  • Do you have to use this format or are you open to different ways of setting this out? Also, how "complicated" can the prompts and answers be? –  Sep 04 '18 at 17:18
  • Does any of your code rely on category code changes (so do you use \verb or a verbatim like environment -- this includes listings)? Would it be ok for you to use an environment for input (I guess that the answers should be moved, too)? – Skillmon Sep 04 '18 at 17:24
  • @Andrew The template is from here. I am rather fond of the look, but I see the answers package can do what I ask, if I concede the stylistic part. This is for math based homework, so answers/prompts may have inline equations, equations, proof environments, etc. There may occasionally be tables in either, but I can concede this. – Almacomet Sep 04 '18 at 17:37
  • @Skillmon I may use a verbatim like environment occasionally, but it would be rare enough that if I am giving that up, it is okay. – Almacomet Sep 04 '18 at 17:38

2 Answers2

2

Here's the idea: you input the answer together with the question, saving it in a token register that can be delivered when you wish.

\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

The requested variant:

\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

egreg
  • 1,121,712
  • Thank you, that's very close. I wanted it to look more like this, sorry if that was unclear. I changed your answer command to remedy this, would it be appropriate for me to edit your post? – Almacomet Sep 04 '18 at 18:19
  • @Almacomet Added the variant – egreg Sep 04 '18 at 20:05
1

Here is an expl3 variant that puts the problems into "even" and "odd" sequences and then prints them using command \evenquestions and \oddquestions, respectively. I have put the answers into environments as from the OP they can be relatively complicated.

Here's the output:

enter image description here

You can of course put the odd and even numbered questions on separate pages by putting something like \newpage in between the \oddquestions and \evenquestions commands.

Here is the code:

\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}

If all question numbers will appear (in the sense that if question k+1 appears then question k>0 also appears), then I would omit the first argument to the question environment and instead automatically insert it using a counter.

Edit

Rather using sequences I realised that it is better to use token lists. The following code does it this way, with identical output. For good measure, I have automated the question numbers:

\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}