Formatting Questions/Answers in book

90 views
Skip to first unread message

Aka Steve

unread,
Jan 23, 2024, 3:24:16 PMJan 23
to LaTeX Users Group
Hi everyone, 
                      I'm in the process of writing a maths workbook for the students that teach. There are exercises in the book with the answers located at the back. The question format looks like this.

1 (a) (i)
          (ii)
     (b) (i)
            (ii)
      (c) (i)
            (ii)
2

When a question is created, the answer is entered one time and printed at the back. I'm having some difficulty with the formating. There seems to be an overlap in the labelling when I create certain types of questions and the question numbering is thrown off and I'm not sure why. Here is a MWE where the the problem can be replicated (Overleaf).

\documentclass{book}

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

\setlength{\parindent}{0pt}

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

\newcounter{exblock}

\newlist{exercises}{enumerate}{3}
\setlist[exercises]{font=\bfseries}
\setlist[exercises,1]{label=Q\arabic*}
\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}
\begin{subparts}
\subpart{arg1}{arg2}
\subpart{arg1}{arg2}
\subpart{arg1}{arg2}
\subpart{arg1}{arg2}
\end{subparts}

\begin{subparts}
\subpart{arg1}{arg2}
\subpart{arg1}{arg2}
\subpart{arg1}{arg2}
\subpart{arg1}{arg2}
\end{subparts}

            \begin{subparts}
\subpart{arg1}{arg2}
\subpart{arg1}{arg2}
\subpart{arg1}{arg2}
\subpart{arg1}{arg2}
\end{subparts}

 

\end{subquestions}

 
 
\end{exercises}

\chapter{Appendix}

\section{Solutions}

\printsolutions

\end{document}

Peter Flynn

unread,
Jan 24, 2024, 6:34:14 AMJan 24
to latexus...@googlegroups.com
On 23/01/2024 21:24, Aka Steve wrote:
> I'm in the process of writing a maths workbook for the students that
> teach. There are exercises in the book with the answers located at
> the back. [...] I'm having some difficulty with the formatting. There
> seems to be an overlap in the labelling when I create certain types
> of questions and the question numbering is thrown off and I'm not
> sure why. Here is a MWE

I ran this successfully here under XeLaTeX Version
3.14159265-2.6-0.999991 (TeX Live 2019/Debian). I don't see any sign of
overlapping or numbering errors.

Peter
test.pdf

Ariovaldo V Garcia

unread,
Jan 24, 2024, 6:52:38 AMJan 24
to latexus...@googlegroups.com
Hi, Peter

In your pdf, look below item Q3. Items (b) and (c) overlap with subitem (i).

Ariovaldo

--
You received this message because you are subscribed to the Google Groups "LaTeX Users Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to latexusersgro...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/latexusersgroup/4e3df515-23be-49b7-bfcb-c16c603712eb%40gmail.com.

Peter Flynn

unread,
Jan 24, 2024, 7:19:49 AMJan 24
to latexus...@googlegroups.com


On 24/01/2024 12:52, Ariovaldo V Garcia wrote:
> Hi, Peter
>
> In your pdf, look below item Q3. Items (b) and (c) overlap with subitem (i).

Sorry, I missed that. I thought you mean overlap in the Answers.

I'll look at the code but not today.

Peter

Ariovaldo V Garcia

unread,
Jan 24, 2024, 7:31:43 AMJan 24
to latexus...@googlegroups.com
OK.

See, Aka Steve <terry...@gmail.com> was the one who sent the question

Regards


Aka Steve terry...@gmail.com  that sent the 


--
You received this message because you are subscribed to the Google Groups "LaTeX Users Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to latexusersgro...@googlegroups.com.

Terry David

unread,
Jan 24, 2024, 2:51:39 PMJan 24
to latexus...@googlegroups.com
Hi, Yes, the answers are formatting properly, but the overlap is in the actual questions.

You received this message because you are subscribed to a topic in the Google Groups "LaTeX Users Group" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/latexusersgroup/-b8xOm2htsY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to latexusersgro...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/latexusersgroup/CAFjDMc%2BBETY8Jty8F2X-xhSQ16T9bEJGVOgBMhxKjR4jZy6n3w%40mail.gmail.com.

Aka Steve

unread,
Jan 29, 2024, 12:38:36 AMJan 29
to LaTeX Users Group
Hi Peter, Have you gotten a chance to review the code?

Peter Flynn

unread,
Jan 29, 2024, 8:57:18 AMJan 29
to latexus...@googlegroups.com
On 29/01/2024 05:38, Aka Steve wrote:
> Hi Peter, Have you gotten a chance to review the code?

Sorry for the delay — family problems.

OK, there is a major logical disconnect here. You are trying to GENERATE
an implicit heading in the level ABOVE the one you address, eg:

% this one is fine, it's a question at level 1 so it's Q1
\question{This is a test question}{solution 1}

% This is deeply misleading because there is no Q2. But it works because
% the item for it is embedded in the environment.
\begin{subquestions}
\subquestion{testing1a}{testing answer1}
\subquestion{testing1b}{testing answer2}
\subquestion{testing1c}{testing answer3}

The same applies to the subparts environment.

Weirdly, if I duplicate the first subparts environment (so I now get an
(e) with three subparts, exactly the same as (d) has), the formatting is
fine.

But in Q3, where the code structure is identical, it puts the level 2
counter in the wrong place for the second and subsequent items. BUT most
obviously, there is a GAP between the level 3 blocks which is absent in Q2.

This is a good indicator of the problem when a list jumps from an outer
level to a deeper level without passing through the intervening level,
eg from 1 to 3 without a 2...or in your case in Q3, jumping strait to
subparts without there being a subquestion present.

This works OK

\begin{subquestions}
\subquestion{dummy}{testing dummy}
\begin{subparts}
\subpart{arg1}{arg2}
\subpart{arg1}{arg2}
\subpart{arg1}{arg2}
\subpart{arg1}{arg2}
\end{subparts}
\begin{subparts}
\subpart{arg1}{arg2}
\subpart{arg1}{arg2}
\subpart{arg1}{arg2}
\subpart{arg1}{arg2}
\end{subparts}
\begin{subparts}
\subpart{arg1}{arg2}
\subpart{arg1}{arg2}
\subpart{arg1}{arg2}
\subpart{arg1}{arg2}
\end{subparts}
\end{subquestions}

You will need to create some kind of dummy command to compensate for the
fact that subquestions expects a subquestion before launching into
subparts. That is the nature of contained hierarchies.

Peter
Reply all
Reply to author
Forward
Message has been deleted
0 new messages