Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting pdftitle and pdfauthor automatically in a LaTeX stylesheet

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?

like image 600
Thierry Avatar asked Aug 02 '10 12:08

Thierry


2 Answers

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
like image 151
grddev Avatar answered Oct 30 '22 13:10

grddev


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

like image 39
Konstantin Möllers Avatar answered Oct 30 '22 14:10

Konstantin Möllers