I am currently working on a document class for homework problems which should have a certain layout. To this aim I have created a new environment for the problems and it already works to some extend, however it still has some bugs.
The layout of the problems should be as follows: The problem number should appear on the left side of the page with an optional box containing the points for the problem underneath. Optionally it should also be possible to print an alternative text such as 'Bonus' instead of the number of the problem. The text of the problem should then appear on the right side with the first line starting on the baseline of the problem number.
My current implementation of the problem environment looks as follows (a full MWE can be found below):
\newcounter{Problem}
\NewDocumentEnvironment{Problem}{o D(){\theProblem)}}
{%
\refstepcounter{Problem}%
% Problem number as margin note
\noindent\marginpar{%
\begin{center}
\textbf{\large #2}
\IfNoValueF{#1}{% If arg1 given
% Create box with points
\vspace*{0.6\baselineskip}\par
\framebox[1.1\width]{#1 Points}}
\end{center}%
}%
\ignorespaces%
}{%
\vspace{\baselineskip}
}
In order to obtain the desired layout I tweaked the page margins and reversed the marginpar such that it appears on the left side of the page.
I have the following problems with the current implementation of the problem environment (all problems are also documented in the MWE below):
- If the text of a problem has too few lines, the problem number of a following problem get shifted downwards in the margin in order to not overlap with the previous problem and points. This causes the base line of the following problem to be not aligned with the problem number.
- If I give the problem a label an unwanted whitespace appears at the beginning since
\ignorespaces
does not work as desired in this case. This could be avoided by using\begin{Problem}\label{prob}%
but it is likely to be forgotten. - If the problem starts with an
enumerate
oritemize
environment an unwanted empty line ins inserted at the beginning. Of course this could be fixed by using\vspace
but maybe there is a more elegant solution?
I think all these problems would be fixed if the text at the beginning of a problem would behave like the text at the beginning of a section, where the problem number is regarded as the section heading, but I dont know how to achieve this.
Here comes a full MWE which also demonstrates the three mentioned problems:
\documentclass[a4paper,12pt]{article}
\usepackage{geometry}
\usepackage{enumitem}
\usepackage{xparse}
% Page layout
\geometry{
top=0.07\paperheight,
bottom=0.1\paperheight,
left=0.2\paperwidth,
right=0.07\paperwidth,
marginparwidth=0.1\paperwidth,
marginparsep=0.03\paperwidth
}
% Put margin notes on left side
\reversemarginpar
% Problem environment
\newcounter{Problem}
\NewDocumentEnvironment{Problem}{o D(){\theProblem)}}
{%
\refstepcounter{Problem}%
% Problem number as margin note
\noindent\marginpar{%
\begin{center}
\textbf{\large #2}
\IfNoValueF{#1}{% If arg1 given
% Create box with points
\vspace*{0.6\baselineskip}\par
\framebox[1.1\width]{#1 Points}}
\end{center}%
}%
\ignorespaces%
}{%
\vspace{\baselineskip}
}
\begin{document}
\begin{Problem}[10]
A problem\\with many lines\\and no label\\works well.
\end{Problem}
\begin{Problem}[10](Bonus)
Another problem with only one line.
\end{Problem}
\begin{Problem}\label{prob:3}
A problem with a label suffers from an unwanted whitespace at the
beginning. Furthermore it does not start at the baseline of the
problem number since the previous problem was very short.
\end{Problem}
\begin{Problem}
\begin{enumerate}[leftmargin=*]
\item A problem that starts with an enumerate or itemize environment
\item has a newline at the beginning such that the first line of
text does not start at the baseline of the problem number.
\end{enumerate}
\end{Problem}
\end{document}
I would be very thankful for any help or advises!
Edit: I had partial success by using \trivlist\item\relax
instead of \ignorespaces
in the definition of my environment. This solves at least problems 2. and 3. but introduces some additional vertical space at the beginning of the text which I have to correct for. My current implementation looks as follows:
\newcounter{Problem}
\NewDocumentEnvironment{Problem}{o D(){\theProblem)}}
{%
\refstepcounter{Problem}%
% Problem number as margin note
\noindent\marginpar{%
\parbox[t]{\marginparwidth}{%
\centering
\textbf{\large #2}%
\IfNoValueF{#1}{% If arg1 given
% Create box with points
\vspace*{0.6\baselineskip}\par%
\framebox[1.1\width]{#1 Points}}%
}%
}%
% trivlist introduces some space which we have to correct
\vspace*{-2.5\topsep}%
\trivlist\item\relax%
}{%
\endtrivlist%
\vspace*{\baselineskip}
}
But still, if a problem just has a single line, then the number of the following problem is shifted downwards and consequently is not correctly aligned with the text. Therefore I would be thankful for suggestions on how to improve the implementation of my environment.
\vspace*{\baselineskip}
in the second to last line of your edited MWE in something like\vspace{2cm}
(or however much you'd like, 2cm might be a bit too much). It's not the most fancy solution, but then at least you have the same amount of space between the text of each question, and they won't be too close together when there is a question with just one line.\marginpar
. First is to use some sort of tabular environment, and put the question number in one column and the question text in the other. Second is to throw both the question number and the question text in the same line of text, each wrapped in individual minipages. Both have drawbacks.