# Strange behaviour with \AtEndDocument

I'm creating a document class that has draft and final versions. The final version has a front page generated by \maketitle, but in draft mode \maketitle is \let to \relax. I decided to insert a test to ensure that \maketitle is present in final mode. The simplest method seemed to be to use \AtEndDocument.

\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{myexam}[2013/03/08 \space (v1.0) \space\space Ian Thompson]

\newif\iffinal %Option for final or draft mode
\finalfalse %Default to draft mode
\DeclareOption{final}{\finaltrue}
\DeclareOption{draft}{\finalfalse}
\ProcessOptions\relax

\iffinal
\typeout{FINAL MODE}
\newif\ifmaketitle
\maketitlefalse
\AtEndDocument{\ifmaketitle\relax\else \ClassError{myexam}{Final version requires maketitle}{}\fi}
\renewcommand\maketitle{THIS IS THE TITLE\newpage\maketitletrue}
\else
\typeout{DRAFT MODE}
\let\maketitle\relax
\fi


However, attempting to typeset this simple example file

\documentclass[draft]{myexam}
\begin{document}
\maketitle
The quick brown fox jumps over the lazy dog.
\end{document}


results in the error

! Class myexam Error: Final version requires maketitle

despite the fact that the example uses draft mode. Everything worked until I inserted \AtEndDocument. What is going on here?

-
changed tag [tex-core] to [latex-kernel]. still not sure if this is best choice, but [tex-core] would have been appropriate only if \bye were the trigger for the problem. – barbara beeton Mar 13 '13 at 13:20

  \newif\ifmaketitle

You need to always declare the new if otherwise the scanning of your \iffinal block goes wrong as \ifmaketitle is not an if but the else and \fi are (so terminate the \iffinal.