[Tex/LaTex] hyperref error: Argument of \Hy@setref@link has an extra }

hyperrefpdftex

The hyperref package is returning the following error when I try to compile the pdf:

Argument of \Hy@setref@link has an extra }.
<inserted text> 
                \par
l.45

The line 45 is just an empty line between my paragraphs, there is no \ref or \label command. If I comment the \usepackage{hyperref} statement the document compiles successfully. I'm using the template from overleaf that can be found here. I tried several solutions from similar errors but none worked for me. Couldn't find open {} or other inconsistency. Deleting .aux files, compiling online on overleaf or local via texstudio and teXshop also doesn't work and return the same error message.

My document follows the structure bellow:

\documentclass{comnet}  % class from journal of complex networks
\bibpunct{[}{]}{,}{n}{,}{;}     
\usepackage[utf8]{inputenc}     
\usepackage{microtype}          
\usepackage{graphicx}           
\usepackage{algorithmicx}       
\usepackage{hyperref}           


\begin{document}

\title{Complicated title}
\shorttitle{Complicated title}
\shortauthorlist{author 1, author 2}

\author{
\name{author 1}
\address{springfield}
\name{author 2}
\address{springfield}}

\maketitle

\begin{abstract}
    {abstract text}
    {key words}
\end{abstract}


\section{Introduction}
\section{section 1}   
\subsection{section 1.1}   
\section{section 2}    
\section{Conclusion}    

\bibliographystyle{comnet}
\bibliography{mybib}

\end{document}

I also noted that if I comment the maketitle command the document is able to compile. Can anyone give a clue of what can I do to solve this?

Best Answer

The error comes not directly from \maketitle itself, but from the \thispagestyle{plain} macro usage in \maketitle.

The plain page style is defined in comnet.cls as

\def\ps@plain{%
  \def\@oddfoot{\gridpl\hfill\fontsize{6.8}{9}\selectfont  $\copyright$
    The author \number\year. Published by Oxford University Press on behalf of
    the Institute of Mathematics and its Applications. All rights reserved.}
  % \let\@evenfoot\@empty
  \def\@evnhead{\CROPMARKSA}%
  \def\@oddhead{\CROPMARKSA\begin{tabular}{@{}l}\\[-1pt]\textit{IMA Journal of Complex Networks} (\number\year) Page \fpage\ of\hspace*{2pt} \lpage\\doi:10.1093/comnet/\@kxi\end{tabular}
  }%
  \let\@mkboth\markboth
}

Now, \fpage and \lpage (used in \@oddhead) are shortcuts to

\AtBeginDocument{\label{FirstPage}}
\def\lpage{\pageref{LastPage}}
\def\fpage{\pageref{FirstPage}}

Here the real error comes in: \label{FirstPage} is written at the start of the document, due to the \AtBeginDocument hook, but hyperref is loaded later on, redefining the \label etc. macros with additional information on the labeling, meaning that \label{FirstPage} writes too few information to the .aux file for hyperref to grab it with the redefined \pageref command.

A look into the .aux file after first compilation run of the document shows (unnecessary information not given here) -- the first run is 'ok', the second compilation shows the error then!

\newlabel{FirstPage}{{}{1}}
...
\newlabel{LastPage}{{}{2}{}{page.2}{}}

It is obvious, that LastPage has more information 'slots' (especially the page.2 anchor) which is expected by hyperref to be read, but this does not work for Firstpage.

The solution is to change \fpage to \def\fpage{\pageref{CorrectFirstPageLabel}} and add the CorrectFirstPageLabel after loading of hyperref.

\documentclass{comnet}  % class from journal of complex networks
\bibpunct{[}{]}{,}{n}{,}{;}     
\usepackage[utf8]{inputenc}     
\usepackage{microtype}          
\usepackage{graphicx}           
\usepackage{algorithmicx}       
\usepackage{hyperref}           

\usepackage{blindtext}



\AtBeginDocument{
  \label{CorrectFirstPageLabel}
  \def\fpage{\pageref{CorrectFirstPageLabel}}
}

\begin{document}

\title{Complicated title}
\shorttitle{Complicated title}
\shortauthorlist{author 1, author 2}

\author{%
  \name{author 1}
  \address{springfield}
  \name{author 2}
  \address{springfield}
}

\maketitle

\begin{abstract}
    {abstract text}
    {key words}
\end{abstract}


\section{Introduction}
\section{section 1}   
\subsection{section 1.1}   
\section{section 2}    
\section{Conclusion}    

\blindtext[5]

\bibliographystyle{comnet}
%\bibliography{mybib}

\end{document}

Another solution would be to redefine \ps@plain in order to remove the \fpage macro and replace it with 1, but I don't recommend this, since other authors could make use of plain style at places in the document other than for the title page as well.

enter image description here

The error vanishes of course if hyperref is not loaded at all, whether the Journal of Complex Networks allows hyperrefed document submissions is not known to me. In doubt omit hyperref rather then.

Update

Here is a minimal non - working version that has the same issue by forcing a label in \AtBeginDocument and loading the hyperref package after that:

\documentclass{article}

\AtBeginDocument{%
  \section{Foo}\label{foolabel}
}

\usepackage{hyperref}

\begin{document}


\ref{foolabel}

\section{Foo again}\label{fooagainlabel}

\ref{fooagainlabel}

\end{document}

The .aux file contains

\newlabel{foolabel}{{1}{1}}

\newlabel{fooagainlabel}{{2}{1}{Foo again}{section.2}{}}

The foolabel is from the regular \label command as defined in latex.ltx, storing the section number 1 for the first page, whereas the second label definition is extended by hyperref to contain

  1. section number 2
  2. page number 1
  3. Section title Foo again, to be extracted with \nameref or \nameref*
  4. ('unique') hyper anchor name section.2
  5. 'unused' (empty)