! Undefined control sequence. \@urlcolor with custom documentclass

document-classeserrorshyperref

I am trying to debug a custom documentclass that I need to use, in which the hyperref package (\href command) doesn't work.

This is my document:

\documentclass{article}

\usepackage{hyperref}

\begin{document}

  \href{http://link}{text}
  
\end{document}

Compiles fine. When I change the documentclass to nohref with nohref.cls being

\def\document{
  \everypar{}
}

it fails to compile, throwing

\hyper@linkurl ...tionraw >>}\relax \Hy@colorlink 
                                                  \@urlcolor #1\Hy@xspace@en...
l.9   \href{http://link}{text}

How do I need to change my code to make this work?

Best Answer

The class file shown completely breaks LaTeX, essentially nothing will work.

Redefining \document means that none of the things that normally are set up at \begin{document} happen.

\documentclass{nohref}

\begin{document}

zzz
  
\end{document}

Produces

Overfull \hbox (20.0pt too wide) in paragraph at lines 5--6

as the text width is not set up

! LaTeX Error: The font size command \normalsize is not defined:
               there is probably something wrong with the class file.

as \normalsize is not defined

\gdef \@abspage@last{1}

as the aux file has not been opened for writing.

! I can't find file `file.aux'.
\enddocument ...keatletter \@@input \jobname .aux 

Because the aux file was not written so can not be input at the end.

Specifically with hyperref none of the setup that it normally does \AtBeginDocument happens as the redefined \document never runs that code. But the exact error is fairly arbitrary as essentially no LaTeX constructs will work and will generate errors at more or less arbitrary internal failures.


In the linked chat the OP confirmed that the actual class being used was not quite as minimal, but almost as bad, it defined \document based on the LaTeX2.09 definition so in particular it does not run any code specified to run in \AtBeginDocument{...} any code specified to run there is silently discarded.

hyperref like many other classes delays lots of its setup with \AtBeginDocument which is why the colour in particular failed.

Adding the two lines below to run the AtBeginDocumentHook is a minimal patch to make the example work, but the class has not really been usable for 30 years. Note \document has further changes to add more hooks in the 2021 release and these are all broken by this class, you would need \RequirePackage[2020/01/01]{latexrelease} in the document to undo these updates.


UPDATED AFTER COMMENTS

\def\document{\endgroup
 \@colht\textheight \@colroom\textheight \vsize\textheight
 \columnwidth\textwidth \@clubpenalty\clubpenalty
 \if@twocolumn \advance\columnwidth -\columnsep
 \divide\columnwidth\tw@ \hsize\columnwidth \@firstcolumntrue
 \fi
 \hsize\columnwidth \linewidth\hsize
 \begingroup\@floatplacement\@dblfloatplacement\endgroup
 \if@filesw \immediate\openout\@mainaux=\jobname.aux
 \immediate\write\@mainaux{\string\startlabels\string\@startlabels}\fi
% support \AtBeginDocment
  \let\AtBeginDocument\@firstofone
  \@begindocumenthook
%
 \def\do##1{\let ##1\@notprerr}
 \@preamblecmds
 \let\do\noexpand
 \@normalsize\everypar{}%
}