1

In my MWE, \hypersetup is called after \cleardoublepage. But \hypersetup does not work well after \cleardoublepage, i.e., no PDF metadata in the document properties.

\documentclass{book}

\usepackage{hyperref}
\usepackage{blindtext}

\begin{document}

\blindtext

\cleardoublepage

\blindtext

\hypersetup{pdftitle={test}}

\end{document}
  • Why not use \hypersetup in the preamble of the document? – leandriis Jun 10 at 19:27
  • Because the document has some special requirements, \hypersetup must be called after \cleardoublepage. Do you have some better ideas to solve this problem? – DangeRS2 Jun 10 at 19:30
  • Here the title is just an example. In fact, I want to call \hypersetup to set other metadata. – DangeRS2 Jun 10 at 19:42
  • same applies to all the document metadata, you can only have one entry. hypersetup to change link colours etc is not a problem. – David Carlisle Jun 10 at 19:51
  • Yes, but some values of metadata are given after \cleardoublepage in my document. – DangeRS2 Jun 10 at 19:54
3

hyperref calls the command to set the metadata after the first page. So every \hypersetup on following page is ignored. You can set the title with primitives, but then it is up-to-you to ensure that the content is valid pdf. And you should better remove the metadata from the hyperref command to avoid to have two titles in the info dictionary:

\documentclass{book}

\usepackage[]{hyperref}
\usepackage{blindtext}
\usepackage{xpatch}
\makeatletter
\xpatchcmd\PDF@FinishDoc{/Title(\@pdftitle)}{}{}{\fail}%
\makeatletter
\begin{document}

\blindtext

\cleardoublepage

\blindtext

\pdfinfo{/Title (test)}

\end{document}

But on the whole it is imho better to change whatever forces you to set the data later.

  • Thanks for your answer! But I have the following error, i.e., Undefined control sequence. ^^I\pdfinfo. – DangeRS2 Jun 10 at 20:12
  • how are you compiling? – Ulrike Fischer Jun 10 at 20:23
  • When I use pdflatex to compile your MWE, it is correct. However, when I use xelatex, it will show me the error. My local environment is TeXLive 2017 + TeXStudio. – DangeRS2 Jun 10 at 20:30
  • With xelatex you need \special{pdf:docinfo<</Title (test)>>}, but David's suggestion to go through the aux is certainly better. – Ulrike Fischer Jun 10 at 20:42
  • Thanks again! Sorry, I just don't know how to do as David's suggestion...How to go through the aux? – DangeRS2 Jun 10 at 20:59
0

An example using aux mechanism:

\documentclass{article}
\usepackage{hyperref}

% usage of pdftitle in preamle is overwritten
\hypersetup{pdftitle={preamble}}

\makeatletter
\newcommand\setPdfTitle[1]{%
  \protected@write\@auxout{}{%
    \string\ifx\string\@onlypreamble\string\@notprerr
    \string\else
      \string\AtBeginDocument{\string\def\string\@pdftitle{#1}}%
    \string\fi
  }%
}
\makeatother

\begin{document}
a \newpage

\setPdfTitle{set title very late}

b
\end{document}

Another example, which delays the execution of \PDF@FinishDoc until the end of the document, can been found in this github comment.

These two examples are both independent of engines.

  • Thanks for your answer! – DangeRS2 Jun 11 at 0:22
  • 1
    Delaying to the end of the document can fail in certain cases - so one should check that the special didn't get lost. – Ulrike Fischer Jun 11 at 6:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.