# pass parameters to pdf metadata in hyperref

I'm trying to pass data to metadata setup of the .pdf file with this code but its brings me an error, when passing the data

\def\author#1{\gdef\insertauthor{#1}\gdef\@author{#1}}
\def\title#1{\gdef\inserttitle{#1}\gdef\@title{#1}}
\def\firstsupervisor#1{\gdef\insertsupervisor{#1}}
\def\secondsupervisor#1{\gdef\insertsupervisor{#1}}
\def\institution#1{\gdef\insertinstitution{#1}}
\def\degree#1{\gdef\insertdegree{#1}}
\def\faculty#1{\gdef\insertfaculty{#1}}
\def\department#1{\gdef\insertdepartment{#1}}
\def\submitdate#1{\gdef\insertsubmitdate{#1}}
\def\keywords#1{\gdef\insertkeywords{#1}}
\submitdate{\number\the\year}

\hypersetup{%
pdftitle    = {\inserttitle},
pdfkeywords = {\insertkeywords},
pdfcreator  = {\LaTeX\ with package \flqq hyperref\frqq},
pdfproducer = {pdfeTeX-0.\the\pdftexversion\pdftexrevision}
}
\RequirePackage{hypcap}


the error says Undefined control sequence. }

• By the time of setting content like pdftitle, is \inserttitle defined? Probably not. – Werner Jul 9 '15 at 1:40
• when I use \title{the title} in other spaces I can get the title with \inserttitle and it works fine on all places except here – efirvida Jul 9 '15 at 1:51

\hypersetup comes into action before \title is used, so it tries to get grip of an undefined command \inserttitle at that time. The same is true for \insertauthor and \insertkeywords. LaTeX is complaining on \inserttitle because its the first of those three commands used in \hypersetup.

Since \hypersetup is no preamble-restricted command, it can be used more than once and even in the document body, if needed.

I suggest to use the \title etc. commands to automatically insert the relevant data. This works, as long as \title etc. are not used before hyperref is loaded and preset \hypersetup only with basically-non-author/document content relevant data

Apparently the shown fragment is from a .cls or .sty file, I've built a document around it, wrapping \makeatletter...\makeatother pair around the various \def.... commands there.

\documentclass{article}

\makeatletter

\def\title#1{\gdef\inserttitle{#1}\gdef\@title{#1}\hypersetup{pdftitle={#1}}}
\def\firstsupervisor#1{\gdef\insertsupervisor{#1}}
\def\secondsupervisor#1{\gdef\insertsupervisor{#1}}
\def\institution#1{\gdef\insertinstitution{#1}}
\def\degree#1{\gdef\insertdegree{#1}}
\def\faculty#1{\gdef\insertfaculty{#1}}
\def\department#1{\gdef\insertdepartment{#1}}
\def\submitdate#1{\gdef\insertsubmitdate{#1}}
\def\keywords#1{\gdef\insertkeywords{#1}\hypersetup{pdfkeywords={#1}}}
\submitdate{\number\the\year}

\makeatother

\hypersetup{%
%    pdftitle    = {\inserttitle},%
%    pdfkeywords = {\insertkeywords},
pdfcreator  = {\LaTeX\ with package \flqq hyperref\frqq},
pdfproducer = {pdfeTeX-0.\the\pdftexversion\pdftexrevision}
}

\title{Theory on Brontosaurs}
\author{Miss Ann Elk}
\keywords{Brontosaurs,Monty Python,LaTeX is fun}

\begin{document}
\maketitle%

Lorem ipsum

\end{document}


Please note, that \maketitle redefines \title, and \author etc. in many classes to become effectively nothing.

The first call to \hypersetup where you want to set the PDF document properties requires all of the macros to exist, otherwise there you're receive an error message. My suggestion would therefore be to make sure all of these new \insert... macros exist. Do so by adding

\providecommand{\insertauthor}{}
\providecommand{\inserttitle}{}
\providecommand{\insertfirstsupervisor}{}
\providecommand{\insertsecondsupervisor}{}
\providecommand{\insertinstitution}{}
\providecommand{\insertdegree}{}
\providecommand{\insertfaculty}{}
\providecommand{\insertdepartment}{}
\providecommand{\insertsubmitdate}{}
\providecommand{\insertkeywords}{}


to your preamble. Secondly, delay the setting of PDF document properties until the end of the document using

\AtEndDocument{
\hypersetup{
pdftitle    = \inserttitle,
pdfkeywords = \insertkeywords,
pdfauthor   = \insertauthor
}
}