[Tex/LaTex] Typesetting exercise sheets

exerciseskoma-script

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}
\titleformat{\subsection}{\large\bfseries\sffamily}{}{0pt}{Exercise \thesubsection:\quad}

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

\addtocounter{section}{1}\setcounter{subsection}{0}
\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?

Related: How to implement a verbatim or null environment using a boolean within \newcommand

Best Answer

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{
  headings = block-subtitle ,
  headings-format = \large\bfseries\sffamily ,
  % 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:

enter image description here

\documentclass{scrartcl}

\usepackage{exsheets}[2014/09/14] % v0.16 or newer
\SetupExSheets{
  headings = block-subtitle ,
  headings-format = \large\bfseries\sffamily ,
  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:
% \DeclareInstance{exsheets-heading}{block-subtitle}{default}{
%   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

enter image description here

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

enter image description here

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

Related Question