1

I am tweaking the template for final exams which unfortunately need to follow some formatting guidelines. In particular, the question numbering needs to look like this:

1.0 Question 1
1.1 Part 1 of question 1
1.2 Part 2 of question 1
...
2.0 Question 2
2.1 Part 1 of question 2
2.2 Part 2 of question 2
2.3.0 Part 3 of question 2
2.3.1 Subpart 1 of part 3 of question 2
2.3.2 Subpart 2 of part 3 of question 2
2.4 Part 4 of question 2
...

The problem is that the part labels should sometimes contain two numbers (if the parts have no subparts) but sometimes three numbers (if the ports do have subparts), the last one being a zero. I already successfully implemented a formatting where there are always two numbers, or always three numbers, but I didn't manage to combine them so far.

Here is my code for two number labels:

\renewcommand{\questionlabel}{\thequestion.0 }
\renewcommand{\partlabel}{\thequestion.\arabic{partno} }
\renewcommand{\partshook}{
    \settowidth{\leftmargin}{}
    \labelwidth\leftmargin\advance\labelwidth-\labelsep
}
\renewcommand{\questionshook}{
    \setlength{\labelsep}{0pt}
    \setlength{\labelwidth}{2em}
}

And here is my code for three number labels:

\renewcommand{\questionlabel}{\thequestion.0.0 }
\renewcommand{\partlabel}{\thequestion.\arabic{partno}.0 }
\renewcommand{\subpartlabel}{\thequestion.\arabic{partno}.\arabic{subpart} }
\renewcommand{\questionshook}{
    \setlength{\labelsep}{0pt}
    \setlength{\labelwidth}{3em}
}
\renewcommand{\partshook}{
    \settowidth{\leftmargin}{}
    \labelwidth\leftmargin\advance\labelwidth-\labelsep
}
\renewcommand{\subpartshook}{
    \settowidth{\leftmargin}{}
    \labelwidth\leftmargin\advance\labelwidth-\labelsep
}

So now it's all about combining them depending on whether a part has subparts where I fail.

edit: Here is a MWE:

\documentclass[a4paper,ngerman,12pt]{exam}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[ngerman]{babel}
\usepackage[left=2cm,right=2cm,top=2cm,bottom=1cm]{geometry}


\pointformat{/\thepoints}
\addpoints
\pointsdroppedatright 

%%% For two numbers labels %%%
\renewcommand{\questionlabel}{\thequestion.0 } %% Has one zero added
\renewcommand{\partlabel}{\thequestion.\arabic{partno} } %% Has no zeros added
\renewcommand{\partshook}{
    \settowidth{\leftmargin}{}
    \labelwidth\leftmargin\advance\labelwidth-\labelsep
}
\renewcommand{\questionshook}{
    \setlength{\labelsep}{0pt}
    \setlength{\labelwidth}{2em}
}


%%% For three numbers labels %%%
\renewcommand{\questionlabel}{\thequestion.0.0 } %% Has two zeros added
\renewcommand{\partlabel}{\thequestion.\arabic{partno}.0 } %% Has one zero added
\renewcommand{\subpartlabel}{\thequestion.\arabic{partno}.\arabic{subpart} }
\renewcommand{\questionshook}{
    \setlength{\labelsep}{0pt}
    \setlength{\labelwidth}{3em}
}
\renewcommand{\partshook}{
    \settowidth{\leftmargin}{}
    \labelwidth\leftmargin\advance\labelwidth-\labelsep%
}
\renewcommand{\subpartshook}{
    \settowidth{\leftmargin}{}
    \labelwidth\leftmargin\advance\labelwidth-\labelsep
}

\begin{document}
    \begin{questions}
        \question question 1
        \begin{parts}
            \part[5] part 1 of question 1
            \droppoints
            \part part 2 of question 1
            \begin{subparts}
                \subpart[2] subpart 1 of part 2 of question 1
                \droppoints
                \subpart[4] subpart 2 of part 2 of question 1
                \droppoints
            \end{subparts}
        \end{parts}
        \question question 2
    \end{questions}
\end{document}

Here is how it looks like and where the problem lies:

Problem with labels

Here is how the current solution proposal looks like:

Problem with paragraph width

8
  • Try setting \labelwidth the same for each and use \setlength{\csname @totalleftmargin\endcsname}{0pt} (you might need some \expandafters). Apr 10, 2023 at 2:37
  • Thanks, but how do I do the \renewcommand{\partlabel}{...} call? In one case there is a trailing ".0" and in one case there isn't...
    – Photon
    Apr 10, 2023 at 6:56
  • 1
    A Minimal Working Example (MWE) would be helpful here. Apr 10, 2023 at 15:10
  • Added, thanks for the hint!
    – Photon
    Apr 10, 2023 at 16:15
  • 1
    The good news is that the aux file contains \newlabel{subpart@1@2@1}{{i}{1}} so that you can check whether there are subparts ahead of time (second run). \@ifundefined{r@subpart@1@1@1} would tell you that 1.1 contains no subparts. Apr 10, 2023 at 16:48

1 Answer 1

1

I suspect the part and subpart labels are right jusified inside \labelwidth, hense the need for \hbox to \labelwidth. Also, it seems the hooks occur after \linewidth is set.

\documentclass[a4paper,ngerman,12pt]{exam}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[ngerman]{babel}
\usepackage[left=2cm,right=2cm,top=2cm,bottom=1cm, showframe]{geometry}
\usepackage{lipsum}

\pointformat{/\thepoints}
\addpoints
\pointsdroppedatright 

\makeatletter
\renewcommand{\thequestion}{\arabic{question}\@ifundefined{r@part@\arabic{question}@1}{\phantom{.0}}{.0}}
\renewcommand{\thepartno}{\arabic{question}.\arabic{partno}\@ifundefined{r@subpart@\arabic{question}@\arabic{partno}@1}
  {\phantom{.0}}{.0}}
\renewcommand{\thesubpart}{\arabic{question}.\arabic{partno}.\arabic{subpart}} %% Has no zeros added
\renewcommand{\questionlabel}{\thequestion\hfill}%% Has two zeros added
\renewcommand{\partlabel}{\hbox to \labelwidth{\thepartno\hfill}}%% Has one zero added
\renewcommand{\subpartlabel}{\hbox to \labelwidth{\thesubpart\hfill}}
\renewcommand{\questionshook}{%
    \setlength{\leftmargin}{3em}%
    \labelwidth\leftmargin\advance\labelwidth-\labelsep
    \setlength{\linewidth}{\textwidth}%
}
\renewcommand{\partshook}{%
    \setlength{\@totalleftmargin}{0pt}%
    \setlength{\leftmargin}{3em}%
    \labelwidth\leftmargin\advance\labelwidth-\labelsep
    \setlength{\linewidth}{\textwidth}%
}
\renewcommand{\subpartshook}{%
    \setlength{\@totalleftmargin}{0pt}%
    \setlength{\leftmargin}{3em}%
    \labelwidth\leftmargin\advance\labelwidth-\labelsep
    \setlength{\linewidth}{\textwidth}%
}
\makeatother

\begin{document}
    \begin{questions}
        \question question 1 \lipsum[2]
        \begin{parts}
            \part[5] part 1 of question 1 \lipsum[2]
            \droppoints
            \part part 2 of question 1
            \begin{subparts}
                \subpart[2] subpart 1 of part 2 of question 1 \lipsum[2]
                \droppoints
                \subpart[4] subpart 2 of part 2 of question 1
                \droppoints
            \end{subparts}
        \end{parts}
        \question[10] question 2
    \end{questions}
\end{document}
5
  • That looks perfect, thanks! However, regarding the points, the problem is even worse, the whole text has different widths, I'll add a screenshot to the original question!
    – Photon
    Apr 11, 2023 at 5:54
  • Added a second screenshot. It used to work without this problem before (see my first screenshot), so it looks like some collateral damage with the label widths maybe?
    – Photon
    Apr 11, 2023 at 6:02
  • Also, exam class is not completely compatible with geometry. Use \extrawidth, etc. instead. OTOH, the points seem to have a fixed right edge. Apr 11, 2023 at 10:51
  • That's great, thanks! Another minor thing which I struggled with, also fixed! But how can I adjust the page margins without geometry, in particular the top and bottom margin?
    – Photon
    Apr 11, 2023 at 15:39
  • Never mind, I think, I found it in the exam class documentation!
    – Photon
    Apr 11, 2023 at 16:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .