# Typesetting exercise sheets

I want to typeset weekly exercise sheets for students. Typically, one sheet consists of 2-5 exercises. Additionallly, a version including solutions shall be provided for the tutors.

Since more or less the same exercises are given out each year, we want to create a collection of exercises which can then be put together in a custom order. So there shall be one main document with the preamble and everything, which then includes or inputs the exercises.

I'm quite happy with the KOMA-Script class scrartcl, the only thing i'd like to have is an automatic numbering for the exercises and the solutions.

An easy (yet maybe not elegant) way to have the exercises numbered as i wish is the following:

\documentclass{scrartcl}

\usepackage{titlesec}

\newcommand{\exercise}[1]{\subsection{#1}}
\newcommand{\solution}[1]{\subsection{#1}}

\begin{document}

\setcounter{section}{1}\setcounter{subsection}{0}
\exercise{This is the Name of the first Exercise}
This is the problem given for the first Exercise.

\exercise{This is the Name of the second Exercise}
This is the problem given for the second Exercise.

\exercise{This is the Name of the third Exercise}
This is the problem given for the third Exercise.
\solution{This is the solution for the third exercis.}
However, it should be titled "Solution 2.1" and not "Exercise 2.2".
\end{document}


However, i still need another command for \solution, which gives out "Solution X.Y: ...", where X.Y is just the numbering of the last exercise.

I know there are a lot of packages like exam, answers, etc. out there, but none did what i wanted yet (could be me, though). In the exercise package i couldn't include figures or minipages the usual way, which i really disliked.

By the way, i don't mind commenting in or out the solutions by hand for the different versions. However, if there's an automated version to only include exercises or exercises and solutions, all the better!

Does anyone have a nice solution for my problem?

-
Welcome to TeX.SX! –  Christian Hupfer Sep 3 at 12:59
I think exsheets is a good alternative –  Christian Hupfer Sep 3 at 13:05
You can also have a look to the acrotex bundle by D.P. Story. I think it has the features you need. –  grames Sep 3 at 13:09
Besides the already mentioned exsheets package (which I personally prefer) there are a number of alternatives, see for example Comparing packages which facilitate typesetting exercises and solutions: exercise vs. answers vs. probsoln –  cgnieder Sep 3 at 13:16
@cgnieder: I just wonder, why you prefer exsheets ;-) –  Christian Hupfer Sep 3 at 13:30

As mentioned in the comments the exsheets package can be used for this. The titles/names of the exercises can be given as a subtitle using the question environment's subtitle option. To actually print the subtitles a headings instance must be used that typesets it. The predefined block-subtitle instance nearly looks the same as your own defined headings so it can be used.

This means we set

\SetupExSheets{
% needs v0.16 2014/09/14 to work:
subtitle-format = \large\bfseries\sffamily
}


which mimics the definitions in your question.

Next thing we need is obviously some mainquestion counter which can be used to print the question numbers as 1.1, 1.2, 2.1, etc. Like you I use the section counter for the task. Then we need

\SetupExSheets{
counter-within = section ,
counter-format = se.qu\IfQuestionSubtitleT{:} ,
}


\IfQuestionSubtitleT{:} ensures that the colon only is typeset when a subtitle is given.

For managing the exercises they can be defined in an external file, myexercises.tex, say. For the example below I saved it with the following contents:

\begin{question}[subtitle=This is the Name of the first Exercise,ID=Q1]
This is the problem given for the first Exercise.
\end{question}
\begin{solution}
This is the first solution.
\end{solution}
\begin{question}[subtitle=This is the Name of the second Exercise,ID=Q2]
This is the problem given for the second Exercise.
\end{question}
\begin{solution}
This is the second solution.
\end{solution}
\begin{question}[subtitle=This is the Name of the third Exercise,ID=Q3]
This is the problem given for the third Exercise.
\end{question}
\begin{solution}
This is the third solution.
\end{solution}


If we put everything together we get:

\documentclass{scrartcl}

\SetupExSheets{
subtitle-format = \large\bfseries\sffamily ,
counter-within = section ,
counter-format = se.qu\IfQuestionSubtitleT{:} ,
% solution/print = true % uncomment for tutors
}

% needed in earlier versions of exsheets:
%   subtitle-format = \large\bfseries\sffamily ,
%   join = {
%     title[r,B]number[l,B](.333em,0pt) ;
%     title[r,B]subtitle[l,B](1em,0pt)
%   } ,
%   attach = {
%     main[l,vc]title[l,vc](0pt,0pt) ;
%     main[r,vc]points[l,vc](\marginparsep,0pt)
%   }
% }

\begin{document}

\stepcounter{section}
\includequestions[IDs={Q1,Q2}]{myexercises}

\stepcounter{section}
\includequestions[IDs=Q3]{myexercises}

\end{document}


If you uncomment the line

% solution/print = true


in the above example you'll get

If you instead add \printsolutions at the end you'll get

Unfortunately there's currently no way to add subtitles to solutions.

-
Thanks for the detailed example! I had some of that already figured out, but especially the counter-within option is really good and the \includequestions command is interesting, too. I still wonder if this works better for me than including each question on it's own, since I have one file for each question anyway. But thanks a lot so far! –  Timitry Sep 4 at 8:25

This is a reduced/simplified version of my package, which is still not ready for publication.

This code defines a command

\Exercise[2][]


where the mandatory argument holds the exercise text and the optional argument with value solution={....}, which is optional.

The solution should be specified in {} for grouping.

The solution content is then written to a file.

(Very simple) Example:

\Exercise[solution={2}]{What is 1 + 1}

All exercises can be in an external file or directly in the preamble, or in the document body.

They are not printed, however, as long as there is no list declared, which allows for selecting particular exercises.

\DeclareExerciseList{1,3,4,5} will choose the exercises 1,3,4 and 5, omitting number two and any other. Non existing exercise numbers will be ignored!

## Driver *.tex/package code

\documentclass{scrartcl}

\usepackage{tcolorbox}%
\usepackage{xkeyval}%
\usepackage{blindtext}%
\usepackage{etoolbox}%
\usepackage{assoccnt}%

\newcounter{realexercisecounter}%
\newcounter{exercise}%
\newcounter{solution}%

\DeclareAssociatedCounters{exercise}{realexercisecounter}%

% Some definitions
\newcommand{\SolutionName}{Solution}
\newcommand{\SolutionPageName}{Solutions}
\newcommand{\ExerciseName}{Exercise}%

\AtBeginDocument{%
\immediate\openout\exerfile=\jobname.solutions
\immediate\write\exerfile{%
\string\setcounter{solution}{0}%
}%
\immediate\write\exerfile{%
\string\clearpage
\string\section*{\SolutionPageName}%
}%
}%

\newwrite\exerfile%

\makeatletter

\define@key{exerkeys}{exercise}{%
\def\exerkeys@@exercise{#1}%
}%

\define@key{exerkeys}{solution}{%
\def\exerkeys@@solution{#1}%
}%

\newcommand{\writesolutiontofile}[1]{%
\immediate\write\exerfile{%
\string\begin{Solution}^^J%
\unexpanded\expandafter{#1}^^J%
\string\end{Solution}^^J%
}%
}%

\newcommand{\Exercise}[2][]{%
\setlength{\parindent}{0pt}%
\refstepcounter{exercise}%
\xifinlist{\number\value{realexercisecounter}}{\exerciselist}{%
\setkeys{exerkeys}{#1}%
\begin{tcolorbox}[width=\textwidth,colbacktitle=red,coltitle=black,title={\ExerciseName~\theexercise}]
#2%
\ifdef{\exerkeys@@solution}{%
\xifinlist{\number\value{realexercisecounter}}{\exerciselist}{%
\immediate\write\exerfile{%
\string\setcounter{solution}{\number\value{exercise}}^^J%
}%
}{}%
\writesolutiontofile{\exerkeys@@solution}%
}{}%
\end{tcolorbox}%
}{}%
\undef\exerkeys@@solution% Must be outside/at the end!
}%

\newenvironment{Solution}{%
\setlength{\parindent}{0pt}%
\refstepcounter{solution}
\begin{tcolorbox}[width=\textwidth,colbacktitle=green,coltitle=black,
title={\SolutionName~\thesolution}]
}{\end{tcolorbox}}%

\makeatother%

\AtEndDocument{%
\immediate\closeout\exerfile%
\cleardoublepage%
\InputIfFileExists{\jobname.solutions}{}{}%
}%

\newcommand{\DeclareExerciseList}[1]{%
\undef{\exerciselist}{}%
}%

\begin{document}

\DeclareExerciseList{1,3,4,5}%

\InputIfFileExists{exercises}{}{Ooopps!}

\end{document}


## exercises.tex

\Exercise[solution={Yes, it's a question}]{First Question}

\Exercise[solution={They are thin at one end, thick in the middle and thin on the end again}]{%

Question: What is special about Brontosaurs?
}%

\Exercise[solution={to be done}]{%

Proof
$$a^2 + b^2 = c^2$$
}%

\Exercise[solution={\textcolor{blue}{\blindtext}}]{%
Cite a Latin Text
}%

\Exercise[]{%
\textcolor{red}{\textbf{Cite a Latin text again}} % Won't be shown as solution
}%


-

To solve your problem, I would provide every single exercise sheet in a single document (tex-file) like this:

-- sheet1.tex:

\documentclass{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage[ngerman]{babel}

% generate blindtext
\usepackage{blindtext}

% add dot after section number
\makeatletter
\makeatother

% define command for exercise and solution
\newcommand{\problem}[1]{\section{Exercise}\subsection*{Problem}{#1}}
\newcommand{\solution}[1]{\subsection*{Solution for Problem \arabic{section}}{#1}}

\begin{document}
\title{Sheet 1}
\maketitle

% testing without own commands
\section{Exercise}

\subsection*{Problem}
\blindtext

\subsection*{Solution for Problem \arabic{section}}
\blindtext

% using own commands defined above

\end{document}


Save the second sheet in a file like the first one (in this example here it is just a renamed copy of "sheet1.tex):

-- sheet2.tex:

\documentclass{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage[ngerman]{babel}

% generate blindtext
\usepackage{blindtext}

% add dot after section number
\makeatletter
\makeatother

% define command for exercise and solution
\newcommand{\problem}[1]{\section{Exercise}\subsection*{Problem}{#1}}
\newcommand{\solution}[1]{\subsection*{Solution for Problem \arabic{section}}{#1}}

\begin{document}
\title{Sheet 2}
\maketitle

% testing without own commands
\section{Exercise}

\subsection*{Problem}
\blindtext

\subsection*{Solution for Problem \arabic{section}}
\blindtext

% using own commands defined above

\end{document}


To produce an "overall document" with all exercise sheets ever handed out, you can use the standalone package to ignore the preamble of external files when using input or include to import that external file. So for the "overall document" you need to compile "allsheets.tex" only:

-- allsheets.tex:

\documentclass{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage[ngerman]{babel}

% input external file but ignore the preamble
\usepackage{standalone}

% generate blindtext
\usepackage{blindtext}

% add dot after section number
\makeatletter
\makeatother

% define command for exercise and solution
\newcommand{\problem}[1]{\section{Exercise}\subsection*{Problem}{#1}}
\newcommand{\solution}[1]{\subsection*{Solution for Problem \arabic{section}}{#1}}

\begin{document}
\title{All Sheets}
\maketitle

\input{sheet1.tex}
\input{sheet2.tex}

\end{document}


This approach will provide single exercise sheets during the semester/course and a "overall document" at the end of the semester/course.

-
I like this possibility, too! However, I will try out the exsheets package first, since I could possibly be needing some of it's special features in the future. Still, thanks for this alternative solution! –  Timitry Sep 4 at 8:27
@Timitry: It was just a very general way to solve your problem without any additional packages... –  albert Sep 4 at 12:07