0

I'm working on a textbook with sections and subsections, where some subsections are dedicated to exercises.

\documentclass[12pt]{book}

\begin{document}

\section{First!}
\subsection{Some theorems}
Theorem I guess...
\subsection{Exercises}
\begin{enumerate}
\item How much is 1+1?
\item How much is 2+3?
\end{enumerate}

\section{Second!}
\subsection{Let's define something}
A natural number is a number different from a fraction.
\subsection{More theorems}
I like fractions.
\subsection{Exercises}
\begin{enumerate}
\item Is $\pi$ a natural number?
\item Do you like natural numbers?
\end{enumerate}

\end{document}

enter image description here

I would like to add the solutions in the code and have them gathered for a solutionfile. I know packages like answers and exsheets can do a good job, but I would like to keep the lay-out of the questions, while having one large solution-file, with automated numbering (in which it is clear which answer corresponds with which question).

Preferably something like

enter image description here

Keep in mind the number of exercises per subsection can be more then 10, so I'd like to keep my solutions (in code) close to the questions.

3
  • The tricky bit is that things like \thesection need to be expanded immediately and things like \textbf or \hspace* should not be expanded. This is usually accomplished using \string or \protected. Alternatively, one can predefine a number of formats and simply pass an indicator. – John Kormylo Aug 17 '17 at 15:22
  • @JohnKormylo How can I predefine those formats to get my result? – Yannick Neyt Aug 18 '17 at 7:50
  • Book defines section as \@startsection {section}{1}{\z@}% {-3.5ex \@plus -1ex \@minus -.2ex}% {2.3ex \@plus.2ex}% {\normalfont\Large\bfseries}} One can find a listing of \@startsection in source2e.pdf from CTAN. The arguments control the indentation, vertical spacing and font used. For a single paragraph, one can fake enumerate using \noindent\makebox[\labelwidth][r]{...}\hspace{\labelsep}\parbox{\dimexpr \textwidth-\labelwidth-\labelsep}{...} – John Kormylo Aug 18 '17 at 14:38
0

I've been tinkering with some settings and the answers package, and have settled for the following solution:

\documentclass[12pt]{article}

\usepackage{answers}
\Newassociation{opdr}{Opdrachten}{ans}
\Newassociation{opl}{Oplossing}{ans}
\renewenvironment{Opdrachten}[1]{\begin{trivlist}\item \bfseries \large #1}{\end{trivlist}}
\renewenvironment{Oplossing}[1]{\begin{enumerate}\item[\bfseries #1.]}{\end{enumerate}}

\begin{document}
\Opensolutionfile{ans}[ans]
\section{First!}
\subsection{Some theorems}
Theorem I guess...

\subsection{Opdrachten}\label{Opdr_First1}
\begin{opdr}p.\pageref{Opdr_First1}\end{opdr}
\begin{enumerate}
    \item How much is 1+1?
        \begin{opl}
        2
        \end{opl}
    \item How much is 2+3?
        \begin{opl}
        5
        \end{opl}
\end{enumerate}

\section{Second!}
\subsection{Let's define something}
A natural number is a number different from a fraction.
\subsection{More theorems}
I like fractions.

\subsection{Opdrachten}\label{Opdr_Second1}
\begin{opdr}p.\pageref{Opdr_Second1}\end{opdr}
\begin{enumerate}
    \item Is $\pi$ a natural number?
        \begin{opl}
            Yes.
        \end{opl}
    \item Do you like natural numbers?
        \begin{opl}
            No.
        \end{opl}
    \item This question doesn't have an answer.

    \item This question does.
        \begin{opl}
            Yeah.
        \end{opl}
\end{enumerate}

\Closesolutionfile{ans}
\newpage
\section*{Oplossingen}
\input{ans}

\end{document}

enter image description here

I like the lay-out, plus, I was able to include the page numbers.

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.