0

I was scouring the internet for some time now and could not figure out how this could be done. I am trying to add the current section name to the header of a page in the exam class which treats headers and footers a bit differently. I renewed the commands \thesection and \thesubsection to set roman numbers for sections (I, II, III, ...) and alphabet for subsections (A, B, C, ...). I think that inteferes with the headings. Is there a way around this problem?

\documentclass[12pt,a4paper]{exam}

%formatting
\usepackage[T1]{fontenc}
\usepackage[english]{babel}
\usepackage[utf8]{inputenc}

%packages
    %maths
\usepackage{amsfonts, amsmath}
    %blind text
\usepackage{lipsum}

%headers and footers
\pagestyle{headandfoot}
    %headers
    \lhead{\thesection}
    \chead{}
    \rhead{\thesubsection}
    %footers
    \lfoot{}
    \cfoot[]{Page \thepage\ of \numpages}
    \rfoot{}
    %line under head
    \runningheadrule

%sets roman numbers for sections (I, II, III, ...) alphabet for subsections (A, B, C, ...)
\renewcommand{\thesection}{\Roman{section}}
\renewcommand{\thesubsection}{\thesection.\Alph{subsection}}

%title
\author{Author} \title{Title} \date{Last updated: \today}



\begin{document} \maketitle \tableofcontents \newpage

%first section
\section{Section Name}
    \subsection{Subsection Name}
    \lipsum[3]
    
\end{document}

Output

1 Answer 1

2

Normally \section uses \sectionmark to store the section name and \subsection uses \subsectionmark to store the subsection name, although in this case both were set to not store anything (\@gobble). When they are stored, they normally use \markboth and \markright, but these would delay the definition for a page (each).

\documentclass[12pt,a4paper]{exam}

%formatting
\usepackage[T1]{fontenc}
\usepackage[english]{babel}
\usepackage[utf8]{inputenc}

%packages
    %maths
\usepackage{amsfonts, amsmath}
    %blind text
\usepackage{lipsum}

%headers and footers
\pagestyle{headandfoot}
    %headers
    \lhead{\leftmark}
    \chead{}
    \rhead{\rightmark}
    %footers
    \lfoot{}
    \cfoot[]{Page \thepage\ of \numpages}
    \rfoot{}
    %line under head
    \runningheadrule
    
    \def\sectionmark#1{\xdef\leftmark{\thesection\space #1}}% \markboth{\thesection\space #1}{}
    \def\subsectionmark#1{\xdef\rightmark{\thesubsection\space #1}}% \markright{\thesubsection\space #1}

%sets roman numbers for sections (I, II, III, ...) alphabet for subsections (A, B, C, ...)
\renewcommand{\thesection}{\Roman{section}}
\renewcommand{\thesubsection}{\thesection.\Alph{subsection}}

%title
\author{Author} \title{Title} \date{Last updated: \today}



\begin{document} \maketitle \tableofcontents \newpage

%first section
\section{Section Name}
    \subsection{Subsection Name}
    \lipsum[2]
    
\end{document}
1
  • Works like a charm. Thanks! Commented Jan 29, 2023 at 11:31

You must log in to answer this question.

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