1

I am trying to typeset a exam subject with the same numbering as this document by Concours CentraleSupélec.

Sections are numbered with Roman, subsections are numbered with Alph and questions are numbered within each subsection with arabic : that is, question number 2 within subsection A of section I has number I.A.2.

I know how to do this with ntheorem and KOMA scrartcl : it suffices to do

\renewcommand\thesection{\Roman{section}}
\renewcommand\thesubsection{\thesection.\Alph{subsection}}
\newtheorem{question}{Question}[subsection]

And this works.

However, not all sections have subsections. In this case, questions are numbered within the section and with Alph, not arabic. So the third question in a section IV that does not have subsections is numbered IV.C, not IV..3.

I know I can do

\theoremnumbering{Alph}
\renewtheorem{question}{Question}[section]

I am trying to automate this. Clearly, it is not possible, when encountering a new section, to know whether there will be a subsection or not, so one has to change the numbering at each section and again at each subsection (or at least each first subsection within a section).

I could not find a AtBeginSection or AtBeginSubsection in the KOMA-script manual. Is there some hook that I can use to execute some code at each section/subsection without hacking the whole thing ?

  • A complete minimal working example would have been nice. \AtBeginSection and \AtBeginSubsection are beamer commands, I don't know whether there is a package that does provide them to KOMA. Though one could use etoolbox's \pretocmd (as I did) or \apptocmd. – Skillmon Jan 8 '18 at 21:25
1

The following does what you want. I just redefined \thequestion to check for the current \value of subsection (which is \c@subsection) and based on that print it differently.

\documentclass[]{scrartcl}

\usepackage{ntheorem}

\renewcommand*\thesection{\Roman{section}}
\renewcommand*\thesubsection{\thesection.\Alph{subsection}}

\newtheorem{question}{Question}[subsection]
\makeatletter
\renewcommand*\thequestion{%
  \ifnum\c@subsection=0
    \thesection.\Alph{question}%
  \else
    \thesubsection.\arabic{question}%
  \fi
}
\makeatother

\begin{document}
\section{Foo}
\begin{question}
  foo
\end{question}

\section{Foo}
\subsection{Bar}
\begin{question}
  foo
\end{question}
\end{document}

Issue: What if you have something like the following structure:

I. Section
  A. Question
  B. Subsection
    1. Question

Solution: Add a Boolean switch to the \section and \subsection command which indicates whether we are inside a subsection and if so use thesubsection.\arabic{question}. If not step the subsection counter and use \thesection.\Alph{question}. For the patching part I used etoolbox:

\documentclass[]{scrartcl}

\usepackage{ntheorem}
\usepackage{etoolbox}

\newif\ifInsideSubsection
\pretocmd\section{\InsideSubsectionfalse}{}{}
\pretocmd\subsection{\InsideSubsectiontrue}{}{}

\renewcommand*\thesection{\Roman{section}}
\renewcommand*\thesubsection{\thesection.\Alph{subsection}}

\newtheorem{question}{Question}[subsection]
\makeatletter
\renewcommand*\thequestion{%
  \ifInsideSubsection
    \thesubsection.\arabic{question}%
  \else
    \thesection.\Alph{question}%
    \global\advance\c@subsection by 1\relax
  \fi
}
\makeatother

\begin{document}
\section{Foo}
\begin{question}
  foo
\end{question}

\section{Foo}
\begin{question}
  foo
\end{question}
\subsection{Bar}
\begin{question}
  foo
\end{question}
\end{document}
  • 1
    There shouldn't be \relax in the definition of \thequestion; just don't put % after 0. It's questionable to have \advance in \thequestion (second solution). – egreg Jan 9 '18 at 0:09
  • 1
    By the way, you can also say \ifnum\value{subsection}=0, instead of going a level down to \c@subsection. – egreg Jan 9 '18 at 0:13
  • @egreg the \advance is there to ensure consistent numbering (if that is not needed one can just use the first approach). \stepcounter didn't work there and I didn't want to dig to deep into it, so I just used \advance. My first code had \value{subsection} instead of \c@subsection don't know why I changed that but I think there is no harm in doing so. The \relax I started to use based on someone else here on TeX.SX who advised that (neither know who it was nor where exactly) to keep it safe (if % is used). But you're right, it isn't needed. Does it any harm (serious question)? – Skillmon Jan 9 '18 at 8:57
  • Just a note to say sorry for accepting the answer after such a delay --- the CentraleSupélec template is indeed much more complicated than that and I finally opted to diverge from it, but the proposed answer does do what was intended in my first question. – ysalmon Mar 4 '18 at 15:02

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.