[Tex/LaTex] Conflict between exam class and fancyhdr package

examfancyhdr

With the code below:

\documentclass[answers,a4paper]{exam}
\usepackage{fancyhdr}
\begin{document}
hello world
\end{document}

I get the build error:

! LaTeX Error: Command \lhead already defined.
           Or name \end... illegal, see p.192 of the manual.

I need to manually set the header and foot in my page using fancyhdr. So is there any way to disable the header and foot setting in exam class?

Best Answer

The exam class has its own built-in fancy formatting of headers and footers. Since both the exam class and fancyhdr package use the same names for defining the headers and footers, you get the clash.

You can get the details on how to define headers and footers in the exam class in section 11 of the package document, titled Headers and footers.

So, without disabling the header and footer in exam class, it would be better to use the commands of that class. Since the basic concept is very similar to fancyhdr, you will not face much difficulty in mastering the techniques.


For the impatient, some quick examples are:

\lhead{CSE 101}
\chead[\bfseries\large Midterm Examination]{}
\rhead[January 01, 2020]{Midterm Examination Continued}

\lfoot{}
\cfoot{}
\rfoot[]{Page \thepage\ of \numpages}