30

我使用下面的代码在 pdf 文档属性中设置标题和作者。

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

我想通过将它放在样式表(.sty)中来自动化它下面是我的尝试,但它不起作用。pdf 被编译(pdflatex)有错误。但 pdf 文档属性仍然为空。

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

我使用 \@title 和 \@author 变量来创建自定义标题页。所以我知道那些工作。

有什么建议么?

4

2 回答 2

33

如果你得到编译错误,我猜问题是@字符。您需要将代码包装在\makeatletterand中\makeatother。另一个可能的问题是您在执行\titleand\author命令之前执行此操作。一个很好的解决方法是使用\AtBeginDocument,这将允许您将其放置在序言中的任何位置。请注意,您必须在 之前定义\title\author信息\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}

更新:将相关部分放在名为的样式文件中xxx.sty

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

\makeatletter
\AtBeginDocument{
  \hypersetup{
    pdftitle = {\@title},
    pdfauthor = {\@author}
  }
}
\makeatother
于 2010-08-07T10:10:34.360 回答
8

有它的包选项pdfusetitle,请参阅Make hyperref take pdfinfo from \title 和 \author

于 2016-01-02T15:33:40.540 回答