在 LaTeX 样式表中自动设置 pdftitle 和 pdfauthor

发布于 2024-09-12 07:05:52 字数 445 浏览 2 评论 0原文

我使用下面的代码在 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 变量来创建自定义标题页。所以我知道这些有效。

有什么建议吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

终遇你 2024-09-19 07:05:52

如果出现编译错误,我猜问题出在 @ 字符上。您需要将代码包装在 \makeatletter\makeatother 中。另一个可能的问题是您在执行 \title\author 命令之前执行此操作。一个很好的解决方法是使用 \AtBeginDocument,它允许您将其放置在序言中的任何位置。请注意,您必须在 \begin{document} 之前定义 \title\author 信息。

\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

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
烟织青萝梦 2024-09-19 07:05:52

有一个包选项 pdfusetitle ,请参阅 让 hyperref 从 \title 和 \author 获取 pdfinfo

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文