[Tex/LaTex] How to use section in exam class

examsectioning

I am using latex exam class and need to sectioning questions like

\documentclass[12pt,paper=a4,answers]{exam}
  \begin{document}
    \begin{questions}
    \section{First Section}
    \question
    First question
    \question
    Second question
    \section{Second Section}
    \question
    First question
    \question
    Second question
  \end{questions}
\end{document}

This works, but doesn't align properly under section heading as well as not numbering like 1.1, 1.2 etc. Any option for that?

Probably a solution is updating question font and size to that of a section heading and use part/sub part. Any help to do this?

Best Answer

Here is one solution, although I'm not sure how this will affect the built in answers and grading. Normally exam organizes by \question, \part and \subpart.

\documentclass[12pt,paper=a4,answers]{exam}
\usepackage{etoolbox}
\usepackage{blindtext}% MWE only

\renewcommand{\thequestion}{\thesection.\arabic{question}}
\patchcmd{\questions}{10.}{\thequestion.}{}{}% fix left margin

\begin{document}
  \section{First Section}
  \begin{questions}
    \question
    \blindtext
    \question
    Second question
    \end{questions}
  \section{Second Section}
  \begin{questions}
    \question
    First question
    \question
    Second question
  \end{questions}
\end{document}

demo


The patch will make each question margin precisely aligned, but different questions will have different margins.


This solution uses the \fullwidth macro inside the questions environment. I assume you aren't planning on using a table of contents.

\documentclass[12pt,paper=a4,answers]{exam}
\usepackage{etoolbox}% opener for can of worms
\usepackage{blindtext}% MWE only

\newcommand{\mysection}[1]% #1 = title
{\stepcounter{section}%
\setcounter{question}{0}%
\fullwidth{\smallskip\textbf{\large #1}}}

\renewcommand{\thequestion}{\thesection.\arabic{question}}
\patchcmd{\questions}{10.}{\thequestion.}{}{}% fix left margin

\begin{document}
  \begin{questions}
   \mysection{First Section}
    \question
    \blindtext
    \question
    \blindtext
   \mysection{Second Section}
    \question
    First question
    \question
    Second question
  \end{questions}
\end{document}

second demo

Related Question