Setting pdftitle and pdfauthor automatically in a LaTeX stylesheet

20.9k views Asked by At

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?

2

There are 2 answers

4
grddev On BEST ANSWER

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
1
Konstantin Möllers On

There is the package option pdfusetitle for it, see Make hyperref take pdfinfo from \title and \author.