views:

107

answers:

1

I use the code below to set the title and author in the pdf document properties.

\usepackage[pdftex]{hyperref}
\hypersetup{
    pdftitle = {The documents title},
    pdfauthor = {me}
}

I would like to automate this by putting it in a stylesheet (.sty) Below is my attempt, but it is not working. The pdf is compiled (pdflatex) with errors. But the pdf document properties remain empty.

\usepackage[pdftex]{hyperref}
\hypersetup{
    pdftitle = {\@title},
    pdfauthor = {\@author}
}

I use the \@title and the \@author variables to create a custom titlepage. So I know those work.

Any suggestions?

+1  A: 

If you get compile errors, I'm guessing the problem is the @ character. You need to wrap your code in \makeatletter and \makeatother. Another possible problem is that you do this before you execute the \title and \author commands. A nice fix for this would be to use \AtBeginDocument, which would allow you to place this anywhere in your preamble. Note that you have to define the \title and \author information before \begin{document}.

\documentclass{article}
\usepackage[pdftex]{hyperref}

\makeatletter
\AtBeginDocument{
  \hypersetup{
    pdftitle = {\@title},
    pdfauthor = {\@author}
  }
}
\makeatother

\title{Test title}
\author{Sam Author}

\begin{document}
\maketitle
\end{document}

UPDATE: Putting the relevant parts in a style file named xxx.sty:

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{xxx}
\RequirePackage{hyperref}

\makeatletter
\AtBeginDocument{
  \hypersetup{
    pdftitle = {\@title},
    pdfauthor = {\@author}
  }
}
\makeatother
grddev
Your solution works fine when put in the main tex file. However, it does not work when you put it in a .sty file.
Thierry
@Thierry: I have no problems putting this in a `.sty` file.
grddev
After your update, everything works fine. Thank you for the answer.
Thierry