1

I am currently using the following code to print the section and subsection in the header.

\pagestyle{fancy}
\fancyhf{}
\renewcommand{\headrulewidth}{0.5pt}
\renewcommand{\footrulewidth}{0.5pt}
\lhead{\textit{\leftmark}}
\rhead{\textit{\rightmark}}
\rfoot{\textit{Page \thepage}}

\renewcommand{\sectionmark}[1]{\xdef\leftmark{\thesection\quad#1}\xdef\rightmark{}}
\renewcommand{\subsectionmark}[1]{\xdef\rightmark{\thesubsection\quad#1}}

However, if I try to use \mathcal in the section titles, it causes an error. How do I get around this?

5
  • How does this differ from your earlier question: tex.stackexchange.com/q/533524 ? Apr 3 '20 at 20:42
  • 1
    Put \protect before \mathcal when the latter is used in a section (or similar) heading. Apr 3 '20 at 20:43
  • @barbarabeeton it doesn't, but no one answered my previous question
    – Manj
    Apr 3 '20 at 21:38
  • Suppose the heading is \section{Properties of \mathcal{S}_n}. Where would I add \protect?
    – Manj
    Apr 3 '20 at 21:45
  • \section{Properties of \protect\mathcal{S} ... Apr 3 '20 at 22:02
2

LaTeX will define \leftmark and \rightmark for you, don't force it.

\documentclass{article}

\usepackage{lipsum} % for mock text

\usepackage{fancyhdr}

\pagestyle{fancy}
\fancyhf{}
\renewcommand{\headrulewidth}{0.5pt}
\renewcommand{\footrulewidth}{0.5pt}
\fancyhead[L]{\itshape\leftmark}
\fancyhead[R]{\itshape\rightmark}
\fancyfoot[R]{\itshape Page \thepage}

\renewcommand{\sectionmark}[1]{\markboth{\thesection\quad#1}{}}
\renewcommand{\subsectionmark}[1]{\markright{\thesubsection\quad#1}}

\begin{document}

\title{title}
\author{me}

\maketitle

\section{Test section}

\subsection{Test subsection $\mathcal{A}$}

\lipsum[1-20]

\end{document}

enter image description here

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.