I had a similar error in similar circumstances, using Ubuntu 20.04.6 LTS, after upgrading to 2.18, check_modules.pl
and check_latex
were both fine, but there were errors of the form
! LaTeX Error: Can be used only in preamble.
See the LaTeX manual or LaTeX Companion for explanation.
Type H for immediate help.
...
l.524 \webworkSetCopyrightFooter
Your command was ignored.
...
I am posting what I found as the cause of the error.
I eventually tracked down the source of the error, and Glenn is correct. In the (new to WW 2.18) webwork2.sty
LaTeX package, the command \webworkSetCopyrightFooter
is defined. This command is invoked at the end of the hardcopy.tex
document. The definition of \webworkSetCopyrightFooter
uses the LaTeX commands \@ifpackageloaded{...}{...}
and @ifclassloaded{...}{...}
to set the copyright footer according to the class and package commands available in hardcopy.tex
.
Some versions of LaTeX throw an error when \@ifpackageloaded
or @ifclassloaded
is used in the body of a LaTeX document. Indeed, the documentation will tell you that \@ifpackageloaded
should only be used in class or style files, or in the document preamble (using \makeatletter
and \makeatother
), but never after the \begin{document}
statement in a LaTeX document. This is the source of the error. When \webworkSetCopyrightFooter
is invoked and uses \@ifpackageloaded
in the body of the LaTeX document.
This LaTeX behaviour changed in 2021. With the release of LaTeX 2021-11-15, the @\if(something)loaded
commands work in a latex document without issue (see LaTeX2e News Issue 34, Tests for package and class loading on page 4 and, in particular, footnote 2).
This explains why Andras' solution works. To avoid this error, update LaTeX to a version 2021-11-15 or later.
It is also possible to patch the definition of \webworkSetCopyrightFooter
to work in older versions of LaTeX.
The following definition of \webworkSetCopyrightFooter
works in older and newer versions of LaTeX:
%%%%%%%%%%
% A definition of \webworkSetCopyrightFooter
% that works in old and new latexs
% 2024-08-22
%%%%%%%%%%
% Define the footer components
\newcommand{\webworkSetCopyrightFooterLeft}{%
\raisebox{-0.325cm}{\includegraphics[width=3cm]{webwork_logo.png}}%
}
\newcommand{\webworkSetCopyrightFooterCenter}{%
\small\sffamily Generated by WeBWorK, \copyright~The~WeBWorK~Project.%
}
\newcommand{\webworkSetCopyrightFooterRight}{%
%\url{openwebwork.org}%
\href{https:\\openwebwork.org}{openwebwork.org}
}
% Define the macro that declares the copyright
% A format using fancyhdr
% A format for the exam class
% Empty otherwise
\AtBeginDocument{%
\newcommand{\webworkSetCopyrightFooter}{\relax}
\makeatletter%
\@ifpackageloaded{fancyhdr}{%
\@ifpackageloaded{tcolorbox}{%
\renewcommand{\webworkSetCopyrightFooter}{%
\fancyfoot[L]{\webworkSetCopyrightFooterLeft}%
\fancyfoot[C]{\webworkSetCopyrightFooterCenter}%
\fancyfoot[R]{\webworkSetCopyrightFooterRight}%
\pagestyle{fancy}%
}%
}%
{%
\renewcommand{\webworkSetCopyrightFooter}{%
\fancyfoot[L]{\webworkSetCopyrightFooterLeft}%
\fancyfoot[C]{\webworkSetCopyrightFooterCenter}%
\fancyfoot[R]{\webworkSetCopyrightFooterRight}%
\pagestyle{fancy}%
\clearpage%
}%
}%
{}%
}%
\@ifclassloaded{exam}%
{%
\renewcommand{\webworkSetCopyrightFooter}{%
\footer%
{\webworkSetCopyrightFooterLeft}%
{\webworkSetCopyrightFooterCenter}%
{\webworkSetCopyrightFooterRight}%
\clearpage
}%
}%
{}%
\makeatother%
}